DllImport doesn't work as advertised in Mono (Linux, C#)
I'm getting to know Mono development in Linux, in baby steps. I'm trying to call Linux C libraries. This page, in theory, tells me how, but when I type the code below in MonoDevelop 2.2.2 (Fedora 13), I get a "Parsing Error (CS8025)" in "private static extern int getpid();". Moreover, the help system doesn't work.
using System;
using System.Runtime.InteropServices;
[DllImport("libc.so")]
private static extern int getpid();
namespace LinuxCaller
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
开发者_StackOverflow }
}
}
Function definitions cannot appear in the namespace scope in C#. This includes DLL import definitions. To fix this just move the function definition inside a type.
class MainClass {
[DllImport("libc.so")]
private static extern int getpid();
...
}
If you just need to access some common *nix system calls, check out the Mono.Unix namespace which provides wrappers around a lot of functions.
http://www.go-mono.com/docs/index.aspx?link=N%3aMono.Unix
精彩评论