如何解决如何从Powershell调用Win32 API函数?
我需要使用PowerShell从Win 32 API中的Imagehlp.dll调用MapFileAndCheckSumA函数。我试图在线遵循一些类似的教程,例如: https://devblogs.microsoft.com/scripting/use-powershell-to-interact-with-the-windows-api-part-1/ 我尝试使用Add-Type cmdlet在PowerShell中编译C#代码。我写的代码如下。
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class Imagehlp
{
[DllImport("imagehlp.dll",CharSet=CharSet.Auto)]
public static extern bool MapFileAndCheckSumA(
[MarshalAs(UnmanagedType.LPStr)] string Filename,UIntPtr HeaderSum,UIntPtr CheckSum);
}
"@
$x = [UIntPtr]::new(5)
$y = [UIntPtr]::new(5)
[Imagehlp]::MapFileAndCheckSumA("C:\Program Files\Internet Explorer\iexplore.exe",$x,$y)
但是,当我执行代码的最后一行时,在下面出现了异常。
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Imagehlp.MapFileAndCheckSumA(String Filename,UIntPtr CheckSum)
at CallSite.Target(Closure,CallSite,Type,String,Object,Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site,T0 arg0,T1 arg1,T2 arg2,T3 arg3)
at System.Management.Automation.Interpreter.DynamicInstruction`5.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause,Object dollarUnderbar,Object inputToProcess)
at System.Management.Automation.DlrScriptCommandProcessor.Complete()
at System.Management.Automation.CommandProcessorBase.DoComplete()
at System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(CommandProcessorBase commandRequestingUpstreamCommandsToStop)
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
at System.Management.Automation.Runspaces.PipelineThread.WorkerProc()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,Object state)
at System.Threading.ThreadHelper.ThreadStart()
我在网上搜索了异常,我猜测这是由代码的C#部分引起的编译错误。在线解决方案说,我必须从正在使用的IDE中更改编译设置,但是我对PowerShell和C#还是很陌生,因此,我不知道如何实现此目的。你能帮我吗?
解决方法
这一切都错了:
- 使用
CharSet.Auto
时,该函数也不应同时具有A
或W
前缀,这就是Auto
会为您做的。 - 使用
CharSet.Auto
时,使用UnmanagedType.LPStr
是错误的,因为这总是将字符串编组为ANSI。 -
MapFileAndCheckSum
被记录为返回DWORD
,而不是BOOL
。这一点尤其重要,因为成功的价值是0
,它映射到False
,而1
(“无法打开文件”)则映射到True
。 -
HeaderSum
和CheckSum
是指向DWORD
值的指针,用于输出值,应将其编组为out int
,而不是IntPtr
。
更正后的签名:
[DllImport("imagehlp.dll",CharSet = CharSet.Auto)]
public static extern int MapFileAndCheckSum(string Filename,out int HeaderSum,out int CheckSum);
然后可以在PowerShell中按如下所示调用它:
[int] $headerSum = 0;
[int] $checkSum = 0;
$result = [Imagehlp]::MapFileAndCheckSum(
"C:\Program Files\Internet Explorer\iexplore.exe",[ref] $headerSum,[ref] $checkSum
)
if ($result -ne 0) {
Write-Error "Error: $result"
}
$headerSum,$checkSum
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。