2017-02-23 136 views
10

靜態方法說你有下面的C++代碼:訪問C++從C#

extern "C" { 
    void testA(int a, float b) { 
    } 

    static void testB(int a, float b){ 
    } 
} 

我想用DllImport訪問這在我的C#項目:

class PlatformInvokeTest 
{ 
    [DllImport("test.so")] 
    public static extern void testA(int a, float b); 
    [DllImport("test.so")] 
    internal static extern void testB(int a, float b); 

    public static void Main() 
    { 
     testA(0, 1.0f); 
     testB(0, 1.0f); 
    } 
} 

這適用於testA完全正常,但testB無法投擲EntryPointNotFoundException。

我可以從我的C#代碼訪問testB嗎?怎麼樣?

+6

函數聲明*靜態*在全球範圍內有沒有外部鏈接,所以不能導出你將需要刪除static關鍵字。你將不得不刪除靜態。你可能會混淆它與聲明一個類成員函數,聲明它靜態做了一些非常不同的事情。 –

回答

8

static在C++中的含義與在C#中的含義不同。在命名空間範圍內,static給出了一個名稱內部鏈接,這意味着它只能在包含該定義的翻譯單元中訪問。沒有靜電,它具有外部鏈接,並且可以在任何翻譯單元中訪問。

當你想使用DllImport