2016-09-09 55 views
0

對於功能一切都只是簡單的簽名:的DllImport靜態C++庫在C#Unity3d

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

哪有它的功能

static App* Create(const AppOptions& options, JNIEnv* jni_env, 
       jobject activity); 

什麼C#取代的C指針和引用++ ?

回答

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