What is DLL?
DLL refers to a dynamic link library, which is an executable binary that can be called at the same time by multiple applications (even different language applications), is a shared library. The DLL is the concept of establishing a client / server communication, a library file, a function, and data stored in a number of functions, classes, or resources are stored on a DLL (server) and is used by one or more customers. These customers can be Applications or other DLLs.
In the following we will explain how to define a DLL file with VC.NET in a specific example, and call in the VC.NET application, the main function of this example is the machine name of the DLL acquisition system, operating system type and IP address.
In vc.net
Define DLL in
file
Select the VC.NET menu item, select the file -> New-> project, in the pop-up new project dialog box, select the project type to the Visual C project, the category of the MFC, in the top template, select the MFC DLL template , Give the item name TestDLL, select the location of the project, press OK to enter the application settings.
In the application settings, we can see that there are three DLL types, which correspond to three types of DLLs.
The difference between the static DLL and the shared DLL is that the former uses the MFC's static link library, the generated DLL file length is large, generally not in this way, the latter uses the MFC dynamic link library, the generated DLL file length; dynamic Link to the MFC shared DLL all outputs should start with the statement (for proper switching MFC module status): AFX_Manage_State (AFXGETSTAICMODULESTATE ()) Extension DLL is used to establish a derived class of MFC, only written in the MFC class library Application is called. One feature of conventional DLL (including static and dynamic) is that there is a class inherited CWINAPP class (from cwinapp derive, but no message loop), the exported function is a C class or C member function, calling the application of regular DLL The program does not have to be an MFC application. Extending DLL and regular DLL are different, it does not have an object that is inherited from CWINAPP, the compiler defaults a DLL entry function dllmain () as an initialization of the DLL.
You can also add two additional features: Automation and Windows sockets. If you have selected these two items, the program will do some initialization, here we don't discuss it.
In this example, we choose "Use Sharing MFC DLL".
Add code:
1. Import systeminfo.cpp and systeminfo.h files in the project, which is used to get the machine name, operating system version, and native IP list. For details, refer to the source file.
In the TestDLL.h header file, introduce systeminfo.h header files
#include "systeminfo.h"
Add variables:
Csysteminfo m_systeminfo;
2, add three functions to the ctestdllapp class to get information:
// Machine name
Char * gethostname (void);
// System type
Char * getSystemType (void);
// IP address
Void getipaddresslist (char ** lpiplist, dword * lpnumber);
The function is defined as follows:
// Machine name char * ctestdllapp :: gethostname (void)
{
Char * lpsz = new char [1024];
m_systeminfo.getHostName (LPSZ);
Return LPSZ;
}
// System type
Char * ctestdllapp :: getSystemType (void)
{
Char * lpsz = new char [1024];
m_systeminfo.getlsystemType (LPSZ);
Return LPSZ;
}
// IP address
Void ctestdllapp :: getipaddresslist (char ** lpiplist, dword * lpnumber)
{
m_systeminfo.getipaddressList (LPIPLIST, LPNUMBER);
}
3, add output functions:
Open the "TestDLL.cpp" file in the TestDLL project.
// Unique CTestDLLAPP object
Ctestdllapp theApp;
The rear adding the output DLL function, the function is defined as follows:
/ **************************************************************************************************** ** /
/ **********************************************************
Function Name: gethostname
Function: Get the machine name of this machine
Back: strhostname - Name of this machine
*************************************************** /
Extern "C" _Declspec (DLLEXPORT) Void getHostName (LPTSTSTSTRHOSTNAME)
{
// If it is to pass the string, you need to use the address of the strCPy copy string without or equal.
STRCPY (strHostname, THEAPP.GETHOSTNAME ());
}
/ **********************************************************
Function Name: GetSystemType
Function: Get the version of the operating system
Returns: strsystemtype- Native operating system version
*************************************************** /
Extern "c" _declspec (dllexport) Void getSystemType (Char * strsystemtype)
{
STRCPY (strsystemtype, theapp.getsystemtype ());
}
/ **********************************************************
Function Name: GetipaddressList
Function: Get the IP address of this unit
Returns: LPIPLIST- Number of IP Address of this machine, number of lpnumber IP addresses
*************************************************** /
Extern "C" _Declspec (DLLEXPORT) Void getipaddressList (char ** lpiplist, dword * lpnumber)
{
THEAPP.GETIPADDRESSLIST (LPIPLIST, LPNUMBER);
}
Finally compile the project file, generate a TestDLL.dll file.
At this point, a DLL file is already done.
In vc.net
Use DLL in
file
Newly built a dialog-based VC.NET project DemotestDLL, the interface is shown below (running result map):
In order to make DemotestDLL to call the TestDll.dll program, you need to "see" DLL programs for the former. We take the testDLL.dll file to the DEMOTESTDLL's debug directory, the order of the Windows program positioning DLL is: 1. Contains the directory of the EXE file.
2, the current working directory of the process.
3, the Windows System Directory.
4, windows directory.
5, list a series of directories in the PATH environment variable.
Add the following code on the test DLL button:
Void cdemotestDLDLG :: OnbnclicKedButton1 ()
{
// TODO: Add control notification processing code here
// Declare the DLL function
TypedEf void (_CDecl * gethostname) (LPTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTR STRHOSTNAME);
Typedef void (_CDecl * getSystemType);
Typedef void (_CDecl * getipaddresslist) (CHAR ** LPIPLIST, DWORD * LPNUMBER);
// Declaration function handle
HModule HTestDLL = NULL;
Gethostname gethostname = null;
GetsystemType getSystemType = NULL;
GetipaddressList GetIpaddressList = NULL;
/ / Load Dynamic Link Library
HTESTDLL = LoadLibrary ("Testdll.dll");
IF (htestdll == null) /
{
Printf ("cannot loading lcddll.dll / n");
exit (0);
}
/ *** Find the entrance to each function ***** /
//system name
GetHostName = (gethostname) getProcaddress (HTestDLL, "gethostname");
IF (gethostname == null)
{
Printf ("Cannot Load Process Gethostname / N");
Freelibrary (htestdll);
Exit (1);
}
// Operating system type
GetSystemType = (getSystemType) getProcaddress (HTestDLL, "GetSystemType");
IF (getSystemType == NULL)
{
Printf ("Cannot Load Process GetSystemType / N");
Freelibrary (htestdll);
Exit (1);
}
// IP address list
GetipaddressList = (GetipaddressList) getProcaddress (HTestDLL, "GetipadDressList);
IF (getSystemType == NULL)
{
Printf ("Cannot Load Process GetipaddressList / N");
Freelibrary (htestdll);
Exit (1);
}
/ *** Use lPTSTR and the effect of using char * is the same *** /
// Take the machine name
LPTSTR SZHOSTNAME = New Char [1024];
(* Gethostname) (szhostname);
// Take the operating system type
Char * szsystemtype = new char [1024];
(* GetSystemType) (szsystemtype); // ip address list
DWORD iPLISTNUMBER = 0;
// declaration
// lptstr * lpaddress = new lptstr [256];
// Declaration
Char ** lpaddress = new char * [256];
For (int i = 0; i <256; i )
{
LPADDRESS [I] = NULL;
}
(* Getipaddresslist) (LPADDRESS, & iPLISTNUMBER);
// Display on the interface
m_sethostname.SetWindowText (SzhostName);
m_setsystemType.SetWindowText (SzsystemType);
// Add IP to the LIST
For (int i = 0; i { m_iplist.addstring (LPADDRESS [I]); } } The result of the compilation operation is as shown above. Complete source code Testdll.rar reference: http://www.51pub.net/class/showproject.asp?art_id=212&cat_id=12