Search code examples
c++windowswinapidevicecreatefile

Win32-Api CreateFile(...) in C++ failed


The following code is working under one PC but not on an other PC. Both PC's have Windows 7 as their OS.

char device_name[] = "\\\\.\\interception00";
printf("device_name: %s \n", device_name);

device_array[i].handle = CreateFile(device_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

DWORD error = GetLastError();
printf("GetLastError (Number): %d, ", error);

if (error == ERROR_FILE_NOT_FOUND)
{
    printf("error == ERROR_FILE_NOT_FOUND \n");
}
else if (error == ERROR_SUCCESS)
{
    printf("error == ERROR_SUCCESS \n");
}
else
{
    printf("error == UNBEKANNT \n");
}

The Output of the PC that is successfully opens the File:

device_name: \\.\interception00
GetLastError (Number): 0, error == ERROR_SUCCESS

The other PC is not able to open the File. The Output is:

device_name: \\.\interception00
GetLastError (Number): 2, error == ERROR_FILE_NOT_FOUND 

Does someone have a clue why this happens ? Maybe it is a problem of insufficient rights ?


Solution

  • From MSDN's CreateFile() documentation:

    OPEN_EXISTING

    Opens a file or device, only if it exists.

    If the specified file or device does not exist, the function fails and the last-error code is set to ERROR_FILE_NOT_FOUND (2).

    So this means that \\\\.\\interception00 exists on one computer but not the other. Try to look with some other program that is normally capable of opening the File/Device.

    Or just fix that \\\\.\\interception00 is not available.