Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling multiple dll imports with the same method name

I'm importing several unmanaged c++ DLL's into my project however the imported DLLs have the same method name which causes compiler issues. For example;

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);
   }
}

Now what I'd like to do would be rename the method on import. Something like;

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

Is this possible?

like image 407
wonea Avatar asked Oct 05 '11 15:10

wonea


3 Answers

Use the EntryPoint property of DllImport attribute.

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

Now when you call ReturnValidate2 in your C# code, you will effectively call ReturnValidate on myseconddll.dll.

like image 179
m-sharp Avatar answered Sep 27 '22 16:09

m-sharp


You could provide any name for your imported function, you should only specify in DllImport the name of the function in it, using EntryPoint property. So you code could look like:

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

[DllImport("myseconddll.dll", EntryPoint="ReturnValidate")]  
public static extern bool ReturnValidate2(long bignum);  
like image 29
Eugene Avatar answered Sep 27 '22 16:09

Eugene


Use the EntryPoint parameter:

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

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

Documentation:
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

like image 23
Polynomial Avatar answered Sep 27 '22 17:09

Polynomial