如何使用P / Invoke在PowerShell 1.0中调用Win32函数?
时间:2020-03-06 14:48:49 来源:igfitidea点击:
在许多情况下,从PowerShell脚本调用Win32函数或者某些其他DLL会很有用。给定以下功能签名:
bool MyFunction( char* buffer, int* bufferSize )
我听说有一些东西使PowerShell CTP 2变得更容易,但是我很好奇如何在PowerShell 1.0中最好地做到这一点。需要调用的函数正在使用指针这一事实可能会影响解决方案(但我并不是很清楚)。
因此,问题是编写可以调用上述导出的Win32函数的PowerShell脚本的最佳方法是什么?
记住PowerShell 1.0。
解决方案
PowerShell 1.0中没有任何机制可以直接调用Win32 API。我们当然可以编写一个Cor VB.NET帮助程序类来为我们执行此操作,然后从PowerShell中调用它。
更新:看看-
http://blogs.msdn.com/powershell/archive/2006/04/25/583236.aspx
http://www.leeholmes.com/blog/ManagingINIFilesWithPowerShell.aspx
要从Powershell调用非托管代码,请使用Lee Holmes创建的Invoke-Win32函数。我们可以在此处找到源。在这里,我们可以看到有关如何调用具有指针的函数的示例,但更简单的用法是:
PS C:\> Invoke-Win32 "msvcrt.dll" ([Int32]) "puts" ([String]) "Test" Test 0
对于CTP2,请检查以下链接:
http://blogs.msdn.com/powershell/archive/2008/06/03/show-powershell-hide-powershell.aspx
http://thepowershellguy.com/blogs/posh/archive/2008/08/04/powershell.aspx