0
votes

I made a dll from my project and export a function using extern "C" like the code below:

main.cpp

extern "C" __declspec(dllexport) void __cdecl  VectorOfMarker(char*     InAdd,vector<NewMarker>& VectorMarkers)
 {
    DetectSeg d;
    d.VectorOfMarker(InAdd,VectorMarkers);
 }

I build the project and create the .dll and .lib files successfully. then I create a new visual c++ project and try to use this dll and mentioned function in it. Although I copied the .dll and .lib files to the same directory but I can't use extern "C" to import my function to the 2nd project. I think that I need to change some settings in visual studio 2010 to use the functions Can anyone help me for this? How can I use my exported function?

Too many thanks in advance

1

1 Answers

0
votes

I think you are confused as to what type of the dll you are building.

There are two typed of the dynamitic linking implicit and explicit . To dynamically link a dll implicitly, you create dll that exports some functions and/or variables. This will create a DLL module and .lib import library. The module that is using this type of the dll, must have header file with function prototypes and must be linked with .lib import library. So you are linking at the compile time. Since exports are done using __declspec(dllexport) and __declspec(dlleimport) and exported functions names are decorated (mangled). They look like ?ExportedTest@@YAXPAD@Z.

Another type is explicit linking and that is most likely what you are doing. Usually for this type of DLL function are exported using .def files to produce function names that are not decorated. This also can be achieved by using extern "C" modifier to tell C++ compiler to compile function as C style, hence exported function is not decorated and usre _ (underscore).

To use this type of the DLL you have todeclare function type and parameters, call Load library, and GetProcAddress to get function pointer. Then you will be able to make a call as follows:

typedef void (*DLLVectorOfMarker)(char*, vector<int>&);

HMODULE hMod = LoadLibrary(_T("ExportTest.dll")); // your lib name goes here

DLLVectorOfMarker pfnVectorOfMarker = (DLLVectorOfMarker)GetProcAddress(hMod, "VectorOfMarker");


vector <int> VectorMarkers;

pfnVectorOfMarker("some string", VectorMarkers);