I'm porting a C# application (.NET Framework 4.6.1) on GNU/Linux. I have used Mono to build and run it. The application runs fine, except for some parts. Namely, when some DLL imports are required (user32.dll
, kernel32.dll
), because obviously these are Windows specific and Mono does not include them. For example, in the code, I have the following extern functions referring to 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);
When one of these methods are called using Mono, I received an error like this (example for 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
I understand the error (call to native method impossible under Mono), but I'd like to know if there is a way to convert these methods to avoid depending on external DLLs like kernel32.dll
.
I checked on PInvoke, but unfortunately there is no unmanaged replacements for those methods.
Is there any way to rewrite this code to be compatible with Mono? Or maybe include something to reproduce the behavior of the missing DLLs?