Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling c dll in C#

Introduction to the problem:

I've got to control a certain device through API provided with a DLL file, LIB file, and c header files whose functions are declared as dllimport.

When I use the API in a C++ project everything worked just fine - I included the headers, lib , dll, and called the functions as declared in the header files.

The problem begins when trying to call those functions from a C#.NET project, using [DllImport] attribute: The functions were declared with the exact name and parameters, and running the code did not throw any exception. And yet the device did not respond at all, like the functions had never been actually called.

How it is declared in the C header:

int __declspec(dllimport) Init_Card_RTx(unsigned short device_num, unsigned short enabled_channels, unsigned short xmt_channels);

How it is declared in C#:

[DllImport(Path, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Init_Card_RTx")]
public static extern int Init_Card_RTx(UInt16 device_num, UInt16 enabled_channels, UInt16 xmt_channels);

The questions:

  • Is that because the functions in the headers are declared dllimport?
  • In that case, do I have to wrap the DLL with C++ functions declared as dllexport?
  • What are the steps required for a C DLL to be accessible from C#?
  • In the C# project, do I have to include the LIB file as well? not just the DLL file itself?
like image 243
Giora Ron Genender Avatar asked Jan 07 '14 18:01

Giora Ron Genender


1 Answers

Is that because the functions in the headers are declared dllimport?

Possibly. The functions need to be exported. Whether they are exported or not depends on how they DLL was compiled by whoever gave it to you. I'd guess that they should be though (or the C++ code would try import them and fail).
The first thing I do when troubleshooting this kind of thing is to load the DLL in Dependency Walker which will show you all the exported functions. If they're not showing up, they're not exported, and C# can't call them. If they are showing up, then you're ok and you don't need to change any of your C code or create any wrapper functions.

In that case, do I have to wrap the DLL with C++ functions declared as dllexport?

No. C# can only call C functions using DllImport. C++ does name mangling when exporting functions, which makes things a mess and generally not workable.

What you need to do is make your functions exported somehow. My guess is that they are already exported, but if not, you could make some wrapper functions like this.

int __declspec(dllexport) Init_Card_RTx(unsigned short device_num, unsigned short enabled_channels, unsigned short xmt_channels) {
    // just call the other function here
}

These should make the function exported and you should see it show up in dependency walker.

What are the steps required for a C DLL to be accessible from C#?

The CLR has to be able to find the dll (usually you just put it in the same directory as the C# exe), and the functions have to be exported as C functions. That's pretty much it.

In the C# project, do I have to include the LIB file as well? not just the DLL file itself?

You don't need to include anything in the C# project at all, just the public static extern wrapper functions with their DllImport attributes on them. So long as the CLR can find the dll at runtime, that's all you need. If it can't find it at runtime, you should get an exception when you call the first imported method.

PS: Get Dependency walker. I can't recommend it more highly for this kind of thing

like image 119
Orion Edwards Avatar answered Sep 24 '22 04:09

Orion Edwards