Calling a function from .Net DLL - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Calling a function from .Net DLL

Hi,

I want to do the following (VB.NET) from a CA maybe?

Dim strErrorMsg As String = ""
Dim theobsfuscator = New BTS.Gen.Encrypt.Obfuscator

strPlainTextPassword = GetPasswordFromMSIInputField()
Dim strEncryptedPassword As String = theobsfuscator.Encrypt(strPlainTextPassword, strErrorMsg)

If strErrorMsg <> "" Then
' an error happened
End If

I have alreasy made the encryptoion DLL. How do I use AI CA to accept the input and then store teh output in a property?

Regards,
John
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Calling a function from .Net DLL

Hello John,

In order to achieve this you can use the MsiGetProperty and the MsiSetProperty functions from within the DLL.

Also, if you need to get or set a property value from a VB Script custom action, then you can use Session.Property property. Please keep in mind that only immediate custom actions have direct access to public properties. The "Deferred", "Rollback" or "Commit" custom actions will not have access to installer properties (the properties will be empty when the custom action runs). If you want to use properties in a "Deferred" custom action you need to use the Action Data field.

Let us know if this helped, otherwise please give us more details about your scenario.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

Hi Daniel,

The .NET assembly is alredy written and available it is used to encrypt strings.

What I curently have is a property attached to a simple edit box let's say it is QSM_PASSWORD.
I can get and set the values of this easily using a VBScript CA using Session("QSM_PASSWORD") = strValue
or strValue = Session("QSM_PASSWORD"). Ehat I want to do is the following: -

Dim strInput
Dim strOutput
Dim objOBF
Dim strErrorMsg

' Get the value of plain string
strInput = Session("QSM_PASSWORD")

' Create object.
Set objOBF = CreateObject("BTS.Gen.Encrypt.Obfuscator")

' Convert this string to the encrypted version using the assembly function
strOutput = objOBF.Encrypt(strInput, strErrorMsg)
MessageBox "Output String = " & strOutput

It says it cannot find file? I have made the .NET assembly COM visible in VS.NET.
I have then installed using regasm.exe: -
regasm.exe <assembly name> /tlb:<tlb name>

Is there another way of doing this? i.e. calling the function in the assembly from VBScript.

Thanks,
John
jjones
Posts: 68
Joined: Wed Jun 29, 2011 3:30 pm

Re: Calling a function from .Net DLL

