http 请求导致堆损坏?

http requests are causing heap corruption?

我正在尝试从我的 C++ 项目向 php 脚本发送一些数据,如下所示:

void sendThatBitch()
{
    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    vector <string>  vFileContent;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"WinHTTP Example/1.0",
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
    {
        cout << "WinHttpOpen\n";
        hConnect = WinHttpConnect(hSession, L"example.com", INTERNET_DEFAULT_HTTP_PORT, 0);
    }

    // Create an HTTP request handle.
    if (hConnect)
    {
        cout << "WinHttpConnect\n";

        string req = "/script.php?data1=";
        req += DATA1;
        req += "&data2=";
        req += DATA2;
        req += '[=10=]';

        WCHAR* str = new WCHAR(req.size() - 1);

        MultiByteToWideChar(0, 0, req.c_str(), req.size(), str, req.size());

        hRequest = WinHttpOpenRequest(hConnect, L"GET", str, NULL, WINHTTP_NO_REFERER, NULL, NULL);
    }

    // Send a request.
    if (hRequest)
    {
        cout << "WinHttpOpenRequest\n";
        bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    }

    // End the request.
    if (bResults)
    {
        cout << "WinHttpSendRequest\n";
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }

    // Close any open handles.
    if (hRequest)
    {
        WinHttpCloseHandle(hRequest);
    }

    if (hConnect)
    {
        WinHttpCloseHandle(hConnect);
    }

    if (hSession)
    {
        WinHttpCloseHandle(hSession);
    }
}

现在我的问题是大多数时候都会出现,但并非总是如此。有时确实会发送数据,但当我收到错误时,它看起来像这样:

Critical error detected c0000374 application.exe has triggered a breakpoint.

First-chance exception at 0x77EFE653 (ntdll.dll) in application.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77F34268).

Unhandled exception at 0x77EFE653 (ntdll.dll) in application.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77F34268).

这发生在天气 WinHttpOpenRequestWinHttpSendRequest。有人知道为什么大多数时候会发生这种情况吗?

注意:此代码中提到的域是假的,而不是真实代码中使用的域。

我在上面的评论中关注了 ChrisWard1000 的 link,它现在通过这样做来工作:

if (hConnect)
{
    cout << "WinHttpConnect\n";

    string req = "/script.php?data1=";
    req += DATA1;
    req += "&data2=";
    req += DATA2;
    req += '[=10=]';

    int wchars_num = MultiByteToWideChar(CP_UTF8, 0, req.c_str(), -1, NULL, 0);
    wchar_t* str = new wchar_t[wchars_num];

    MultiByteToWideChar(0, 0, req.c_str(), req.size(), str, req.size());

    hRequest = WinHttpOpenRequest( hConnect, L"GET", str, NULL, WINHTTP_NO_REFERER, NULL, NULL);

    delete[] str;
}

+1 给那个建议,谢谢你们。