Search code examples
cc++-clicalling-convention

Use a C struct in C++/CLI, calling conventions issue


I'm using lib_usb in my C++/CLI project, and I need to use it's functions usb_get_busses and some others in my managed C++/CLI code.

Using a managed AutoPtr to create the usb_bus struct as follows:

AutoPtr<struct usb_bus> bus;

now I need to call the usb_get_busses function, however the linker seems to be unhappy as to the calling conventions. The CLR uses the clrcall calling convention, and obviously C using __cdecl.

usb_bus* usb_get_busses(void) <--- the signature of the C function.

Now how do I make myself able to use the lib_usb in C++/CLI? Do I have to create a C++/CLI wrapper or something?

The linker spits out this message:

error LNK2031: unable to generate p/invoke for "extern "C" struct usb_bus * __clrcall usb_get_busses(void)" (?usb_get_busses@@$$J0YMPAUusb_bus@@XZ); calling convention missing in metadata


Solution

  • That's not specified with __cdecl at all. Look at the linker error- it clearly states __clrcall. Try explicitly specifying it with __cdecl.