Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1949
  • Last Modified:

Dynamically load a C# DLL into C++ DLL

Hi,

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.

Thanks.
0
nivo_Z
Asked:
nivo_Z
  • 2
  • 2
2 Solutions
 
jkrCommented:
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 http://msdn.microsoft.com/en-us/library/aa645738(VS.71).aspx ("COM Interop Part 2: C# Server Tutorial"). The idea would be to do that like
   HRESULT hr;
 
   if(bUseSupport09)
           hr = CoCreateInstance(CLSID_Support09,
               NULL, CLSCTX_INPROC_SERVER,
               IID_IManagedInterface, reinterpret_cast<void**>(&cpi));
    else
           hr = CoCreateInstance(CLSID_Support10,
               NULL, CLSCTX_INPROC_SERVER,
               IID_IManagedInterface, reinterpret_cast<void**>(&cpi));

Open in new window

0
 
nivo_ZAuthor Commented:
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.

Thanks.
0
 
Knut HunstadCommented:
You probably want to use the Assembly class, then. That's the C# equivalent of LoadLibrary.

See http://www.java2s.com/Tutorial/CSharp/0240__Assembly/0080__Assembly-Load.htm for what seems to be a good description about this.

Good luck!
0
 
nivo_ZAuthor Commented:
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??
0
 
Knut HunstadCommented:
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;
    try
    {
      a = Assembly.Load("YourLibraryName");
    }
    catch(FileNotFoundException e)
    {Console.WriteLine(e.Message);}
  
    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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now