Skip to content
⌘ NSIS Forum Archive

Create Managed CLR DLL to call C#DLL

2 posts

r2du-soft#

Create Managed CLR DLL to call C#DLL

i try to call with CLR to C# DLL with the According to the description in the post below:
Are you a developer who uses NSIS to distribute your application? Are you a Winamp plug-in developer who wants to use NSIS to distribute your plug-in? Have suggestions for other people like you? This is the place.


Originally Posted by claesabrandt View Post
Hi there,

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

Background:

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:

#pragma once // placed at the top of the file
extern "C" __declspec(dllexport) char* SomeFunction()
{
  // 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)
  char* chp = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
  // return the char* to NSIS
  return chp;
} 
That's it 🙂 To call the function from NSIS:
SetOutPath $PLUGINSDIR
System::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


i add the FTPCityToolkitPlug-in to the reference of CLR Project

and use CLR like this:



extern "C" __declspec(dllexport) char* FTPMakeDir(string ServerAddress, string ServerDIR, string FTPUsername, string FTPPassword)
{
System::String ^str = FTPCityToolkit::FTPCityToolkitClass::FTPMakeDir(ServerAddress, ServerDIR, FTPUsername, FTPPassword);
char* chp = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
return chp;
}
the methude FTPMakeDir have 4 parameter,but i display errors in each of try


what is the wrong?