Introduction
Today someone was asking how he can load a DLL dynamically with .NET. For
some design related reasons he didn't want to add a reference to the DLL at
compile time. For all I know this might be something everyone knew about except
this particular individual and me. But I didn't know anything about this and I
had never thought that anyone would want to do anything like that. Anyway as I
found out it was really easy. This example is completely in MC++. Just in case
someone starts flaming me saying this is an oft-discussed topic, I can always
claim this is the first MC++ example. And my google searches kept directing me
to pages that talked about normal dynamic loading of DLLs [means the non-.NET
stuff]
The DLL
Create a simple class library called Abc.dll. This will be the DLL which we
will load dynamically.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
namespace Abc
{
public __gc class Class1
{
public:
String* Hello(String* str)
{
return String::Concat(S"Hello ",str);
}
};
}
The Program
Okay, this is the little program that will load the above DLL, and call the
Hello function, passing a string to it and also getting back the string that the
function returns. Remember that Abc.dll must be in the same directory as our
program's executable. I believe there are ways to put the DLL in some special
directories that all .NET programs look into, but I am totally ignorant of such
things.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
int wmain(void)
{
Assembly *a = Assembly::Load("Abc");
Type *t = a->GetType("Abc.Class1");
MethodInfo *mi = t->GetMethod("Hello");
Console::WriteLine("Return type of *{0}* method is *{1}*",
mi->Name,mi->ReturnType);
Object *o = Activator::CreateInstance(t);
String *args[] = new String*[1];
args[0]= S"Nish";
String *s1=static_cast<String*>(mi->Invoke(o,args));
Console::WriteLine(s1);
return 0;
}
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site -
www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
blog.voidnish.com.
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy
Summer Love and Some more Cricket as well as a programming book –
Extending MFC applications with the .NET Framework.
Nish's latest book
C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.