Http://www.cnblogs.com/luxiaoxun/p/3405374.html
1. Export C functions for projects in C or C + +
If you are using a DLL written in C, and you want to export a function to a module in C or C + +, you should use the __cplusplus preprocessor macro to determine which language is being compiled. If you are using from a C + + language module, declare these functions with a C link. If you use this technique and provide a header file for your DLL, these functions can be used unchanged by C and C + + modules.
The following code shows the header files that can be used by C and C + + client applications:
Mycfuncs.h#ifdef __cplusplusextern "C" { //only need to export C interface if //used by C + + source Code#endif __declspec (dllimport) void mycfunc (); __declspec (dllimport) void Anothercfunc (); #ifdef __cplusplus} #endif
Mycfunc () and Anothercfunc () are the exported functions of the C language DLL.
If you need to link a C function to a C + + executable, and the function declaration header file does not use the technique above, add the following in the C + + source file to prevent the compiler from decorating the C function name:
extern "C" {#include "MyCHeader.h"}
The code tells the compiler that "MyCHeader.h" is written in C and does not decorate the name of the C function in the header file, otherwise it will not be found when connected.
2. Export C + + functions for use in C language projects
If you have a function in a DLL written in C + + that you want to access from a C language module, you should declare these functions using C links instead of C + + links. Unless otherwise specified, the C + + compiler uses C + + type-safe naming conventions (also known as name decorations) and C + + calling conventions (which are difficult to invoke from C using this calling convention).
To specify a C link, specify extern "C" for the function declaration in the DLL. For example:
extern "C" __declspec (dllexport) int MyFunc (long parm1);
C-language functions are not directly called C + + code, if you want to call, you can do a wrapper, such as Call_lib_cppfunction, its declaration and implementation as follows:
Wrapper Functionextern "C" void Call_lib_cppfunction (lib* p, dataattribute* dataattribute) { P->dafun ( Dataattribute);} Dafun is the implementation of our C + + code void Lib::d afun (dataattribute* dataattribute) { map<string, mmsinfo>::iterator it; // ...}
Calling each other between C and C + +