Call .NET DLL methods plug-in: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
 
 
(28 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{PageAuthor|claesabrandt}}
{{PageAuthor|claesabrandt}}
== NSIS plug-in for calling methods in .NET DLLs. ==
== Description ==


This is a NSIS plug-in, that can call methods in your managed .NET DLL.
This is a NSIS plug-in, that can call methods in your managed .NET DLL.


While the plug-in is fully functional, it still undergoes some development and changes.
Place the plugin in the NSIS plugins folder. You call your .NET DLL methods by calling CLR:Call. This expects the following parameters:
* Assembly dll filename
* Namespace and classname with dot in between
* Method name
* Number of parameters
* Parameters (optional)


Version 0.1 of the plugin is available here <attach>CLR.zip</attach> for testing purposes.
At the moment, the supported parameter types are string, char, int, float and boolean. Return value can be of those types too but are returned as strings to NSIS. Before calling the plug-in, call SetOutPath and copy the .NET DLL to be invoked. Note that the .NET class must be marked as public.


What is underway and soon to be included:
== Calling methods ==
* support for callbacks
* a much cleaner way of specifying method parameters
* both native NSIS plugin and plugin called via System::Call
* source code, when it done and has been cleaned up


I previously had a description on how to write your own wrapper that loads .NET DLL methods. As it contained some errors I have replaced it with this ready-made plugin. When the source is released for this plugin, you can use it to write your own custom .NET wrapper, if you need some special features not available through this plug-in.
Sample NSIS script calling a method in a .NET DLL, which takes five parameters: string, char, int, float and boolean and returns a string:
<highlight-nsis>
InitPluginsDir
SetOutPath $PLUGINSDIR
File "SomeAssembly.dll"
 
CLR::Call /NOUNLOAD "SomeAssembly.dll" "SomeNamespace.SomeClass" \
  "SomeMethod" 5 "mystring1" "x" 10 15.8 false
 
pop $0 
MessageBox MB_OK $0
 
...
CLR::Destroy
</highlight-nsis>
 
Notice the /NOUNLOAD. This is nessecary if you need to call CLR::Call more than once, otherwise the installer hangs on the second call. When done with calling CLR::Call, call CLR::Destroy, for instance in .onGUIEnd.


Sample NSIS script calling a method in a .NET DLL:
You do not have to put quotes around the dll filename, namespace, classname and method, but if the dll filename contains spaces, you need the quotes around that. Omitting the quotes only makes the code slightly easier to read, the result is the same.
 
Sample NSIS script calling a .NET method that takes no parameters and returns void:
<highlight-nsis>
<highlight-nsis>
SetOutPath $PLUGINSDIR
CLR::Call /NOUNLOAD SomeAssembly.dll SomeNamespace.SomeClass SomeOtherMethod 0
File "SomeAssembly.dll" ; some test .NET assembly
...
File "CLR.dll" ; this plugin
CLR::Destroy
System::Call `CLR::Call(w 'SomeAssembly.dll::SomeNamespace::SomeClass::SomeMethod( \
</highlight-nsis>
  "some string value",12,"15,8",false)') w .r0`
 
The plug-in is fully functional but still undergoes some development and changes.
 
== Calling properties ==
 
Currently you can call properties by using the get_ and set_ notation. If your property is called MyProperty, you can set it's value with set_MyProperty and you can get it's value with get_MyProperty.
 
Example getting a property value:
<highlight-nsis>
CLR::Call /NOUNLOAD SomeAssembly.dll SomeNamespace.SomeClass get_MyProperty 0
pop $0 
MessageBox MB_OK $0
</highlight-nsis>
 
Example setting a property value:
<highlight-nsis>
CLR::Call /NOUNLOAD SomeAssembly.dll SomeNamespace.SomeClass set_MyProperty 1 "Hello Property"
</highlight-nsis>
</highlight-nsis>


