简体   繁体   中英

How do I call a Win32 Function in PowerShell 1.0 using P/Invoke?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM