2011-10-05 74 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

相关问题