|
|
(9 intermediate revisions by one other user not shown) |
Line 1: |
Line 1: |
| {{PageAuthor|claesabrandt}} | | {{PageAuthor|claesabrandt}} |
| == Introduction ==
| |
|
| |
|
| This page shows how to call methods in a managed .NET DLL from NSIS using the System::Call function.
| | I have replaced this article with my NSIS plug-in that can call methods in a .NET DLL. [[Call .NET DLL methods plug-in|It is located here.]] The source code for the plugin is a better explanation of how to do this. |
| | |
| If you want to call methods in a C# DLL from NSIS, it is required that you write a wrapper around it. This is VERY simple. Managed C++/CLR allows you to create a DLL with an interface that NSIS understands, and you get all the benefits of having access to both the Win32 library and the .NET library. From your C++/CLR DLL, you can then instantiate any C# class and call it's methods or you can call static methods without instantiating any classes.
| |
| | |
| One thing you should know is, when you call a .NET DLL from NSIS, your installer will be dependent upon the .NET framework. The NSIS documentation contains information on how to detect the presence of the framework and how to download and install it if nessecary. Also, I needed to ensure that the Visual C++ redistributable package was installed, when I tested it on a system with no .NET framework on.
| |
| | |
| == Setting up your C++/CLR 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 wizard will have created a sample C++ class in a sample namespace. You do not need this and you can delete the h file, empty the cpp file and then you are ready to write your DLL code. | |
| | |
| == The cpp file ==
| |
| | |
| Write a function for each C# method you want to call. The functions should look like this:
| |
| | |
| <highlight-nsis>
| |
| extern "C" __declspec(dllexport) char* HelloWorld()
| |
| {
| |
| // get a string from your C# method (in this case it is a static method)
| |
| System::String ^str = MyNamespace::MyClass::HelloWorld();
| |
| | |
| // 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;
| |
| }
| |
| </highlight-nsis>
| |
| | |
| And now your wrapper is done! To call the function from NSIS:
| |
| | |
| <highlight-nsis>
| |
| SetOutPath $PLUGINSDIR
| |
| System::Call 'DLLName::HelloWorld(v) t .r0'
| |
| </highlight-nsis>
| |
| | |
| This will return the string in register $0. You can have parameters too. For more information, see the [http://nsis.sourceforge.net/System_plug-in_readme#Calling_Functions System::Call] documentation.
| |
| | |
| 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.
| |
| | |
| NSIS forum [http://forums.winamp.com/showthread.php?s=&threadid=295881 thread]
| |
|
| |
|
| [[Category:Code Examples]] | | [[Category:Code Examples]] |