Prev Previous Post   Next Post Next
Old 13th August 2008, 09:19   #1
claesabrandt's Avatar
Join Date: Aug 2008
Location: Denmark
Posts: 54
Smile Calling managed .NET DLL from NSIS - this works :-)

Hi there,

This is a howto on calling managed .NET DLLs from NSIS. It works and is very easy.


One thing you should know is, that when you call a .NET DLL from NSIS, your installer will be dependent upon that the .NET framework is installed. The NSIS documentation contains information on how to detect the presence of the framework and how to download and install it if nessecary.

If you have a C# DLL, it requires that you write a managed .NET C++/CLR wrapper. You do not need to write a C++ Win32 wrapper, which is not so funny when you want to call your managed C# DLL. In your wrapper you will be able to call your C# code directly.

Setting up your project:

I assume you already have your C# DLL ready with some classes and methods in it you want to call. If you have this in a solution, you can now add a new project to the solution, select C++ CLR, then select class library. This will generate a managed C++/CLR dll and this is the DLL you will be calling from NSIS. Remember to add your C# project as a reference in your C++/CLR project.

The C++/CLR code:

Alright. What you have to do now is to write a C++/CLR funtion for each C# method you want to call. The C++/CLR function should look like this in your cpp or h file:

PHP Code:
#pragma once // placed at the top of the file

extern "C" __declspec(dllexportcharSomeFunction()
// get a string from your C# method (in this case it is a static method)
System::String ^str MyNamespace::MyClass::MyMethod();

// convert string to char* (this is one line)
charchp = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);

// return the char* to NSIS
return chp;

That's it To call the function from NSIS:
PHP Code:
::Call 'DLLName::SomeFunction(v) t .r0' 
This will return the string in register $0. You can have parameters too. See the NSIS documentation for more information on System::Call.

Make sure to include both the C++/CLR dll and the C# dll in your installer and place them in for instance the $PLUGINSDIR. When calling the C++/CLR dll with System::Call, be sure to make a call to SetOutPath $PLUGINSDIR first, so NSIS can find the DLLs.

Hope you can use this.

Regards Claes Brandt

Last edited by claesabrandt; 13th August 2008 at 11:40.
claesabrandt is offline   Reply With Quote
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump