call external .dll and access .cs methods with powershell

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
n1mdas
Posts: 12
Joined: Thu Dec 28, 2017 2:05 pm

call external .dll and access .cs methods with powershell

Post by n1mdas » Thu Dec 06, 2018 10:39 am

Hello,

I am creating a portable exe file that i am able to protect and license accordingly with https://www.obsidium.de/show/details/en .
Currently i am looking of ways how to show some licensing details using the C# SDK files.
Could you advise or show me a way of how to best embedded the .dll file along with the .cs file and doing this inside of Powershell? With the demo package the SDK files are shipped and can be reviewed. The idea is to load the .dll file and call the
EntryPoint = "obsGetLicenseStatus"
EntryPoint = "obsGetLicenseExpiration" etc.. within the Powershell GUI to show the license status which is read from an external license.key file.

I tried with the below but seems not possible to run at all:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-6
I found within the Internet the https://activedirectoryfaq.com/2016/01/use-net-code-c-and-dlls-in-powershell/ but would like to check how to proceed or do it in the best possible way?

Thanks,

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: call external .dll and access .cs methods with powershell

Post by jvierra » Thu Dec 06, 2018 1:28 pm

Is the SDK dll a Net assembly. If so just load it an call it.
If it is a classic Win32 style DLL then you ill need to "P/Invoke" the calls from C# and add the generated type to PS.

See: https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Add-Type?view=powershell-5.1

You can probably more easily use C# to create a Net assembly wrapper and load that into PS.

Post Reply