简体   繁体   中英

Cant send any web request in Dll main C++/CLI

i want to send a simple get request to url in c++/cli but i cant get any response ! Im doing this through (WebClient) Or (HttpWebRequest) but Cant get any results! I check the code and i found that the code stucks at DownloadString(in webclient)

by the way i test this exact code in c++/cli console application and works like a charm! but i cant get any result in c++/cli ClassLibrary Project

here is my code:

System::String^ addr = "https://google.com";
System::Net::WebClient^ wc = gcnew System::Net::WebClient();
System::String^ data = wc->DownloadString(addr);
System::IO::File::WriteAllText("Data.txt", data->ToString());

Call the above code through DllMain:

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    {
        
    }
    case DLL_THREAD_ATTACH:
    {
    }
    case DLL_THREAD_DETACH:
    {
    }
    case DLL_PROCESS_DETACH:
    {
    }
    break;
    }
     CallWebClient();
    while (true)
    {

    }

    return TRUE;
}

Here is an example that makes use of global synchronization event.

There are two projects: ClrDll and ClrApp. The DllMain in ClrDll just fires the event. The ClrApp passively waits for this event and when it arrives, starts a new download.

Note:

  • The dllmain.cpp must not contain any managed code!
  • You have to remove /clr option in dllmain.cpp properties (C/C++/General/Common Language RunTime Support)!
  • You probably need to disable usage of precompiled headers in dllmain.cpp properties (C/C++/Precompiled Headers/ Precompiled Header)!

Since you haven't provided enough info, it's possible that your goal could be reached different and simpler way.

This solution makes sense only if you can setup listening for the event in the main application (not in the dll itself).

dllmain.cpp:

#include <windows.h>

HANDLE hEvent = NULL;

void FireEvent()
{
    if (hEvent == NULL)
        hEvent = CreateEventA(NULL, FALSE, FALSE, "ClrDll_DllMain");

    SetEvent(hEvent);
}

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

    FireEvent();

    return TRUE;
}

ClrApp.cpp:

#include "pch.h"
using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Threading;

ref class Watcher
{
private: Thread^ thread;
private: AutoResetEvent^ cancel = gcnew AutoResetEvent(false);
private: EventWaitHandle^ dllmain = gcnew EventWaitHandle(false, EventResetMode::AutoReset, "ClrDll_DllMain");

public: void Start() {

    if (thread == __nullptr) {
        thread = gcnew Thread(gcnew ThreadStart(this, &Watcher::Work));
        thread->Start();
    }
}

public: void Stop() {
    if (thread != __nullptr) {
        cancel->Set();
        thread->Join();
        thread = __nullptr;
    }
}

public: void Work() {
    array<WaitHandle^>^ events = gcnew array<WaitHandle^>(2);
    events[0] = cancel;
    events[1] = dllmain;

    while (true) {
        int index = WaitHandle::WaitAny(events);
        if (index == 0)
            return;

        if (index == 1)
            OnDllMain();
    }
}

protected: void OnDllMain()
{
    String^ addr = "https://google.com";
    WebClient^ wc = gcnew WebClient();
    String^ data = wc->DownloadString(addr);
    File::WriteAllText("Data-" + DateTime::UtcNow.Ticks.ToString() + ".txt", data->ToString());
}

};

int main(array<System::String ^> ^args)
{
    Watcher^ watcher = gcnew Watcher();
    watcher->Start();

    ClrDll::Class1^ o = gcnew ClrDll::Class1();
    Thread::Sleep(5000);

    watcher->Stop();

    return 0;
}

ClrDll.h

#pragma once

using namespace System;

namespace ClrDll
{
    public ref class Class1 {
    public: Class1() {
            System::Diagnostics::Debug::WriteLine("Class1 ctor");
    }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM