Cll Unity3d中的DllImport静态C ++库

时间:2016-09-09 09:54:43

标签: c++ unity3d plugins static-libraries dllimport

对于功能的简单签名,只需:

如何将其作为

    [DllImport("__Internal")]
public static extern MyFunc(int a);

用C#替换C ++中的指针和引用?

1 个答案:

答案 0 :(得分:0)

您需要使用Marshaling

示例:

/* unmanaged code declarations */

 struct UnmanagedStruct {
    int n;
 };

 void PassByValue (struct UnmanagedStruct s);

 void PassByReferenceIn (struct UnmanagedStruct *s);
 void PassByReferenceOut (struct UnmanagedStruct *s);
 void PassByReferenceInOut (struct UnmanagedStruct *s);

 struct UnmanagedStruct ReturnByValue ();
 struct UnmanagedStruct* ReturnByReference ();

 void DoubleIndirection (struct UnmanagedStruct **s);

类包装器可以是:

/* note: sequential layout */
 [StructLayout (LayoutKind.Sequential)]
 class ClassWrapper {
    public int n;

    /* cannot wrap function PassByValue */

    /* PassByReferenceIn */
    [DllImport ("mylib")]
    public static extern
       void PassByReferenceIn (ClassWrapper s);

    /* PassByReferenceOut */
    [DllImport ("mylib")]
    public static extern
       void PassByReferenceOut ([Out] ClassWrapper s);

    /* PassByReferenceInOut */
    [DllImport ("mylib")]
    public static extern
       void PassByReferenceInOut ([In, Out] ClassWrapper s);

    /* cannot wrap function ReturnByValue */

    /* ReturnByReference */
    [DllImport ("mylib")]
    public static extern ClassWrapper ReturnByReference ();
       /* note: this causes returned pointer to be freed
          by runtime */
     /* DoubleIndirection */
    [DllImport ("mylib")]
    public static extern
       void DoubeIndirection (ref ClassWrapper s);
 }

以下是一些有用的链接:

  1. Interop with Native Libraries
  2. Marshaling with C#
  3. Default Marshaling for Objects