• 您的位置:首頁 > 新聞動態(tài) > Unity3D

    UNITY3D C#調(diào)用C封裝的函數(shù)庫方法

    2023/10/30??????點擊:

    如果在C函數(shù)庫test.dll中存在函數(shù)int test1(int a, int b), 那么C#對動態(tài)庫函數(shù)的引用可以這樣聲明,之后可以調(diào)用test1函數(shù).

    [DllImport("test", EntryPoint = "test1", CallingConvention = CallingConvention.Cdecl)]
    public static extern int test1(int a, int b);
    那么如果函數(shù)是指針參數(shù)怎么辦? 

    若dll中存在函數(shù)int test2(int *a, int *b), 那么C#對動態(tài)庫函數(shù)的引用可以這樣聲明,之后可以調(diào)用test2函數(shù).

    [DllImport("test", EntryPoint = "test1", CallingConvention = CallingConvention.Cdecl)]
    public static extern int test1(ref int a,ref int b);
    如遇到函數(shù)的參數(shù)為結(jié)構(gòu)體又該如何呢? 
    struct mydatapack
    {
        string name;
        string sex;
        int age;
    }
    int test3(struct mydatapack data);

    我們可以這樣聲明:

    [StructLayout(LayoutKind.Sequential)] 
    public struct datapaclk 
    { 
        public string name; 
        public string sex;
        public string age; 
        public datapack(string Name,string Sex, int age) 
        { 
            this.name= Name; 
            this.sex= Sex;
            this.age = Age; 
        } 
    } 
    [DllImport("test", EntryPoint = "test3", CallingConvention = CallingConvention.Cdecl)] 
    public static extern int test3(datapack data);