如何使用P/Invoke在PowerShell 1.0中调用Win32函数? [英] How do I call a Win32 Function in PowerShell 1.0 using P/Invoke?

查看:102
本文介绍了如何使用P/Invoke在PowerShell 1.0中调用Win32函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多情况下,从PowerShell脚本调用Win32函数或其他DLL很有用.给定以下功能签名:

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 )

我听说在PowerShell CTP 2中有一些方法可以简化此操作,但是我很好奇在PowerShell 1.0中如何做到最好.需要调用 的函数是使用指针 的事实可能会影响解决方案(但我并不十分了解).

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).

所以问题是编写能够调用上述导出的Win32函数的PowerShell脚本的最佳方法是什么?

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?

记住PowerShell 1.0.

推荐答案

要从Powershell调用非托管代码,请使用Lee Holmes创建的Invoke-Win32函数.您可以在此处找到源.在那里,您可以看到一个有关如何调用具有指针的函数的示例,但更简单的用法是:

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

这篇关于如何使用P/Invoke在PowerShell 1.0中调用Win32函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