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:
 # # ifndef FOUROPERATIONS_H_ 
# define FOUROPERATIONS_H_
# ifdef MYCLASSDLL
# define MYCLASSDLL _declspec (dllimport)
# the else
# define MYCLASSDLL _declspec (dllexport)
# endif

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

CodePudding user response:

DLL export functions to C compatible

CodePudding user response:

reference 1st floor dustpg response:
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 kind is to use the compiler extensions keyword export, suitable for the implicit dynamic link



Thank you very much! I really is a rookie at c + +, there are many places are not very understand, continue to ask:
1, you mean the macro definition can be directly deleted, need only a def file? Def file (or a more convenient, if you can, I just leave a def file)
2, if I only use def file, use dynamic explicitly call is reasonable?
3, how to call a pointer, the member functions of a class method can effective?

CodePudding user response:

DLL export function name the things
Key words: vc + + DLL export functions
Often use VC6 Dependency check the name of the DLL export functions, can find the name of the DLL export functions are sometimes very different, the cause of different mostly and compile DLL when specified by the qualifier with the DLL export functions,
Vc + + to support two languages: C/C + +, this also is the root cause of the differences in the DLL export functions
We use VS2008 new DLL project, a project called "TestDLL"
The default source file suffix. CPP instead. C (C)
Enter the test code is as follows:
01 int _stdcall MyFunction (int iVariant)
02 {
03 return 0;
04}
In order to export the function above, we have the following several methods:
1. Using the traditional (. The module definition file (def)
A new suffix for. Def text file (here to build a TestDll. Def), the file content is:
The LIBRARY TestDll
EXPORTS
MyFunction
When the Link specified depends on input file:/DEF: "TestDll. DEF
"2. Visual c + + provides convenient way
Join before 01 done int __declspec (dllexport) keyword
Through the above two methods, we can export MyFunction function,
We use the Dependency check the exported functions:
The first method the exported functions as follows:
MyFunction
The second method the exported functions as follows:
_MyFunction @ 4
__stdcall can make the export function to add an underscore the names, followed a @ plus the number of bytes parameters, such as _MyFunction @ 4 parameters (int iVariant) is 4 bytes
__fastcall as __stdcall similar, but not in front of the underline, but a @, such as @ MyFunction @ 4
__cdecl is beginning the function name,
Summary: if you want to export the function in the C file, and don't let the compiler function name, def file is used to derive the function,
Let's take a look at c + + files
We use VS2008 new DLL project, a project called "TestDLL"
The default source file suffix for the CPP (namely c + + files),
Enter the test code is as follows:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related