CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2012
    Posts
    4

    Inject .NET assembly into running .NET process

    Hello!

    I have a problem: I have a .NET process in which my unmanaged FreeBasic DLL is loaded by a global windows hook.

    From this DLL, I wanted to inject a VB.NET DLL into the project and call a VB.NET function in the context of the already running .NET runtime.

    I created the following C++ DLL as a "loader":

    Code:
    // HermesLibLoader.cpp : Definiert die exportierten Funktionen für die DLL-Anwendung.
    //
    
    #include "stdafx.h"
    #include "mscoree.h"
    #include "HermesLibLoader.h"
    
    #pragma comment(lib, "mscoree.lib")
    
    HERMESLIBLOADER_API HRESULT fnHermesLibLoader(LPCWSTR szFilename, LPCWSTR szParam)
    {
    		ICLRRuntimeHost *pClrHost = NULL;
    		HRESULT hr = CorBindToRuntimeEx(NULL, L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pClrHost);
    
    		if(!SUCCEEDED(hr)) return hr;
    
    		DWORD dwRet = 0;
    		hr = pClrHost->ExecuteInDefaultAppDomain(szFilename, L"HermesClass", L"Init", szParam, &dwRet);
    
    		pClrHost->Release();
    
    		if(!SUCCEEDED(hr)) return hr;
    
    		if(dwRet != 42) return ERROR_INVALID_DATA;
    
    		return ERROR_SUCCESS;
    }
    ...and the following VB.NET DLL ("HermesLib.dll"):

    Code:
    Public Class HermesClass
        Public Shared Function Init(ByVal Parameter As String) As Integer
            HelloWorld(Parameter)
            Return 42
        End Function
    
        Public Shared Sub HelloWorld(ByVal Parameter As String)
            MsgBox("Hello World from Hermes! (Param = """ & Parameter & """)")
        End Sub
    End Class
    What happens is, I get a HRESULT of 0x80131522 (COR_E_TYPELOAD).

    Can you help me?

    Best regards,
    Cherry

  2. #2
    Join Date
    May 2012
    Posts
    4

    Re: Inject .NET assembly into running .NET process

    Addition (I cannot find the post edit function...?!):

    I am calling fnHermesLibLoader from my FreeBasic DLL like this:

    Code:
    fnHermesLibLoader("c:\full\path\to\HermesLib.dll", "Test Parameter")

  3. #3
    Join Date
    May 2012
    Posts
    4

    Re: Inject .NET assembly into running .NET process

    EDIT2: I also tried "HermesLib.HermesClass" in my ExecuteInDefaultAppDomain call, but I get the same error...

  4. #4
    Join Date
    May 2012
    Posts
    4

    Re: Inject .NET assembly into running .NET process

    Problem resolved: The second attempt ("HermesLib.HermesClass") was actually correct, but I made a mistake and put a comma there instead of a period.

    Thanks anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured