代码之家  ›  专栏  ›  技术社区  ›  JCCyC

DllImport不能像Mono(Linux,C#)中宣传的那样工作

  •  5
  • JCCyC  · 技术社区  · 16 年前

    我正在逐步了解Linux中的Mono开发。我正在尝试调用LinuxC库。 This page 理论上讲,它告诉了我怎么做,但是当我在monodevelope2.2.2(fedora13)中键入下面的代码时,我在“private static extern int getpid();”中得到一个“解析错误(CS8025)”。而且,帮助系统不起作用。

    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!");
            }
        }
    }
    
    2 回复  |  直到 13 年前
        1
  •  15
  •   JaredPar    16 年前

    函数定义不能出现在C#的命名空间范围中。这包括DLL导入定义。要解决这个问题,只需将函数定义移到类型中。

    class MainClass {
      [DllImport("libc.so")]
      private static extern int getpid();
    
      ...
    }
    
        2
  •  2
  •   jpobst    16 年前

    如果您只需要访问一些常见的*nix系统调用,请查看Mono.Unix系统命名空间,它为许多函数提供包装器。

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