以下代码或多或少只是从 MSDN 文档中复制而来。在 Windows Vista 和 Windows 7 上它工作正常,但在 XP 上崩溃时调用InternetQueryOption
. 任何人都知道问题可能是什么?
int main(int argc, char* argv[])
{
HINTERNET hInternet = InternetOpen("x", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
//
// 1. Set
//
{
INTERNET_PER_CONN_OPTION_LIST List;
INTERNET_PER_CONN_OPTION Option[2];
unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
Option[0].Value.pszValue = "http://myserver/get_proxy_info.dll";
Option[1].dwOption = INTERNET_PER_CONN_FLAGS;
Option[1].Value.dwValue = PROXY_TYPE_AUTO_PROXY_URL;
List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection = NULL;
List.dwOptionCount = 2;
List.dwOptionError = 0;
List.pOptions = Option;
if(!InternetSetOption(hInternet, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, nSize))
printf("InternetQueryOption failed! (%d)\n", GetLastError());
}
//
// 2. Query
//
{
INTERNET_PER_CONN_OPTION_LIST List;
INTERNET_PER_CONN_OPTION Option[5];
unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
Option[1].dwOption = INTERNET_PER_CONN_AUTODISCOVERY_FLAGS;
Option[2].dwOption = INTERNET_PER_CONN_FLAGS;
Option[3].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
Option[4].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection = NULL;
List.dwOptionCount = 5;
List.dwOptionError = 0;
List.pOptions = Option;
printf("InternetQueryOption:\n");
if(!InternetQueryOption(hInternet, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, &nSize))
printf("InternetQueryOption failed! (%d)\n", GetLastError());
if(Option[0].Value.pszValue != NULL)
printf("%s\n", Option[0].Value.pszValue);
if((Option[2].Value.dwValue & PROXY_TYPE_AUTO_PROXY_URL) == PROXY_TYPE_AUTO_PROXY_URL)
printf("PROXY_TYPE_AUTO_PROXY_URL\n");
if((Option[2].Value.dwValue & PROXY_TYPE_AUTO_DETECT) == PROXY_TYPE_AUTO_DETECT)
printf("PROXY_TYPE_AUTO_DETECT\n");
INTERNET_VERSION_INFO Version;
nSize = sizeof(INTERNET_VERSION_INFO);
InternetQueryOption(hInternet, INTERNET_OPTION_VERSION, &Version, &nSize);
if(Option[0].Value.pszValue != NULL)
GlobalFree(Option[0].Value.pszValue);
if(Option[3].Value.pszValue != NULL)
GlobalFree(Option[3].Value.pszValue);
if(Option[4].Value.pszValue != NULL)
GlobalFree(Option[4].Value.pszValue);
}
}
在 Windows XP 中,它在试图访问NULL
指针后面的内容时遇到内存访问冲突。
wininet.dll!PROXY_SERVER_LIST::CopyString(void) Unknown
wininet.dll!PROXY_INFO::GetProxySettings(struct INTERNET_PROXY_INFO_EX *,int) Unknown
wininet.dll!PROXY_INFO_GLOBAL::GetProxySettings(struct INTERNET_PROXY_INFO_EX *,int) Unknown
wininet.dll!QueryPerConnOptions(void *,int,struct INTERNET_PER_CONN_OPTION_LISTA *) Unknown
wininet.dll!_InternetQueryOptionA@16() Unknown
> InternetQueryOption.exe!main(int argc=1, char * * argv=0x003e2e60) Line 55 C++
InternetQueryOption.exe!__tmainCRTStartup() Line 278 C