2013-03-31 67 views
0

我試圖在C++中使用WinHTTP執行HTTP GET,但是在解析名稱後(在狀態回調函數中獲得WINHTTP_CALLBACK_STATUS_NAME_RESOLVED之後)它在某個時刻崩潰了。我在winhttp.dll中遇到訪問衝突。調用堆棧是:!!!winhttp.dll中的訪問衝突

winhttp.dll HTTP_USER_REQUEST :: _ IndicateSocketAddress()+ 0x221ed字節
winhttp.dll HTTP_USER_REQUEST :: OnDnsNameResolved()+ 0X24字節
winhttp.dll WEBIO_REQUEST :: _ OnInformation() + 0x1c0c字節 [email protected]()+ 0x15a字節 [email protected]()+ 0x7a字節
[email protected]()+值爲0x2F字節
webio.dll!_WapCallDnsQueryCompletion @ 12()+ 0x6d bytes webio.dll!_WaDnsResolutio nWorker @ 8()+ 0x157字節 [email protected]()+ 0x7b字節
ntdll.dll中! TppWorkerThread @ 4()+ 0x5a4字節
KERNEL32.DLL!@ BaseThreadInitThunk @ 12()+ 0×12字節
NTDLL.DLL! !
__RtlUserThreadStart @ 8()+ 0×27字節
NTDLL.DLL __ RtlUserThreadStart @ 8()+ 0x1b字節

相關的代碼是:

enum OnlineState 
{ 
    OnlineState_Idle, 
    OnlineState_Registering 
}; 

static OnlineState g_OnlineState = OnlineState_Idle; 
HINTERNET g_Request = 0; 
HINTERNET g_Connection = 0; 
unsigned char g_HTTPBuffer[1024]; 

void HTTPRequestComplete() 
{ 
    if(g_Request != 0) 
    { 
     WinHttpCloseHandle(g_Request); 
     g_Request = 0; 
    } 
    if(g_Connection != 0) 
    { 
     WinHttpCloseHandle(g_Connection); 
     g_Connection = 0; 
    } 
    g_OnlineState = OnlineState_Idle; 
} 

void HTTPAsyncCallback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) 
{ 
    switch(dwInternetStatus) 
    { 
     case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE: 
     { 
      // Get the response 
      if (!WinHttpReceiveResponse(g_Request, 0)) 
      { 
      // Failed to get the response 
       HTTPRequestComplete(); 
       return; 
      } 
     } 
     break; 

     case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE: 
     { 
      DWORD statusCode = 0; 
      DWORD statusCodeSize = sizeof(DWORD); 

      if (!WinHttpQueryHeaders(g_Request, 
          WINHTTP_QUERY_STATUS_CODE |  WINHTTP_QUERY_FLAG_NUMBER, 
          WINHTTP_HEADER_NAME_BY_INDEX, 
          &statusCode, 
          &statusCodeSize, 
          WINHTTP_NO_HEADER_INDEX)) 
      { 
       // Failed to query headers 
       HTTPRequestComplete(); 
       return; 
      } 

      if (HTTP_STATUS_OK != statusCode) 
      { 
       // Error status 
       HTTPRequestComplete(); 
       return; 
      } 

      if (!WinHttpReadData(g_Request, 
          g_HTTPBuffer, 
          sizeof(g_HTTPBuffer), 
          0)) 
      { 
       // Error reading data 
       HTTPRequestComplete(); 
       return; 
      } 
     } 
     break; 

     case WINHTTP_CALLBACK_STATUS_READ_COMPLETE: 
     { 
      if (dwStatusInformationLength > 0) 
      { 
       // Store the data 

       // Read the next data 
       if (!WinHttpReadData(g_Request, 
            g_HTTPBuffer, 
             sizeof(g_HTTPBuffer), 
            0)) 
       { 
        // Error 
       HTTPRequestComplete(); 
       return; 
      } 
     } 
     else 
     { 
      // Request completed OK 
      HTTPRequestComplete(); 
     } 
    } 
    break; 

    default: 
     break; 
    } 
} 

