natli
natli

Reputation: 3822

Windows kernel32 functions in Mono on Linux

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?

Upvotes: 3

Views: 7855

Answers (3)

FrozenHaxor
FrozenHaxor

Reputation: 99

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

Everything will start working like supposed to.

Upvotes: -3

Deanna
Deanna

Reputation: 24273

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.

Upvotes: 3

sblom
sblom

Reputation: 27343

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());
  }
}

Upvotes: 5

Related Questions