You need to download the WiX Toolset v3.6. All functions need to be in this format (I use C# so just convert the same syntax to VB.NET):

Code: Select all

[CustomAction]
public static ActionResult MyCustomAction(Session session) {
    // Function logic here
    return ActionResult.Success;
}
Once you compile your source it will generate two dll's. You need the one that ends in .CA.dll. When you create your custom action in AI, use this file as the one you are calling from and then in the "Function" block, SCROLL DOWN TO THE BOTTOM using the slider and the functions appear at the very bottom of the combo box (Yes Advanced Installer - this is irritating). Once you do this, you're golden!

I am doing the encrypt / decrypt of SQL database information so I know this works.
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Calling a function from .Net DLL

Hello John,

In order to achieve what you want you could use a C# custom action instead of VBS custom action. Please take a look on our "Create fully fledged C# custom actions" article.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

I have installed WiX Toolset 3.6. When I try to open a new project Wix does not appear in the list is there something I have missed?
The Toolset has installed and is in the Add/Remove Programs list.
Regards,
John
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

Ignore previous post re-installed and now it is there. Bad day... :roll:

John
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

Hi Daniel,
I tried what you suggested and was able to select the function from (the bottom) of teh dropdown no problem.
When I rebuild AI etc. and ran the installer it failed: -
MSI (s) (8C:AC) [18:09:42:565]: Doing action: EncryptQSMPassword.CA.dll
Action start 18:09:42: EncryptQSMPassword.CA.dll.
MSI (s) (8C:F0) [18:09:42:565]: Invoking remote custom action. DLL: C:\Program Files\MIDDLEWARE\QSM\EncryptQSMPassword.CA.dll, Entrypoint: EncryptQSMPassword
Action ended 18:09:42: EncryptQSMPassword.CA.dll. Return value 3.
MSI (s) (8C:AC) [18:09:42:565]: Machine policy value 'DisableRollback' is 0
MSI (s) (8C:AC) [18:09:42:565]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\Rollback\Scripts 3: 2
MSI (s) (8C:AC) [18:09:42:565]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\Rollback\Scripts 3: 2
MSI (s) (8C:AC) [18:09:42:565]: No System Restore sequence number for this installation.
MSI (s) (8C:AC) [18:09:42:565]: Unlocking Server
Action ended 18:09:42: INSTALL. Return value 3.
MSI (s) (8C:AC) [18:09:42:580]: MainEngineThread is returning 1603
MSI (s) (8C:00) [18:09:42:580]: RESTART MANAGER: Session closed.
MSI (s) (8C:00) [18:09:42:580]: No System Restore sequence number for this installation.
MSI (s) (8C:00) [18:09:42:580]: User policy value 'DisableRollback' is 0
MSI (s) (8C:00) [18:09:42:580]: Machine policy value 'DisableRollback' is 0
MSI (s) (8C:00) [18:09:42:580]: Incrementing counter to disable shutdown. Counter after increment: 0
MSI (s) (8C:00) [18:09:42:580]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\Rollback\Scripts 3: 2
MSI (s) (8C:00) [18:09:42:580]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\Rollback\Scripts 3: 2
MSI (s) (8C:00) [18:09:42:580]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied. Counter after decrement: -1
MSI (s) (8C:00) [18:09:42:580]: Restoring environment variables
MSI (s) (8C:00) [18:09:42:580]: Destroying RemoteAPI object.
MSI (s) (8C:D0) [18:09:42:580]: Custom Action Manager thread ending.
MSI (c) (9C:60) [18:09:42:596]: Back from server. Return value: 1603
MSI (c) (9C:60) [18:09:42:596]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied. Counter after decrement: -1
The class I am using is below and I have tested the obfuscator (from a dll that is referenced) functionality through a console project, which works fine. Am I not allowed to use referenced DLL for this sort of action in the installer? Will I need the functionality stand-alone in the Custom action?

Public Class CustomActions

<CustomAction()> _
Public Shared Function EncryptQSMPassword(ByVal session As Session) As ActionResult
Dim strPlaintextPassword As String = ""
Dim strErrorMsg As String = ""
Dim strEncryptedPassword As String = ""
Dim theobsfuscator As BTS.Gen.Encrypt.Obfuscator

' Create instance of Obfuscator Class.
theobsfuscator = New BTS.Gen.Encrypt.Obfuscator

session.Log("Begin EncryptQSMPassword")
' Getting a property
strPlaintextPassword = session("QSM_PASSWORD")

' Encrypt the password
strEncryptedPassword = theobsfuscator.Encrypt(strPlaintextPassword, strErrorMsg)

' Write the details out to error properties in AI.
If strErrorMsg <> "" Then
' Put out the message to a property.
session("CUSTOM_ERROR_TEXT") = strErrorMsg
' Set the custom error value to failure.
session("CUSTOM_ERROR_VALUE") = CStr(ActionResult.Failure)
Return ActionResult.Failure ' This returns 1603 to the installer
End If

' setting a property
session("QSM_PASSWORD") = strEncryptedPassword

' Tidy up Obfuscator Class.
theobsfuscator = Nothing

Return ActionResult.Success
End Function

End Class
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Calling a function from .Net DLL

Hello John,

I'm not sure why you are encountering this behavior. Can you set the related custom action as "Deferred with no impersonation", from "Custom Action Properties" right pane, and see if the behavior still persist?

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

Hi Daniel,

Tried it BUT I cannot get and set installer properties using a deferred CA! This is shown below. I am actually using a variety of OS's Windows Server 2008 R2 is the one I am using for these tests. Does this mean then that if I use a deferred CA and call Session("CustomActionData") this will work or will I just get an exception as below? Also this means I will have to write another CA to get the custom action data and set my property from there. This sounds incredibly complex for setting a string value! Any idea what I should try next :?:
Regards,
John
Calling custom action EncryptQSMPassword!EncryptQSMPassword.EncryptQSMPassword.CustomActions.EncryptQSMPassword
Begin EncryptQSMPassword
Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Deployment.WindowsInstaller.InstallerException: Cannot access session details from a non-immediate custom action at Microsoft.Deployment.WindowsInstaller.Session.ValidateSessionAccess()
at Microsoft.Deployment.WindowsInstaller.Session.get_Item(String property)
at EncryptQSMPassword.EncryptQSMPassword.CustomActions.EncryptQSMPassword(Session session)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle, String entryPoint, IntPtr remotingDelegatePtr)
Action ended 10:08:01: InstallExecute. Return value 3.
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

Hi Daniel,
Further to this I tried the following: -

strInVar = session("CustomActionData")

This was ok so I can send property value in to CA. However this does not seem to be allowed: -

session("CustomActionData") = strOutVar

This is in a deferred CA as you instructed above. Deferred CA's are not allowed to change installer properties but I thought the idea was so that they could manipulate data through CustomActionData. This seems not to be the case.

Regards,
John
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Calling a function from .Net DLL

Hello John,

I apologize because I wasn't aware that you need to set a property value from within your custom action code. Indeed, you can retrieve an installer property value from within a deferred custom action only through CustomActionData. Also, I'm afraid you cannot set an installer property from within a deferred custom action, so you should set it as immediate.

Can you launch the related CA.dll using a "Launch attached file" custom action and enable the "Run as administrator" option from "Install Parameters" page, and then see if the behavior still persist?

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
jusher
Posts: 195
Joined: Thu Dec 22, 2011 12:00 pm

Re: Calling a function from .Net DLL

Hi Daniel,

That was it thanks, got there in the end. I usually use vbscript as attached files maybe I should have realise I could have used the dll in this way. Well done and thanks again.
I had in fact built with a deferred dll and used teh properties via CustomActionData then wrote values to a file in ; delimitered format for reading by a further VBSCRIPT CA. Just about to finish it off when I read your post :|

Regards,
John
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Calling a function from .Net DLL

You're welcome. I'm glad you got this working.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”