Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++/CLI
Article

Dynamically loading a DLL - MC++

Rate me:
Please Sign up or sign in to vote.
4.84/5 (36 votes)
27 May 20021 min read 217.6K   26   31
Shows how you can load an assembly at run time, instantiate a class in that assembly and call methods on the class

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.

MC++
#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

namespace Abc
{
    public __gc class Class1
    {
    public:
        //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.

MC++
#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}*",
        mi->Name,mi->ReturnType);

    //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));
    Console::WriteLine(s1);
    return 0;
}

License

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


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
QuestionHow to load mixed managed dll Pin
sandi_ro3-Oct-07 5:42
sandi_ro3-Oct-07 5:42 
GeneralInterface and casting issue Pin
Y_R4-Aug-07 4:30
Y_R4-Aug-07 4:30 
GeneralUsing OpenGL and GNU in Visual C++.NET (2002) Pin
Sctt H. Chang21-Aug-03 5:21
Sctt H. Chang21-Aug-03 5:21 
GeneralRe: Using OpenGL and GNU in Visual C++.NET (2002) Pin
Ffelagund1-Mar-04 10:30
Ffelagund1-Mar-04 10:30 
QuestionHow to Pin
vinodgs5-Aug-03 10:31
vinodgs5-Aug-03 10:31 
AnswerRe: How to Pin
Nish Nishant5-Aug-03 14:45
sitebuilderNish Nishant5-Aug-03 14:45 
GeneralRe: How to Pin
vinodgs5-Aug-03 16:35
vinodgs5-Aug-03 16:35 
GeneralMethodInfo null pointer (help please) Pin
brow083327-Jun-03 6:30
brow083327-Jun-03 6:30 
GeneralFound it! Pin
brow083327-Jun-03 11:31
brow083327-Jun-03 11:31 
QuestionRe: Found it! Pin
Anonymous14-Oct-05 3:19
Anonymous14-Oct-05 3:19 
Questionand you cann't unload the dll??? Pin
fftongzhi10-Sep-02 23:37
fftongzhi10-Sep-02 23:37 
AnswerRe: and you cann't unload the dll??? Pin
fftongzhi15-Sep-02 18:40
fftongzhi15-Sep-02 18:40 
GeneralGood Article Pin
jakesher29-May-02 13:00
jakesher29-May-02 13:00 
GeneralRe: Good Article Pin
Nish Nishant29-May-02 15:18
sitebuilderNish Nishant29-May-02 15:18 
GeneralRe: Good Article Pin
Madhu C9-Jun-02 16:28
Madhu C9-Jun-02 16:28 
GeneralRe: Good Article Pin
Nish Nishant9-Jun-02 17:24
sitebuilderNish Nishant9-Jun-02 17:24 
Generalregular dll Pin
Kannan Kalyanaraman28-May-02 20:47
Kannan Kalyanaraman28-May-02 20:47 
GeneralRe: regular dll Pin
Nish Nishant29-May-02 15:19
sitebuilderNish Nishant29-May-02 15:19 
GeneralRe: regular dll Pin
3-Jun-02 9:37
suss3-Jun-02 9:37 
GeneralRe: regular dll Pin
.dan.g.16-May-05 17:19
professional.dan.g.16-May-05 17:19 
GeneralNext Target -100 Articles Pin
28-May-02 3:38
suss28-May-02 3:38 
GeneralRe: Next Target -100 Articles Pin
Nish Nishant28-May-02 3:44
sitebuilderNish Nishant28-May-02 3:44 
GeneralThis completed your half century Pin
Rama Krishna Vavilala28-May-02 2:06
Rama Krishna Vavilala28-May-02 2:06 
GeneralRe: This completed your half century Pin
Nish Nishant28-May-02 3:41
sitebuilderNish Nishant28-May-02 3:41 
GeneralRe: This completed your half century Pin
Shog928-May-02 11:25
sitebuilderShog928-May-02 11: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.