2012-05-07 3 views

답변

2

물론 다른 C 함수와 마찬가지로 DllImport를 사용하십시오. 두 번째 인수에 반환 된 다른 데이터 유형 (당신은 그들이 헤더에 지정된대로 적절한 구조체를 정의 할 수 있습니다) 여러 오버로드를 정의해야

using System; 
using System.Runtime.InteropServices; 

class TestSysctl { 

    [DllImport ("libc")] 
    static extern int sysctlbyname (string name, out int int_val, ref IntPtr length, IntPtr newp, IntPtr newlen); 

    static void Main (string[] args) { 
      int value; 
      IntPtr size = (IntPtr)4; 
      string param = "kern.maxproc"; 
      if (args.Length > 0) 
        param = args [0]; 
      int res = sysctlbyname (param, out value, ref size, IntPtr.Zero, (IntPtr)0); 
      Console.WriteLine ("{0}: {1} {2} (res: {3})", param, value, size, res); 
    } 
} 

참고 : 다음은 샘플입니다.

+0

감사합니다. Mono/Mac interop 디렉토리가 http://pinvoke.net과 비슷한가요? 올바른 형식 마샬링을 알아내는 것은 매우 어렵습니다 ... – ivan

+0

나는 그러한 디렉토리를 모른다. msdn에서 DllImport 및 P/Invoke에 대해 읽을 수 있으며 http://www.mono-project.com/Interop_with_Native_Libraries에서 mono에 대한 특정 페이지가 있습니다. – lupus