0

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?

norbjd
  • 10,166
  • 4
  • 45
  • 80

1 Answers1

0

kernel32.dll is specific for Windows and not available on other platforms. These APIs are restricted for Windows only.

You need to rewrite your code to avoid using these functions. See classes System.Threading.Thread and Diagnostics.Process, perhaps they have all you need.

Nick
  • 4,787
  • 2
  • 18
  • 24
  • Thanks for your answer. But because this is `extern` functions I can't find how these functions are implemented because there is no source for `kernel32.dll`. Do you have any idea? (I've already tried https://www.pinvoke.net). – norbjd Apr 24 '20 at 13:32
  • Look them up on the documentation. They are very well documented. – Nick Apr 24 '20 at 18:46
  • I've checked the doc for `OpenProcess` for example (https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess), but I can't find how to do this in pure C#. I've defined it as : `return System.Diagnostics.Process.GetProcessById(dwProcessId).Handle;` but I'm pretty sure it's incorrect, or else I don't know how to use the other parameters (`dwDesiredAccess` and `bInheritHandle`). – norbjd Apr 25 '20 at 14:06
  • Well, you may need things that are simply not available on other platforms. You need to give more details what exactly you need. – Nick Apr 25 '20 at 17:15
  • Thanks, I have posted another question with a minimal complete example of what I'm trying to achieve : https://stackoverflow.com/questions/61438447/rewrite-code-using-extern-kernel32-dll-functions-in-pure-c-sharp-to-work-with. – norbjd Apr 26 '20 at 09:25