Question

I have created one static "C" library using VS.

I am using the same library file for another VS console C application its working fine but when I am working with windows forms app it's not working.

Referred so many queries in this forum but didn't get the Help.

Is there any naming conventions to call the static library functions from the Windows forms Managed c++ ?

Getting Errors Like this

error LNK2028: unresolved token (0A000032) "enum STATUS __clrcall xyz(unsigned char)" (?xyz@@$$FYM?AW4STATUS@@E@Z) referenced in function __catch$?button3_Click@Form1@Myapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z$0

But I should use the same static library for both console and windows application.

Was it helpful?

Solution

The linker error message gives a strong hint what it going wrong. Note the __clrcall calling convention for the undefined symbol, it tells you that the compiler thinks that these are "CLR" functions. Managed code, of course they are not, they are __cdecl. There's more, the names are also mangled. Note the "@@$$FYM?AW4STATUS@@E@Z" curses in the name. Which tells you that the compiler thinks they were written in C++ instead of C.

You'll have to explicitly tell the compiler about this, the .h file isn't compatible enough. Which you do like this in your C++/CLI source code file:

#pragma managed(push, off)
extern "C" {
#include "yadayada.h"
}
#pragma managed(pop)

The #pragmas temporarily turn off managed code compilation mode so the compiler will now assume these are unmanaged function declarations. The extern "C" {} wrap around the #include tells the compiler that the .h file contains C declarations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top