Page 1 of 1

Calling C# DLL from script

Posted: Wed Mar 30, 2016 5:31 pm
by damjan
Hi guys

After some research I have managed to create a C# assembly with one class that has one method. This method can be called directly from an InstallAware script. This method allows you to use unicode characters for all parameters and also allows you also to return values through the parameters back to the InstallAware script. In case any of you guys are interested in this I added all the things you need in the sample that is attached below.

callingCSharp.zip
(6.77 MiB) Downloaded 648 times


There are two folders in this sample. Each for its own project.

The CSharpDLL folder is the C# solution which has only one class and only one method. This method is called TestMethod. It is exported with an Attribute like the C++ native methods. For this to work the project includes the NUget UnmanagedExports package which is referenced in the project references. Here is what the method looks like:

Code: Select all

[DllExport("TestMethod", CallingConvention = CallingConvention.StdCall)]
public static int TestMethod(string parameter1, string parameter2, IntPtr returnParameter1, IntPtr returnParameter2)

The first two parameters are only passed to the TestMethod, while the last two parameters also return a value back to the InstallAware script. The method also returns an integer.


The IASetup is the InstallAware project that calls the CSharpDLL.dll and its TestMethod. There is only one line that executes the call. The variables that are defined before are used to demonstrate that the method is capable of displaying regular and uni-code characters. The setup requires that the CSharpDLL.dll file is located in the same folder as the .mpr project file. Here is what the Call DLL command looks like:
CallToCSharpDLL.jpg
CallToCSharpDLL.jpg (31.09 KiB) Viewed 5925 times


Give it a try to see if it will work for you.

Regards, Damjan

Re: Calling C# DLL from script

Posted: Thu Mar 31, 2016 2:56 am
by bokkie
Thanks Damjan. I'll download it and try it out. I'll let you know if I have any problems compiling it and using it in a test IA project.

Re: Calling C# DLL from script

Posted: Thu Mar 31, 2016 10:31 am
by FrancescoT
I'll try it out and I let you know.

Regards

Re: Calling C# DLL from script

Posted: Thu Sep 15, 2016 10:11 am
by g3ro81
Hi,
thank you. It works, but i can't run the "Call DLL Function" twice.

For example, this code doesn't work; the first call ends correctly, but the second one doesn't and the installation process doesn't procede.

Code: Select all

Call DLL Function $SUPPORTDIR$\CSharpDLL.dll->TestMethod (get result into variable NUMERICRETURN)
Call DLL Function $SUPPORTDIR$\CSharpDLL.dll->TestMethod (get result into variable NUMERICRETURN)


I tried to add a new method, a new class, a new dll, but the result is the same. Also this code doesn't work.

Code: Select all

Call DLL Function $SUPPORTDIR$\CSharpDLL.dll->TestMethod (get result into variable NUMERICRETURN)
Call DLL Function $SUPPORTDIR$\CSharpDLL2.dll->TestMethod2 (get result into variable NUMERICRETURN2)


You can test this bug using your sample:
- run installer
- click NEXT ("call DLL" works)
- click BACK
- click NEXT again ("call DLL" doesn't work)

Do you experience the same bug?

Thank you

Re: Calling C# DLL from script

Posted: Mon Sep 19, 2016 11:30 am
by FrancescoT
It looks that the DLL cannot be called twice or better, the DLL fails to be loaded after its first call.

Regards