小编egk*_*mor的帖子

如何从 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)
Run Code Online (Sandbox Code Playgroud)

但是,当我执行代码的最后一行时,出现以下异常。

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory …
Run Code Online (Sandbox Code Playgroud)

.net c# powershell winapi

3
推荐指数
1
解决办法
8770
查看次数

标签 统计

.net ×1

c# ×1

powershell ×1

winapi ×1