从dll调用函数时,c ++程序挂起

时间:2013-06-15 08:09:02

标签: c++ curl hang

有一个简单的程序来执行http请求和接收响应。该计划按预期运作:

#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;
}

启动程序后,我在控制台中获取了Google主页的代码。

我将程序重建为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;
}

屏幕显示“第1点”,然后程序永远挂起。

使用以下代码加载库:

#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;
}

项目exe文件运行dll http://rghost.ru/46766786(对于vc2010)只需按下按钮“скачать” 项目dll http://rghost.ru/46766821(适用于vc2010)

如何让程序不挂起?

0 个答案:

没有答案