Click here to Skip to main content
11,932,461 members (53,314 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

Dynamically loading a DLL - MC++

, 27 May 2002
Rate this:
Please Sign up or sign in to vote.
Shows how you can load an assembly at run time, instantiate a class in that assembly and call methods on the class


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]


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
        //This simply prefixes a Hello
        //to whatever string is passed
        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)
    //First we load our assembly
    //using the display name
    Assembly *a = Assembly::Load("Abc");

    //Now we get the type of the object
    //we want to instantiate. Since our class is
    //part of the assembly we have to use the full
    //name of the class with assembly name
    Type *t = a->GetType("Abc.Class1");

    //The MethodInfo class is used to
    //describe a method. It holds metadata about
    //a particular method. We get the MethodInfo for
    //our Hello method    
    MethodInfo *mi = t->GetMethod("Hello");    
    //Just showing off some public properties
    Console::WriteLine("Return type of *{0}* method is *{1}*",

    //We create an instance of our object.
    //Here the default constructor is called.
    Object *o = Activator::CreateInstance(t);

    //Prepare our argument list
    String *args[] = new String*[1]; 
    args[0]= S"Nish";  
    //We use Invoke to call the Hello method
    //on the object we got from CreateInstance above
    //We pass to it our arguments as an Object array
    String *s1=static_cast<String*>(mi->Invoke(o,args));
    return 0;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

QuestionHow to load mixed managed dll Pin
sandi_ro3-Oct-07 6:42
membersandi_ro3-Oct-07 6:42 
GeneralInterface and casting issue Pin
Y_R4-Aug-07 5:30
memberY_R4-Aug-07 5:30 
GeneralUsing OpenGL and GNU in Visual C++.NET (2002) Pin
Sctt H. Chang21-Aug-03 6:21
memberSctt H. Chang21-Aug-03 6:21 
GeneralRe: Using OpenGL and GNU in Visual C++.NET (2002) Pin
Ffelagund1-Mar-04 11:30
memberFfelagund1-Mar-04 11:30 
QuestionHow to Pin
vinodgs5-Aug-03 11:31
membervinodgs5-Aug-03 11:31 
AnswerRe: How to Pin
Nishant S5-Aug-03 15:45
editorNishant S5-Aug-03 15:45 
GeneralRe: How to Pin
vinodgs5-Aug-03 17:35
membervinodgs5-Aug-03 17:35 
GeneralMethodInfo null pointer (help please) Pin
brow083327-Jun-03 7:30
memberbrow083327-Jun-03 7:30 
GeneralFound it! Pin
brow083327-Jun-03 12:31
memberbrow083327-Jun-03 12:31 
QuestionRe: Found it! Pin
Anonymous14-Oct-05 4:19
sussAnonymous14-Oct-05 4:19 
Questionand you cann't unload the dll??? Pin
fftongzhi11-Sep-02 0:37
memberfftongzhi11-Sep-02 0:37 
AnswerRe: and you cann't unload the dll??? Pin
fftongzhi15-Sep-02 19:40
memberfftongzhi15-Sep-02 19:40 
GeneralGood Article Pin
jakesher29-May-02 14:00
memberjakesher29-May-02 14:00 
GeneralRe: Good Article Pin
Nish - Native CPian29-May-02 16:18
memberNish - Native CPian29-May-02 16:18 
GeneralRe: Good Article Pin
Madhu C9-Jun-02 17:28
memberMadhu C9-Jun-02 17:28 
GeneralRe: Good Article Pin
Nish - Native CPian9-Jun-02 18:24
memberNish - Native CPian9-Jun-02 18:24 
Generalregular dll Pin
Kannan Kalyanaraman28-May-02 21:47
memberKannan Kalyanaraman28-May-02 21:47 
GeneralRe: regular dll Pin
Nish - Native CPian29-May-02 16:19
memberNish - Native CPian29-May-02 16:19 
GeneralRe: regular dll Pin
Taranis3-Jun-02 10:37
memberTaranis3-Jun-02 10:37 
GeneralRe: regular dll Pin
.dan.g.16-May-05 18:19
member.dan.g.16-May-05 18:19 
GeneralNext Target -100 Articles Pin
Amita Butch28-May-02 4:38
memberAmita Butch28-May-02 4:38 
GeneralRe: Next Target -100 Articles Pin
Nish - Native CPian28-May-02 4:44
memberNish - Native CPian28-May-02 4:44 
GeneralThis completed your half century Pin
Rama Krishna28-May-02 3:06
memberRama Krishna28-May-02 3:06 
GeneralRe: This completed your half century Pin
Nish - Native CPian28-May-02 4:41
memberNish - Native CPian28-May-02 4:41 
GeneralRe: This completed your half century Pin
Shog928-May-02 12:25
memberShog928-May-02 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 28 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid