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.
This topic is 5 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
n1mdas
Posts: 22
Last visit: Sat Dec 05, 2020 2:51 pm

call external .dll and access .cs methods with powershell

Post by n1mdas »

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,
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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.
This topic is 5 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked