How can I name a Win32 function in PowerShell 1.0 using P / Invoke?

There are many scenarios where it would be useful to call the Win32 function or some other DLL from the PowerShell script. to Define the following function signature:

bool MyFunction( char* buffer, int* bufferSize ) 

I heard that there is something that makes this easier in PowerShell CTP 2, but I'm curious how best to do this in PowerShell 1.0 . The fact that the function to be called uses pointers can influence the decision (I don't know yet).

So the question is, what is the best way to write a PowerShell script that can call an exported Win32 function like the one above?

Remember PowerShell 1.0.

+4
source share
3 answers

To invoke unmanaged code from Powershell, use the Invoke-Win32 function created by Lee Holmes. Here you can find the source. There you can see an example of how to call a function with pointers, but a more trivial use:

 PS C:\> Invoke-Win32 "msvcrt.dll" ([Int32]) "puts" ([String]) "Test" Test 0 
+5
source

There is no mechanism in PowerShell 1.0 to directly call the Win32 API. You could, of course, write a helper class C # or VB.NET to do this for you, and call it from PowerShell.

Update: take a look at -

http://blogs.msdn.com/powershell/archive/2006/04/25/583236.aspx http://www.leeholmes.com/blog/ManagingINIFilesWithPowerShell.aspx

+1
source

Source: https://habr.com/ru/post/1277240/


All Articles