c++ Cli WebClient不能在dll中工作

C++ Cli WebClient not working in a dll

本文关键字:dll 工作 不能 Cli WebClient c++      更新时间:2023-10-16

我正在尝试使用c++/Cli中的WebClient从web地址下载字符串。但是,在将dll注入本机进程后,它将失败。

这是我的代码:

#include <Windows.h>
using namespace System;
using namespace System::Net;
using namespace System::Windows;
using namespace System::Windows::Forms;
BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL,_In_ DWORD     fdwReason,_In_ LPVOID    lpvReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH) {
        System::Windows::Forms::MessageBox::Show("test");
        WebClient web;
        String^ Data = web.DownloadString("http://www.google.com");
        System::Windows::Forms::MessageBox::Show(Data);
    }
    return TRUE;
}

在Data字符串初始化之前,它会正常工作,但由于某种原因,执行会停止。弹出窗口:显示("测试");虽然有用,但之后就没有了。是什么原因造成的呢?

原来你只需要链接你的托管/本地混合dll对winsock库(ws2_32.lib) &为下载创建一个单独的线程