c-为什么对RasGetEntryProperties()的第二次调用返回一个正在进行的重叠I/O操作



有这样的代码:

#include <ras.h>
#include <raserror.h>
#include <stdio.h>
#include <windows.h>
int main(int argc, char** argv) {
DWORD dwEntryInfoSize = 0;
DWORD dwDeviceInfoSize = 0;
DWORD dwRet = 0;
LPRASENTRY lpRasEntry = NULL;
LPBYTE lpDeviceInfo = NULL;

创建RAS连接。获取默认电话簿条目的缓冲区大小信息。

if ((dwRet = RasGetEntryProperties(NULL, "", NULL, &dwEntryInfoSize, NULL, &dwDeviceInfoSize)) != ERROR_SUCCESS) {
if (dwRet != ERROR_BUFFER_TOO_SMALL) {
printf("RasGetEntryProperties error: %sn", GetLastError());
return EXIT_FAILURE;
}
}
if (dwEntryInfoSize == 0) {
printf("Entry info size error: %sn", GetLastError());
return EXIT_FAILURE;
}

lpRasEntry = (LPRASENTRY) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwEntryInfoSize);
if (lpRasEntry == NULL) {
printf("HeapAlloc RasEntry error: %sn", GetLastError());
return EXIT_FAILURE;
}
if (dwDeviceInfoSize) {
lpDeviceInfo = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwDeviceInfoSize);
}
lpRasEntry->dwSize = sizeof(RASENTRY);

这里有一个错误:重叠I/O操作正在进行中。该错误发生在Windows XP和Windows 10中,一切正常。我需要这段代码才能在XP中工作。

if ((dwRet = RasGetEntryProperties(NULL, "", lpRasEntry, &dwEntryInfoSize, lpDeviceInfo, &dwDeviceInfoSize)) != ERROR_SUCCESS) {
printf("RasGetEntryProperties error: %sn", GetLastError());
return EXIT_FAILURE;
}
// Validate new phonebook name "TestEntry"
if ((dwRet = RasValidateEntryName(NULL, "TestEntry")) != ERROR_SUCCESS) {
printf("RasValidateEntryName error: %sn", GetLastError());
return EXIT_FAILURE;
}
// Install a new phonebook entry, "TestEntry", using default properties
if ((dwRet = RasSetEntryProperties(NULL, "TestEntry", lpRasEntry, dwEntryInfoSize, lpDeviceInfo, dwDeviceInfoSize)) != ERROR_SUCCESS) {
printf("RasSetEntryProperties error: %sn", GetLastError());
return EXIT_FAILURE;
}
// Deallocate memory for the connection buffer
HeapFree(GetProcessHeap(), 0, lpRasEntry);
lpRasEntry = NULL;
return EXIT_SUCCESS;
}

此代码创建拨号连接。我被这个bug卡住了。

在此处输入图像描述

代码显示错误。。。

错误#1:Ras*函数没有为GetLastError()设置错误代码。那些GetLastError()没有意义。它们以DWORD形式返回错误代码。请阅读MSDN中的路由和远程访问错误代码。

错误#2:从RasGetEntryProperties()函数5和6中的NT开始,未使用参数,参数应为NULL。

错误#3:最阴险的。事实是,某些Windows操作系统的RASENTRY结构的大小不同。例如,在XP和10之间。因此,在头文件的某个地方,我们应该指定这样的内容:

#define WINVER 0x0501
#define _WIN32_WINNT 0x0501

更多详细信息可以在MSDN中阅读。修改WINVER和_WIN32_WINNT。在这种情况下,我们指定了Windows XP的版本。这正是我所需要的。在这种情况下,正在进行的重叠I/O操作与我们无关。我们只是输出GetLastError()的结果,当执行以前的函数时,它可能出现在Windows深处的某个地方。

最新更新