Dynamically load a C# DLL into C++ DLL

Posted on 2009-02-24
Last Modified: 2012-05-06

I have 2 C# DLLS, one is called support_10.dll and one is called support_09.dll.
In a C++ DLL, I want to run some code and based on my calcualtions (for example it turns out that I'm on platform 09), load one of the above DLLS,  support_09.dll.
Then I would like to call a method that exists in these DLLS and pass it a string argument.
I want to use the name of the C# DLL for loading, or some other identifier if name is not possible.

Question by:nivo_Z
    LVL 86

    Assisted Solution

    Unfortunately you cannot just use 'LoadLibrary()' for that purpose. If you want to call managed code from unmanaged code, you should consider COM Interop. For your particular setup, see ("COM Interop Part 2: C# Server Tutorial"). The idea would be to do that like
       HRESULT hr;
               hr = CoCreateInstance(CLSID_Support09,
                   NULL, CLSCTX_INPROC_SERVER,
                   IID_IManagedInterface, reinterpret_cast<void**>(&cpi));
               hr = CoCreateInstance(CLSID_Support10,
                   NULL, CLSCTX_INPROC_SERVER,
                   IID_IManagedInterface, reinterpret_cast<void**>(&cpi));

    Open in new window


    Author Comment

    My bad as I wasn't clear enough in my first post.
    The C# DLL is to be loaded dynamically from managed C++ (and not C++) as I wrote in the beginning of the thread.
    I was told I could use reflection and indeed it looks like a solution I can use, but I cant seem to get it to work.

    The C# DLL looks similar to this (the DLL name is support_10.dll):
    namespace MyNameSpace
        public class MyClass
             public String invoke(String MyStr)
                return MyStr;

    In the managed C++ code I would like to dynamically load the support_10.dll and call the public String invoke(String MyStr) method.

    LVL 8

    Expert Comment

    You probably want to use the Assembly class, then. That's the C# equivalent of LoadLibrary.

    See for what seems to be a good description about this.

    Good luck!

    Author Comment

    I'm almost done. I have this code now, but I'm not sure what goes into the Invoke(null, null) at the last line. I need to pass a sting.

        AssemblyName^ myAssemblyName = AssemblyName::GetAssemblyName( "09_Support.dll" );
        Assembly^ al = Assembly::Load(myAssemblyName);
        Type^ t = al->GetType("MyNameSpace.MyClass");
        MethodInfo^ m = t->GetMethod("MyMethod");
        Respose = (String^)m->Invoke(null, null); <------ what should go into here??
    LVL 8

    Accepted Solution

    Doesn't this example from the pages I pointed you to, cover it?

    If it is a static function, the first parameter of invoke should be null.

    The return value of Invoke should match the return value of your method.
     public static int Main(string[] args)
        Assembly a = null;
          a = Assembly.Load("YourLibraryName");
        catch(FileNotFoundException e)
        Type classType = a.GetType("YourLibraryName.ClassName");
        object obj = Activator.CreateInstance(classType);
        MethodInfo mi = classType.GetMethod("MethodName");
        mi.Invoke(obj, null);
        object[] paramArray = new object[2];    
        paramArray[0] = "Fred";
        paramArray[1] = 4;
        mi = classType.GetMethod("MethodName2");
        mi.Invoke(obj, paramArray);
        return 0;

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Flash ( has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now