Home > Software engineering > C class DLL export contains the namespace, dynamic invocation is always wrong?
C class DLL export contains the namespace, dynamic invocation is always wrong?
Time:10-12
Today's learning dynamic link library DLL export, found a problem, consult everybody, 1, the DLL project header file FourOperations. H are defined as follows:
The namespace MathFuncs { The class MYCLASSDLL MyMathFuncs { Public: //Returns a + b Double the Add (double a, double b); //Returns a - b Double Subtract (double a, double b); //Returns a * b Double Multiply (double a, double b); //Returns a/b Double Divide (double a, double b);
}; }
# endif
2, CPP file FourOperations DLL project. CPP are defined as follows:
# include "stdafx. H" # include "FourOperations. H" # include & lt; iostream> # include & lt; Stdexcept> using namespace std;
The namespace MathFuncs { Double MyMathFuncs: : Add (double a, double b) { Return a + b; } Double MyMathFuncs: : Subtract (double a, double b) { Return a - b; } Double MyMathFuncs: : Multiply (double a, double b) { Return a * b; } Double MyMathFuncs: : Divide (double a, double b) { If (b==0) { Throw new invalid_argument (" b always be zero!" ); } Return a/b; } }
3, DLL project maclassdll. The module definition file (def is as follows:
LIBRARY "MyClassDll "EXPORTS The Add @ 1 Subtract @ 2 Multiply @ 3 Divide the @ 4
4, the client calls the DLL file USER_DYNAMIC. CPP are defined as follows:
# include "stdafx. H" # include & lt; Windows. H> # include & lt; String> # include & lt; iostream>
using namespace std;
Int _tmain (int arg c, _TCHAR * argv []) { HMODULE HDLL;//HMODULE is module handles, HINSTANCE said instance handle HDLL=LoadLibrary (_T (" MyClassDll. DLL "));//_T make the default character set "unicode" effective //address to determine, whether the returned handle is null, if empty, the release of memory load the DLL occupies If (HDLL==NULL) { FreeLibrary (HDLL); } Typedef int (* AddAddr) (int x, int y);//define a function pointer type (consistent with DLL define parameters) AddAddr Add=(AddAddr) GetProcAddress call (HDLL, "Add");//first to set the function Pointers AddAddr Add; Then through GetProcAdress (handle to the DLL and the calling function) to obtain the address of the function If (Add==NULL) { FreeLibrary (HDLL); }//address to determine, whether the returned handle is null, if null, the handle to release
Int x=Add (1, 2); cout<& lt; X & lt; & lt; endl; FreeLibrary (HDLL); return 0; }
5, the question is: Call the ADD function in 4 files, always is not successful, because of the namespace to? I don't know what to do, beg god for help, thank you! Here is to generate the view of DLL
CodePudding user response:
I don't know how to speak, words can export success is diao. Don't say namespace. 1. Lz have defined the MYCLASSDLL, there is no need to use def file 2. But clearly defines the MYCLASSDLL explicit dynamic invocation 3. It is a class method ignore this pointer 4. FreeLibrary parameters and free the same nullptr without release, assume that as the nullptr, but the program will continue to go down, that I should be returned directly. It is a bug,
General exports c + + method there are two ways: One is encapsulated with C functions for export, suitable for explicit dynamic link Another is to use the compiler extensions keyword export, suitable for the implicit dynamic linking
CodePudding user response:
Add in the DLL is a class member function, how to call it and put it as a normal function call