在Mono中使用依赖于kernel32.dll的外部函数。

问题描述 投票:0回答:1

我正在GNULinux上移植一个C#应用程序(.NET Framework 4.6.1)。我已经使用了 单声道 来构建和运行它。该应用程序运行正常,除了某些部分。也就是说,当需要导入一些DLL时(user32.dll, kernel32.dll),因为很明显,这些都是Windows特有的,Mono不包含它们。例如,在代码中,我有以下内容 外部 指的功能 kernel32.dll :

[DllImport("kernel32.dll")]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess,
  bool bInheritHandle, uint dwThreadId);

[DllImport("kernel32.dll")]
static extern uint SuspendThread(IntPtr hThread);

[DllImport("kernel32.dll")]
static extern int ResumeThread(IntPtr hThread);

[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(ProcessAccess dwDesiredAccess,
  bool bInheritHandle, int dwProcessId);

[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(IntPtr hProcess,
  UIntPtr lpBaseAddress, byte[] lpBuffer, IntPtr dwSize, ref int lpNumberOfBytesRead);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddress,
  byte[] lpBuffer, IntPtr dwSize, ref int lpNumberOfBytesWritten);

[DllImport("kernel32.dll")]
static extern IntPtr VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress,
  out MemoryBasicInformation lpBuffer, IntPtr dwLength);

当使用Mono调用其中一个方法时,我收到了这样的错误(例子为 CloseHandle) :

[ERROR] FATAL UNHANDLED EXCEPTION:
System.EntryPointNotFoundException: CloseHandle assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) MyProject.Utilities.Kernel32NativeMethods.CloseHandle(intptr)
  at MyProject.Utilities.Kernel32NativeMethods.CloseProcess (System.IntPtr processHandle) [0x00001] in <38a03fe220d245f3b3d5e7486135e053>:0 
  at MyProject.Utilities.WindowsProcessRamIO.Dispose (System.Boolean disposing) [0x0003f] in <38a03fe220d245f3b3d5e7486135e053>:0 
  at MyProject.Utilities.WindowsProcessRamIO.Finalize () [0x00002] in <38a03fe220d245f3b3d5e7486135e053>:0

我理解这个错误(在Mono下不可能调用本地方法),但我想知道是否有办法转换这些方法,以避免依赖外部DLLs,如 kernel32.dll.

我查了一下 PInvoke但不幸的是,这些方法没有非托管的替代方法。

有什么办法可以重写这段代码,使其与Mono兼容?或者包含一些东西来重现丢失的DLLs的行为?

c# .net dll mono
1个回答
0
投票

kernel32.dll 是专门针对Windows的,在其他平台上不可用。 这些API只限于Windows。

你需要重写你的代码以避免使用这些函数。 参见类 System.Threading.ThreadDiagnostics.Process也许他们有你需要的一切。

© www.soinside.com 2019 - 2024. All rights reserved.