Home > Software engineering >  About the DLL call
About the DLL call

Time:10-08

I am a novice, just learning c + +, is studying the DLL call, the function is the whole understand, has been thinking about how to call DLL class method, also didn't find the specific implementation, which tall person can take time to demonstrate the code, thank you,
Just think of a way to myself, is in the DLL containing the class definition and a function return a pointer to the class instance, and then call this function to get a pointer to a class instance, but when generating project, prompt an error occurred when calling class method, information is as follows:
2> ExeProj. Obj: error LNK2019: unresolved external symbol "public: void __thiscall ClassA: : M (void)" (? M @ ClassA @ @ QAEXXZ) referenced in the function _main
2> C: \ users \ \ administrator \ documents \ visual studio 2015 \ Projects \ TestSln \ Debug \ ExeProj exe: fatal error LNK1120:1 unresolved externals

DLL class header file is as follows:
# pragma once
The class ClassA
{
Public:
Void M ();
};

CPP in the DLL file is as follows:
# include & lt; Iostream>
# include "ClassA. H"

using namespace std;
Void ClassA: : M ()
{
Cout & lt; <"I am invoked.";
}

Extern "C" _declspec (dllexport) ClassA * CreatClassA ()
{
Return new ClassA;
}

The caller of the CPP file is as follows:
# include & lt; Windows. H>
# include & lt; Iostream>
# include "../TestSln ClassA. H "

using namespace std;


Int main ()
{
Auto hmod=LoadLibrary (" DllProj. DLL ");
If (hmod==NULL)
{
Cout & lt; <"Load the DLL failed.";
FreeLibrary (hmod);
return 0;
}

Auto hf=GetProcAddress call (hmod, "CreatClassA");
If (hf==NULL)
{
Cout & lt; <"Get funtion failed.";
FreeLibrary (hmod);
return 0;
}

Using pf=ClassA * (*) ();
Auto pClass=reinterpret_cast & lt; Pf> (hf) (a);
PClass - & gt; M ();
FreeLibrary (hmod);
The delete pClass;

return 0;
}

Please people!!!!!!!!!!

  • Related