DllImport does not work as advertised in Mono (Linux, C #)

I became acquainted with the development of Mono in Linux, in the children's steps. I am trying to name the Linux C libraries. This page , theoretically, tells me how, but when I type the code below in MonoDevelop 2.2.2 (Fedora 13), I get "Parse error (CS8025)" in "private static extern int getpid ( ); ". In addition, the help system does not 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!"); } } } 
+4
source share
2 answers

Function definitions cannot be displayed in a namespace area in C #. This includes DLL import definitions. To fix this, simply move the function definition inside the type.

 class MainClass { [DllImport("libc.so")] private static extern int getpid(); ... } 
+14
source

If you just need to access some common * nix system calls, check out the Mono.Unix namespace, which provides wrappers around many functions.

http://www.go-mono.com/docs/index.aspx?link=N%3aMono.Unix

+2
source

All Articles