[Source] .NET Dll in Native process

Results 1 to 3 of 3
  1. #1
    Apprentice kristians521 is offline
    MemberRank
    Aug 2011 Join Date
    yes i doLocation
    21Posts

    [Source] .NET Dll in Native process

    Idk if this is correct section for sources code snipets, but here is a source to load .net dll in native process..

    .NET C# dll code
    Code:
    namespace MyNamespace
    {
        public class MyClass
        {
            // This method will be called by native code inside the target process...
            public static int MyMethod(String pwzArgument)
            {
                MessageBox.Show("Hello World");
                return 0;
            }
    
        }
    }
    C++ Code
    Code:
    void Start()
    {
        // Bind to the CLR runtime..
        ICLRRuntimeHost *pClrHost = NULL;
        HRESULT hr = CorBindToRuntimeEx(
            NULL, L"wks", 0, CLSID_CLRRuntimeHost,
            IID_ICLRRuntimeHost, (PVOID*)&pClrHost);
    
        // Push the big START button shown above
        hr = pClrHost->Start();
    
        // Okay, the CLR is up and running in this (previously native) process.
        // Now call a method on our managed C# class library.
        DWORD dwRet = 0;
        hr = pClrHost->ExecuteInDefaultAppDomain(
            L"c:\\PathToYourManagedAssembly\\MyManagedAssembly.dll",
            L"MyNamespace.MyClass", L"MyMethod", L"MyParameter", &dwRet);
    
        // Optionally stop the CLR runtime (we could also leave it running)
        hr = pClrHost->Stop();
    
        // Don't forget to clean up.
        pClrHost->Release();
    }
    Orginal credits: codingthewheel.com
    Last edited by kristians521; 01-09-11 at 02:54 PM.


  2. #2
    Valued Member pushedx is offline
    MemberRank
    Oct 2008 Join Date
    100Posts

    Re: [Source] .NET Dll in Native process

    Quote Originally Posted by kristians521 View Post
    Orginal credits: Dont remember were i read this
    Probably from here: How To Inject a Managed .NET Assembly (DLL) Into Another Process. The method is good for .Net 2.0 but you will have to use a more updated approach for .Net 4.0. MSDN has some good samples to follow in the link the compiler spits out as part of a deprecated API call.

  3. #3
    Apprentice kristians521 is offline
    MemberRank
    Aug 2011 Join Date
    yes i doLocation
    21Posts

    Re: [Source] .NET Dll in Native process

    Quote Originally Posted by pushedx View Post
    Probably from here: How To Inject a Managed .NET Assembly (DLL) Into Another Process. The method is good for .Net 2.0 but you will have to use a more updated approach for .Net 4.0. MSDN has some good samples to follow in the link the compiler spits out as part of a deprecated API call.
    thanks ill update credits, i have tested it on .NET 3.5 was working fine..



Advertisement