views:

1420

answers:

3

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

bool MyFunction( char* buffer, int* bufferSize )

I hear there is something that makes this easier in PowerShell CTP 2, but I'm curious how this is best done in PowerShell 1.0. The fact that the function needing to be called is using pointers could affect the solution (yet I don't really know).

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

Remember for PowerShell 1.0.

+1  A: 

There isn't any mechanism in PowerShell 1.0 to directly call Win32 API's. You could of course write a C# or VB.NET helper class to do this for you and call that 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

Kev
+3  A: 

To call unmanaged code from Powershell, use the Invoke-Win32 function created by Lee Holmes. You can find the source here. There you can see an example of how to call a function that has pointers, but a more trivial usage would be:

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