0

I'm trying to get a request from a DLL (which is hooked into a program) but no matter what I try, everything just hangs.

I tried to use CURL and WinHttpClient. Both did the same thing but slightly differently.

CURL made the program hang for a long time and then displayed nothing (that code isn't commented and contains code from other Stack Overflow questions.)

WinHttpClient immediately returns nothing.

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
//#include "WinHttpClient/WinHttpClient.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>

using namespace std; // fix later

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

extern "C" __declspec(dllexport) void Quack();

__declspec(dllexport) void Quack() {
    /*WinHttpClient client(L"http://www.google.com");
    client.SetUserAgent(L"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1;...)");

    client.SendHttpRequest();
    wstring httpResponseHeader = client.GetResponseHeader();
    wstring httpResponseContent = client.GetResponseContent();

    std::string thingstr((const char*)&httpResponseContent[0], sizeof(wchar_t) / sizeof(char)*httpResponseContent.size());

    LPCSTR thing = (LPCSTR)httpResponseContent.c_str();

    MessageBoxA(NULL, thing, "Quack", MB_OK);*/

    CURL *curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

    MessageBoxA(NULL, (LPCSTR)readBuffer.c_str(), "Quack", MB_OK);
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            Quack();
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}

Does anyone have a solution? Any answer or suggestion is appreciated.

Raymonf
  • 114
  • 1
  • 16
  • 1
    `DllMain` is very limited in what it's allowed to do. Your code would likely work, as long as it's not running from within `DllMain`. – Igor Tandetnik Feb 08 '16 at 03:55
  • @IgorTandetnik It appears that `Quack()` is actually being called by DllMain. I threw some messageboxes and that's how it's executed. Do you have any suggestions? – Raymonf Feb 08 '16 at 22:21
  • What do you mean, "it appears"? Is there any doubt? There's a line that reads `Quack();` smack dab in the middle of `DllMain`'s body. My suggestion is, don't call `Quack()` (or anything else of substance) from `DllMain`; I thought I made it clear the first time round. – Igor Tandetnik Feb 08 '16 at 23:21
  • @IgorTandetnik Yes, but then I'm not sure how to call it. I set the import to Quack but still only DllMain is called. – Raymonf Feb 09 '16 at 00:15
  • Whoever it is that loads your DLL, should then call a function exported from that DLL. – Igor Tandetnik Feb 09 '16 at 02:50
  • @IgorTandetnik I should have pointed out that I don't have the source code to the program I'm hooking.. – Raymonf Feb 09 '16 at 05:25
  • I presume you aren't hooking another program for the sole purpose of sending an HTTP request from inside it; you could just as well send the same request from your own program. You are probably hooking in order to catch some kind of events or notifications - so send your requests when those events arrive. – Igor Tandetnik Feb 09 '16 at 05:33

0 Answers0