0

Whenever I try to call the WinHttpSendRequest() function, it always returns null.

It is worth mentioning that the function works fine in a normal .exe, but doesn't work in a .dll.

I was trying to find a solution for a long time.

Here is the code:

bool c_lw_http::send_request(std::wstring s_url, std::vector<BYTE>& bt_reply, const PWCHAR psz_type, const LPVOID p_data, const DWORD dw_data_len)
{
    bool b_result = false;

    if (!m_h_session_) return b_result;

    INTERNET_PORT w_srv_port = 0;
    std::wstring s_server, s_object;
    parse_url_a(s_url, s_server, s_object, w_srv_port);

    const HINTERNET h_connect = WinHttpConnect((HINTERNET)m_h_session_, (LPCWSTR)s_server.c_str(), (INTERNET_PORT)w_srv_port, (DWORD)0);
    if (!h_connect) return b_result;

    LPCWSTR psz_accept_types[2];
    psz_accept_types[0] = L"*/*";
    psz_accept_types[1] = NULL;

    const HINTERNET h_request = WinHttpOpenRequest(h_connect, psz_type,
        s_object.c_str(), L"HTTP/1.1", m_psz_referer_, psz_accept_types,
        w_srv_port == INTERNET_DEFAULT_HTTPS_PORT ? WINHTTP_FLAG_SECURE : 0);
    if (!h_request) {
        MessageBoxA(0, "h_request", 0, 0);
    }

    DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
    WinHttpSetOption(h_request, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));

    // Custom Header: Content-Type
    BOOL b_http_result = WinHttpAddRequestHeaders(h_request,
        LWHTTP_CONT_TYPE, -1, WINHTTP_ADDREQ_FLAG_ADD);
    if (!b_http_result) { 
        MessageBoxA(0, "b_http_result", 0, 0);
        goto CleanUp; 
    }

    b_http_result = WinHttpSendRequest((HINTERNET)h_request,(LPCWSTR)WINHTTP_NO_ADDITIONAL_HEADERS, (DWORD)0, (LPVOID)p_data, (DWORD)dw_data_len, (DWORD)dw_data_len, (DWORD_PTR)NULL);
    if (!b_http_result)  {
        MessageBoxA(0, "b_http_result2", 0, 0);
        goto CleanUp;
    }

    b_http_result = WinHttpReceiveResponse((HINTERNET)h_request, (LPVOID)NULL);
    if (!b_http_result) {
        MessageBoxA(0, "b_http_result3", 0, 0);
        goto CleanUp;
    }

    if ((m_dw_last_reply_size_ = read_req_reply(h_request, bt_reply)))
        b_result = true;

CleanUp:
    if (h_request)
        LI_FN(WinHttpCloseHandle).get()((HINTERNET)h_request);

    if (h_connect)
        LI_FN(WinHttpCloseHandle).get()((HINTERNET)h_connect);

    return b_result;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Per the [documentation](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpsendrequest): "*Returns TRUE if successful, or FALSE otherwise. For extended error information, call GetLastError.*" So, when `WinHTTPSendRequest()` fails, what is `GetLastError()` returning to explain WHY it failed? – Remy Lebeau May 04 '22 at 19:37
  • GetLastError returns 5023 – Kuwbua May 04 '22 at 19:43
  • Does this apply to your situation? [Error 5023 (ERROR_INVALID_STATE) on WinHttpSendRequest in DLL](https://social.msdn.microsoft.com/Forums/azure/en-US/3528f634-336c-42c0-bd2e-73f0153d1bb5/error-5023-errorinvalidstate-on-winhttpsendrequest-in-dll?forum=vcgeneral) – Remy Lebeau May 04 '22 at 19:53
  • Yes, it is apply to this situation. – Kuwbua May 04 '22 at 20:01
  • OK, and did it help resolve your issue? That person's mistake was they were calling WinHTTP from within `DllMain()`, which is not allowed. Are you doing the same thing in your code? Where are you calling ` c_lw_http::send_request()` from, exactly? – Remy Lebeau May 04 '22 at 20:04
  • I didn't fix the issue yet, I'm probably calling it from DllMain by other function. Should i make a new thread or is there other fix? – Kuwbua May 04 '22 at 20:15
  • Simply don't call it until `DllMain()` exits. Export a function to call it, and then have the EXE call that function after the DLL is loaded. Whether or not you need to create a thread depends on what you are trying to accomplish to begin with – Remy Lebeau May 04 '22 at 20:27
  • thanks it worked, sorry for responding slow – Kuwbua May 06 '22 at 14:50

0 Answers0