关于Windows:C中的Win32-Api CreateFile(…)失败

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

以下代码在一台PC上运行,但在另一台PC上不运行。两台PC都将Windows 7作为其操作系统。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char device_name[] ="\\\\\\\\.\\\\interception00";
printf("device_name: %s \
"
, 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 \
"
);
}
else if (error == ERROR_SUCCESS)
{
    printf("error == ERROR_SUCCESS \
"
);
}
else
{
    printf("error == UNBEKANNT \
"
);
}

成功打开PC的输出:

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

另一台PC无法打开文件。输出为:

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

有人知道为什么会这样吗?也许是权限不足的问题?


来自MSDN的CreateFile()文档:

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).

因此这意味着\\\\\\\\.\\\\interception00在一台计算机上存在,但另一台计算机上不存在。尝试查看其他通常可以打开文件/设备的程序。

或者只是修复\\\\\\\\.\\\\interception00不可用。