Is dllexport needed in the source C dll to be able to be used in c# with [DllImport]?

176 views Asked by At

I am trying to write a wrapper functions around C.dll ( pbmman32.dll, if someone is familiar with it ) to use with c#.

Looking at the header files of the source, it doesn't seem that they implement __declspec(dllexport) although they do use extern "C"{}. Can i still call functions within that dll with the dllexport line missing?

For example:

BOOL PBM_Version (TCHAR* Buffer, WORD* Version, DWORD Size)

into:

[DllImport("pbmman32.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
public static extern bool PBM_Version(StringBuilder Buffer, StringBuilder Version, System.UInt32 Size);
2

There are 2 answers

0
David Heffernan On

The vendor might have supplied a header file for you to use when importing rather than the version they used to build the DLL. More likely is that they exported the functions with a .def file. But there's no compulsion to use __declspec(dllexport). Stop worrying about its absence.

I am sceptical of your translation though. The second parameter is probably an unsigned 2 byte type, passed as an out param. Declare it like this:

out ushort version

Decode the major version number with version >> 8 and the minor with version & 0xff.

It's not enough to read the header file alone. You must read the documentation too.

The other area which gives cause for concern is the use of TCHAR. Does the library really offer ANSI and Unicode exports?

2
Mike Dinescu On

The __declspec(dllexport) is a convenient way to mark functions as exportable but it's not the only way (for instance they might have been exported using a .def file.

You should be able to query a DLL to get all the exported functions (you could use something like http://www.dependencywalker.com/). If the one you want to call is exported then you're good to go.