1

There is a simple program to perform http requests and receive responses. The program works as expected:

#include <stdio.h>
#include <curl/curl.h>
#pragma comment(lib,"curllib.lib")

int main(int argc, char *argv[])
{
    CURL *curl_handle;
    CURLcode res;

    curl_handle = curl_easy_init();
    if(curl_handle)
        {
             curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.google.com");
             res = curl_easy_perform(curl_handle);
             curl_easy_cleanup(curl_handle);
        }
    getchar();
    return 0;
}

After starting the program I get the code for the main page of Google in the console.

I rebuilt the program into a dll.

#include <stdio.h>
#include <curl/curl.h>
#pragma comment(lib,"curllib.lib")

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
        CURL *curl_handle;
        CURLcode res;

        curl_handle = curl_easy_init();
        if(curl_handle)
        {
                 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.google.com");
                 printf("point 1");
                 res = curl_easy_perform(curl_handle); //Program hang
                 printf("point 2");
                 curl_easy_cleanup(curl_handle);
        }
        getchar();

        switch (ul_reason_for_call)
        {
                case DLL_PROCESS_ATTACH:
                break;
                case DLL_PROCESS_DETACH:
                break;
        }
        return TRUE;
}

The screen displays "point 1", then the program hangs forever.

For loading the library using the following code:

#include "stdafx.h"
#include <iostream>
#include <windows.h>

using namespace std;
typedef unsigned __int64 ulong64;

typedef int (*ph_dct_imagehash)(const char* file,ulong64 &hash);

int _tmain(int argc, _TCHAR* argv[])
{
    ph_dct_imagehash _ph_dct_imagehash;
    HINSTANCE hInstLibrary = LoadLibrary(L"dlltest.dll");

   if (hInstLibrary)
   {
      std::cout << "Ok!" <<  std::endl;


      FreeLibrary(hInstLibrary);
   }
   else
   {
      std::cout << "DLL Failed To Load!" <<  std::endl;
      std::cout << GetLastError() <<  std::endl;
   }

   std::cin.get();

   return 0;
}

The project exe file to run dll http://rghost.ru/46766786 (for vc2010) just press button "скачать" The project dll http://rghost.ru/46766821 (for vc2010)

How can I make the program does not hang?

Dmitriy
  • 161
  • 2
  • 12
  • 2
    Wait, why are you making curl calls in DllMain?! You should do only basic initialization there, and everything else in an exported function. – Jonathon Reinhart Jun 15 '13 at 08:14
  • try to move your work to an exported method, it it still hangs then start debugging and look inside the curl_easy_perform implementation to see where the problem is. – ahmedsafan86 Jun 15 '13 at 08:20
  • What should I do if there is no exported functions? – Dmitriy Jun 15 '13 at 08:40
  • exported functions are functions that you define and implement. you make it visible outside the dll by decorating it with `__declspec(dllexport)`. Get a pointer to it from your driver program with `GetProcAddress`. – greatwolf Jun 15 '13 at 08:42
  • I'm doing this library for program that can not be changed. I can not add or change the exported functions – Dmitriy Jun 15 '13 at 08:46
  • @Dmitriy did you ever figure out what was wrong? – Ryan Glenn Oct 17 '22 at 05:27

0 Answers0