// Online functionality 
void Online_UpdateServer() 
{ 
    switch(g_OnlineState) 
    { 
    case OnlineState_Idle: 
     { 
      // Get our local ip address by connecting a local socket to a web address and reading the socket name 

      // Look up the address to connect to 
      addrinfo hints; 
      addrinfo* res = 0; 
      memset(&hints, 0, sizeof hints); 
      hints.ai_family = AF_UNSPEC; 
      hints.ai_socktype = SOCK_DGRAM; 
      getaddrinfo("www.google.com", "80", &hints, &res); 

      // Create the socket 
      int tempSocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 
      unsigned int localAddress = 0; 
      if (tempSocket >= 0) 
      { 
       // Connect the socket 
       connect(tempSocket, res->ai_addr, res->ai_addrlen); 

       // Get the socket name (our local ip address) 
       sockaddr_in localName; 
       memset(&localName, 0, sizeof(localName)); 
       int bufferSize = sizeof(localName); 
       if(getsockname(tempSocket, (sockaddr*)&localName, &bufferSize) == 0) 
       { 
        // Get the IP address 
        localAddress = localName.sin_addr.S_un.S_addr; 
       } 

       closesocket(tempSocket); 
      } 

      // Connect 
      g_Connection = WinHttpConnect(g_Internet, L"www.google.com", INTERNET_DEFAULT_PORT, 0); 

      // Open the request 
      std::wstringstream urlString; 
      urlString << L"/"; 
      std::wstring tempString = urlString.str(); 
      const wchar_t* wurlString = tempString.c_str(); 
      g_Request = WinHttpOpenRequest(g_Connection, 0, wurlString, 0, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0); 

      // Install the status callback function. 
      if(WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(g_Request, (WINHTTP_STATUS_CALLBACK)HTTPAsyncCallback, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, NULL)) 
      { 
       OutputDebugString(L"Error"); 
      } 

      // Send the request 
      if(!WinHttpSendRequest(g_Request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) 
      { 
       OutputDebugString(L"Error"); 
      } 

      // Log that we're registering 
      g_OnlineState = OnlineState_Registering; 
     } 
     break; 

    case OnlineState_Registering: 
     { 
      // Don't do anything, as we're currently registering 
     } 
     break; 

    default: 
     break; 
    } 
} 

g_Internet被初始化這樣的:

// Initialise HTTP 
g_Internet = WinHttpOpen(L"Boomba", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC); 

至於我可以告訴大家,我初始化並正確使用WinHTTP的,一切似乎又回來了確定沒有錯誤。回調被調用了兩次,第一次與WINHTTP_CALLBACK_STATUS_RESOLVING_NAME,然後用WINHTTP_CALLBACK_STATUS_NAME_RESOLVED,那之後我得到一個訪問衝突:

0000005:訪問衝突讀取位置0x00000014

的位置變化,各種各樣的東西,所以我我認爲它看起來可能是一個線程問題,但我不確定可能是什麼問題。有任何想法嗎? (我在Windows 7 64位上運行)。

+0

哪行代碼?也許有什麼東西在堆棧上被損壞? – OldProgrammer

回答

2
if (WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(
     g_Request, 
     (WINHTTP_STATUS_CALLBACK)HTTPAsyncCallback,  // <=== evil 
     WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 
     NULL)) 

編譯器原本抱怨的功能和所需的函數指針類型之間的不匹配。通過關閉編譯器來解決問題。但是這並沒有解決問題。現在你給自己買了一個真正的問題,一個損壞的堆棧。很難診斷。

回調函數必須聲明爲__stdcall,而不是默認__cdecl調用約定。 Windows頭文件使用CALLBACK宏。修復:

void CALLBACK HTTPAsyncCallback(/* etc*/) 

當然,刪除該演員。

+0

謝謝,那正是錯誤的地方。我從Microsoft文檔複製了對WinHttpSetStatusCallback的調用,該文檔中已經有了cast,但沒有顯示它們的回調聲明,所以我從來沒有看到編譯器錯誤。不知道爲什麼他們會在文檔中輸入。感謝:) –

+0

這可能是2歲,但這是我剛剛確切的問題。謝謝! –