Windows kernel32 functions in Mono on Linux

丶灬走出姿态 提交于 2021-02-09 09:02:31

问题


I got this awesomely simple ini class that I downloaded from somewhere a while ago, but now that I'm using mono I'm running into the issue that it's importing stuff from kernel32

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
    string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
            string key, string def, StringBuilder retVal,
    int size, string filePath);

Which on mono (in linux) gives the error DLLNotFoundException: kernel32

Is there any way to get this to work with mono? Maybe embed the whole thing into the assembly at compile time (if that even makes sense at all, I wouldn't know). Or will I have to create/find an ini class that doesn't use WinAPI? (Nini springs to mind).

I'd really like it if WinAPI stuff could work with Mono, any thoughts?


回答1:


You'll need to rewrite the functionality of those functions in native .NET to use them on Mono/Linux (unless you can convince Mono and Wine to play nicely).

If the INI files are controlled, then you may get away with simple file/string manipulation, but then you may be better off moving to something a bit more cross platform anyway.




回答2:


Mono supports C#'s P/Invoke, which is what's required for running Win32 API functions. (As long as you're running Mono on Windows--the fact that it can't find "kernel32" causes me to suspect you're not.)

The site pinvoke.net collects the necessary DllImport signatures for most of the Win32 API.

Here's what it has to say about GetPrivateProfileString.

This code worked for me using Mono 2.10.8 on Windows 7:

using System;
using System.Text;

public class MainClass
{
  [DllImport("kernel32")]
  private static extern long WritePrivateProfileString(string section,
    string key, string val, string filePath);
  [DllImport("kernel32")]
  private static extern int GetPrivateProfileString(string section,
    string key, string def, StringBuilder retVal,
    int size, string filePath);

  static void Main()
  {
    StringBuilder asdf = new StringBuilder();
    GetPrivateProfileString("global","test","",asdf,100,@"c:\example\test.ini");
    Console.WriteLine(asdf.ToString());
  }
}



回答3:


Change [DllImport("kernel32")] into [DllImport("kernel32.dll")]

Everything will start working like supposed to.



来源:https://stackoverflow.com/questions/11602781/windows-kernel32-functions-in-mono-on-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!