none
Calling C# DLL from C++ Using LoadLibrary RRS feed

  • Question

  • I know this question has been asked but I can't seem to find a clear cut answer to what the solution is. Any input would be appreciated.

    I am making a 'compatibility dll'. What it's purpose is: It will be loaded by numerous third party applications and perform specialized intermediary functions. I do not have any control, access over, or influence on the application loading my dll.

    What I do know is: The DLL will be loaded using LoadLibrary, GetProcAddress, and FreeLibrary.

    I'm mainly a C# programmer. I was wondering if I could write a C# DLL that could be called using LoadLibrary. I'm slowly seeing that it may not be able to. I don't have a problem doing work in VC++ but I don't know if the managed dll can be called using loadlibrary.

    I read everywhere that I can use COM but since I can not touch the host loading the DLL I don't think that's possible.

    Does anyone have any ideas on how to accomplish this?
    Also does anyone know how to accomplish this via C#?

    Anything that can support my endeavor is appreciated. If anything -- thanks for reading this far

     

    BlnDef

     

    Monday, April 19, 2010 6:35 PM

Answers

  • The clear cut answer is : It can't be done in C#.

    The not so clear cut answer is : write a wrapper in C/C++ that exports the function and calls your C# code in the background. from C/C++ you could just call your code using com interop.

    Monday, April 19, 2010 7:38 PM

All replies

  • The clear cut answer is : It can't be done in C#.

    The not so clear cut answer is : write a wrapper in C/C++ that exports the function and calls your C# code in the background. from C/C++ you could just call your code using com interop.

    Monday, April 19, 2010 7:38 PM
  • Ok, I had thought this was the case. I just wanted to be sure.

    Now a quick followup:

    I have gotten a library to load via loadlibrary that is a C++/Managed C++ Hybrid. I say it's hybrid since it exports the function calls that the 3rd party application is using and then uses VC++ in the background to do the processing work. Can I attach a C# DLL as a reference, create an object defined within that referenced DLL, and use it in VC++?

    I know this seems ugly but I have never had good experiences with COM, and I'm trying to see if there's a native to dotnet way I can go about things.

     

    Thanks for your time,
    BlnDef

    Monday, April 19, 2010 9:51 PM
  • so for injection C++ has it's advantages for the payload to be injected, it can spin up automatically .NET can not

    .NET can inject into a target just fine

    you call .NET from c++

     

     

    so I use C# to inject a C++ payload this unpacks and loads C#

     

    /////////code like

        //Retrieve a pointer to the ICorRuntimeHost interface
        HRESULT hr = CorBindToRuntimeEx(
                                                 NULL,    //Retrieve latest version by default
                                                 L"wks",    //Request a WorkStation build of the CLR
                                                 STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC,
                                                 CLSID_CorRuntimeHost,
                                                 IID_ICorRuntimeHost,
                                                 (void**)&spRuntimeHost
                                                 );

        if (FAILED(hr)) return hr;

        //Start the CLR
        hr = spRuntimeHost->Start();

        CComPtr<IUnknown> pUnk;

        //Retrieve the IUnknown default AppDomain
        hr = spRuntimeHost->GetDefaultDomain(&pUnk);

    or

        HDOMAINENUM hEnum = NULL;
        spRuntimeHost->EnumDomains(&hEnum);

    ....

    ---code end

     

    see more injector code/projects at www.DigitalBodyGuard.com

     

     

    I will make a solid project for injection later this month

     

    I'm currently hitting a wall injecting .NET v4.0, I need to jump AppDomains, If you can jump AppDomains LMK,THK

    Sunday, August 15, 2010 5:04 PM