InteropServices and Dll function entrypoint.

  • Thread starter Thread starter Thomas Johansen
  • Start date Start date
T

Thomas Johansen

Hi


I have an GetObject() function in my "old" unmangaed C++ compiled DLL. I
need to call some function in this dll, so I use DllImport in my C#
application.

This is how I do it: (This code works)

[DllImport("ITSIOU.dll", EntryPoint="?GetObjectW@CIODriver@@SAPAV1@XZ",
CallingConvention=CallingConvention.Cdecl)]
static extern int GetObject();

public ITSIOServiceInterface()
{
// Call GetObject to initialize ITSIOu.DLL.
CIODriver = new IntPtr(GetObject());
// Check for success
if( CIODriver.ToInt32() == 0)
{
// Error. No IO subsystem ready
return;
}
}

The CallingConvention isn't nesecary in my case (the dll is compiled with
Cdecl). I thought default was StdCall, so why does it work without it ?

If I use EntryPoint="GetObjectW" or EntryPoint="GetObject" I get an
EntryPointNotFoundException.

So my question is:

1: Why can't I use a more friendly name like "GetObject" instead of
?GetObjectW@CIODriver@@SAPAV1@XZ. ? Any way of doing this ?
(Tried with "[DllImport("ITSIOU.dll", EntryPoint="GetObject",
CallingConvention=CallingConvention.Cdecl)]". No luck)

2: When using DllImport on other function, ex. CreateFile from kernel32.dll,
I can use "CreateFile" without using an "unfriendly" name as entry point.
Why is there no problem on other DLL's ? (And others I have tried ?)

Thomass
 
Thomas,
The CallingConvention isn't nesecary in my case (the dll is compiled with
Cdecl). I thought default was StdCall, so why does it work without it ?

There's no difference between the two when you don't have any
parameters.

1: Why can't I use a more friendly name like "GetObject" instead of
?GetObjectW@CIODriver@@SAPAV1@XZ. ? Any way of doing this ?
(Tried with "[DllImport("ITSIOU.dll", EntryPoint="GetObject",
CallingConvention=CallingConvention.Cdecl)]". No luck)

Because of the way the DLL was compiled and the function exported.
There's nothing you can do about that from the maanged side, you'd
have to rebuild the DLL if you want to change the name.

2: When using DllImport on other function, ex. CreateFile from kernel32.dll,
I can use "CreateFile" without using an "unfriendly" name as entry point.
Why is there no problem on other DLL's ? (And others I have tried ?)

The A and W suffix for Win32 APIs are such a common naming pattern
that the runtime has built in support for finding such functions even
if you don't explicitly provide the full name.



Mattias
 
Back
Top