Hello would you mind stating which blog pltfaorm you're working with? I'm planning to start my own blog soon but I'm having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique.                  P.S My apologies for getting off-topic but I had to ask!
== Download ==
Version 0.5 including source is available here <attach>CLR.zip</attach> as-is.
Update: dll with Unicode support. [http://taralex.us/CLR.rar Link]
== Prerequisites ==
The plugin will need the .NET 2.0 SP1 framework to be installed on the target machine. It can be included in the installer or downloaded using for instance the inetc plugin.
== Known issues ==
When the installer finishes, the CLR.dll file remains in the temporary plugins directory on the target machine. The file is not locked, but somehow the plugin cannot be deleted. The next time the user reboots the machine, this file is deleted automatically. This issue is being worked on.
Another issue is that if want to call a .NET dll from your .NET dll, you will find that the installer cannot find the second .NET dll. For the moment the remedy is to wrap your installer in another installer. This other installer should place the .NET dlls in the same directory as the installer will run from. When the installer runs, the .NET dlls can now be found. There will only be distributed one setup file.
== Version history ==
* 0.1 Initial release of the plugin
* 0.2 Changed to native NSIS plugin and some bugfixes
* 0.3 Bugfix: If more than one class was found in an assembly, only the first one could be loaded.
* 0.4 CLR.dll is now compiled in release mode, in order to work on systems without VS2008 installed. Various other minor bugfixes as well.
* 0.5 CLR.dll is now compiled with Visual Studio 2005 so there is no need for Visual C++ 2008 Redist.
* 2009-01-13: Added description of how to call properties.
NSIS forum [http://forums.winamp.com/showthread.php?s=&threadid=295881 thread]
NSIS forum [http://forums.winamp.com/showthread.php?s=&threadid=295881 thread]
[[Category:Plugins]]
[[Category:Plugins]]

Latest revision as of 21:01, 15 May 2013

Author: claesabrandt (talk, contrib)


Description

This is a NSIS plug-in, that can call methods in your managed .NET DLL.

Place the plugin in the NSIS plugins folder. You call your .NET DLL methods by calling CLR:Call. This expects the following parameters:

  • Assembly dll filename
  • Namespace and classname with dot in between
  • Method name
  • Number of parameters
  • Parameters (optional)

At the moment, the supported parameter types are string, char, int, float and boolean. Return value can be of those types too but are returned as strings to NSIS. Before calling the plug-in, call SetOutPath and copy the .NET DLL to be invoked. Note that the .NET class must be marked as public.

Calling methods

Sample NSIS script calling a method in a .NET DLL, which takes five parameters: string, char, int, float and boolean and returns a string:

InitPluginsDir
SetOutPath $PLUGINSDIR
File "SomeAssembly.dll"
 
CLR::Call /NOUNLOAD "SomeAssembly.dll" "SomeNamespace.SomeClass" \
  "SomeMethod" 5 "mystring1" "x" 10 15.8 false
 
pop $0  
MessageBox MB_OK $0
 
...
CLR::Destroy

Notice the /NOUNLOAD. This is nessecary if you need to call CLR::Call more than once, otherwise the installer hangs on the second call. When done with calling CLR::Call, call CLR::Destroy, for instance in .onGUIEnd.

You do not have to put quotes around the dll filename, namespace, classname and method, but if the dll filename contains spaces, you need the quotes around that. Omitting the quotes only makes the code slightly easier to read, the result is the same.

Sample NSIS script calling a .NET method that takes no parameters and returns void:

CLR::Call /NOUNLOAD SomeAssembly.dll SomeNamespace.SomeClass SomeOtherMethod 0
...
CLR::Destroy

The plug-in is fully functional but still undergoes some development and changes.

Calling properties

Currently you can call properties by using the get_ and set_ notation. If your property is called MyProperty, you can set it's value with set_MyProperty and you can get it's value with get_MyProperty.

Example getting a property value:

CLR::Call /NOUNLOAD SomeAssembly.dll SomeNamespace.SomeClass get_MyProperty 0
pop $0  
MessageBox MB_OK $0

Example setting a property value:

CLR::Call /NOUNLOAD SomeAssembly.dll SomeNamespace.SomeClass set_MyProperty 1 "Hello Property"

Hello would you mind stating which blog pltfaorm you're working with? I'm planning to start my own blog soon but I'm having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!

Download

Version 0.5 including source is available here CLR.zip (35 KB) as-is.

Update: dll with Unicode support. Link

Prerequisites

The plugin will need the .NET 2.0 SP1 framework to be installed on the target machine. It can be included in the installer or downloaded using for instance the inetc plugin.

Known issues

When the installer finishes, the CLR.dll file remains in the temporary plugins directory on the target machine. The file is not locked, but somehow the plugin cannot be deleted. The next time the user reboots the machine, this file is deleted automatically. This issue is being worked on.

Another issue is that if want to call a .NET dll from your .NET dll, you will find that the installer cannot find the second .NET dll. For the moment the remedy is to wrap your installer in another installer. This other installer should place the .NET dlls in the same directory as the installer will run from. When the installer runs, the .NET dlls can now be found. There will only be distributed one setup file.

Version history

  • 0.1 Initial release of the plugin
  • 0.2 Changed to native NSIS plugin and some bugfixes
  • 0.3 Bugfix: If more than one class was found in an assembly, only the first one could be loaded.
  • 0.4 CLR.dll is now compiled in release mode, in order to work on systems without VS2008 installed. Various other minor bugfixes as well.
  • 0.5 CLR.dll is now compiled with Visual Studio 2005 so there is no need for Visual C++ 2008 Redist.
  • 2009-01-13: Added description of how to call properties.

NSIS forum thread