2011-10-05 66 views
6

我正在將多個非託管C++ DLL導入到我的項目中,但導入的DLL具有相同的方法名稱,這會導致編譯器問題。例如;使用相同方法名稱調用多個dll導入

unsafe class Myclass 
{ 
    [DllImport("myfirstdll.dll")] 
    public static extern bool ReturnValidate(long* bignum); 

    [DllImport("myseconddll.dll")] 
    public static extern bool ReturnValidate(long* bignum); 

    public Myclass 
    { 
     int anum = 123; 
     long passednum = &anum; 
     ReturnValidate(passsednum); 
    } 
} 

現在我想要做的就是重命名導入方法。就像是;

[DllImport("myseconddll.dll")] 
public static extern bool ReturnValidate(long bignum) AS bool ReturnValidate2(long bignum); 

這可能嗎?

回答

7

你可以爲你的輸入函數提供任何名稱,你應該只在DllImport指定的函數的名稱它使用EntryPoint屬性。所以你的代碼可能看起來像:

[DllImport("myfirstdll.dll", EntryPoint="ReturnValidate")] 
public static extern bool ReturnValidate1(long bignum); 

[DllImport("myseconddll.dll", EntryPoint="ReturnValidate")] 
public static extern bool ReturnValidate2(long bignum); 
12

使用DllImport屬性的EntryPoint屬性。

[DllImport("myseconddll.dll", EntryPoint = "ReturnValidate")] 
public static extern bool ReturnValidate2(long bignum); 

現在,當你在你的C#代碼中調用ReturnValidate2,你將有效調用ReturnValidate上myseconddll.dll。

2

使用EntryPoint參數:

[DllImport("myfirstdll.dll", EntryPoint="ReturnValidate")] 
public static extern bool ReturnValidate1(long bignum); 

[DllImport("myseconddll.dll", EntryPoint="ReturnValidate")] 
public static extern bool ReturnValidate2(long bignum); 

文檔:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.entrypoint.aspx

相關問題