Trydll2.h
// From a DLL simpler. All files within this DLL are compiled with the trydll2_exports
// Symbol defined on the command line. This symbol shocould not be defined on any project
// That uses this DLL. This way any other project whose source files include this file see
// Trydll2_api functions as being imported from a DLL, wheras This dll sees symbols
// Defined with this macro as being exported.
# Ifdef trydll2_exports
# Define trydll2_api _ declspec (dllexport)
# Else
# Define trydll2_api _ declspec (dllimport)
# Endif
// This class is exported from the trydll2.dll
Class trydll2_api ctrydll2 {
Public:
Ctrydll2 (void );
Void diplay ();
Int FNP (int I );
// Todo: add your methods here.
};
Extern trydll2_api int ntrydll2;
Extern "C"
{
Trydll2_api ctrydll2 * getinstance ();
Trydll2_api int fntrydll2 (void );
Trydll2_api int FNP (INT para );
};
// Trydll2_api int fntrydll2 (void); // errors may occur when used in the C ++ file.
// The C ++ compiler modifies the function name based on the unmodified function name, function parameter type, and number of function parameters.
// The class where the function is located (if any) and the file where the function is located.
// When I use the DLL using the hermit method, because the function (set to void FN () in the DLL is not declared as extern "C", it is modified according to the C ++ rule
// Then use the FN () function directly on the client (DLL is the server) to compile the code, but an error occurs during running,
// System prompt: The function entry point cannot be found. Because the FN of the client is also renamed, the modified name is different from the modified name on the server.
// So I guess C ++'s modification to the function name also depends on the file name ..
// I guess the truth is wrong, but remember, don't pin your hopes on Renaming the system to make the name of a function the same.
,,. Trydll2.cpp ..........
# Include "stdafx. H"
# Include "trydll2.h"
//..............................
# Include <iostream>
Using namespace STD;
///...............................
Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Switch (ul_reason_for_call)
{
Case dll_process_attach:
Case dll_thread_attach:
Case dll_thread_detach:
Case dll_process_detach:
Break;
}
Return true;
}
// This is an example of an exported variable
Trydll2_api int ntrydll2 = 0;
// This is an example of an exported function.
Trydll2_api int fntrydll2 (void)
{
Cout <"fntrydll2/N ";
Return 42;
}
Trydll2_api ctrydll2 * getinstance ()
{
Return new ctrydll2;
}
Trydll2_api int FNP (INT para)
{
Cout <"FNP:" <para <Endl;
Return para = 1;
}
// This is the constructor of a class that has been exported.
// See trydll2.h for the class definition
Ctrydll2: ctrydll2 ()
{
Return;
}
Void ctrydll2: diplay ()
{
Cout <"ctrydll2: diplay" <Endl;
}
Int ctrydll2: FNP (int I)
{
Cout <"ctrydll2: FNP:" <I <Endl;
I ++;
Return I;
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,
/* The hermit uses the DLL-exported class, variable, no-argument function, and argument function. It is the same as the general usage method !!!!!!
*/
# Include "stdafx. H"
/*
Method called by the hermit: If the header file eg: # include "../trydll2.h"
Assume that the Lib file generated when the DLL is generated.
Directly call a function. Eg: fntrydll2 ();
*/
# Include "../trydll2.h"
# Include "windows. H"
# Include <iostream>
Using namespace STD;
Int main (INT argc, char * argv [])
{
Cout <"used DLL with hide way :";
Fntrydll2 ();
// Fntrydll2: do not forget that the C ++ default compiler will rename the function.
// So you should force a statement not to be renamed
// However, It is troublesome to reload .. Because reload is the method for renaming an application.
FNP (200); // a function with Parameters
//..............
Ntrydll2 = 20; // OK
Cout <"ntrydll2:" <ntrydll2 <Endl;
Ctrydll 2 CT;
Ct. diplay ();
Ct. FNP (10 );
Ctrydll2 * PCT = getinstance ();
PCT-> diplay ();
/*
The export variables and classes referenced by the hermit DLL are very simple, and there is no difference with general usage.
*/
Return 0;
}