Using DLL files in vc.net
Create a new Vc.net project Demotestdll based on the dialog box, the following diagram (run result chart):
In order for Demotestdll to be able to invoke the TestDLL.dll program, the former is required to "see" the DLL program. We take the TestDLL.dll file to the Demotestdll debug directory, where a Windows program locates DLLs in the following order:
1, contains the exe file directory.
2, the current working directory of the process.
3, Windows system directory.
4, Windows directory.
5. A series of directories listed in the PATH environment variable.
Add the following code to the test DLL button:
Program code:
void Cdemotestdlldlg::onbnclickedbutton1 ()
{
TODO: Add control notification Handler code here
declaring DLL functions
typedef void (_cdecl *gethostname) (LPTSTR strhostname);
typedef void (_cdecl *getsystemtype) (char * strsystemtype);
typedef void (_cdecl *getipaddresslist) (char * Lpiplist,dword *lpnumber);
Declaring a function handle
hmodule htestdll = NULL;
GetHostName gethostname = NULL;
Getsystemtype getsystemtype = NULL;
Getipaddresslist getipaddresslist = NULL;
Load Dynamic Link Libraries
Htestdll = LoadLibrary ("TestDLL.dll");
if (Htestdll = NULL) \
{
printf ("Cannot load lcddll.dll\n");
Exit (0);
}
/*** find the entry for 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);
}
The effect of/*** using LPTSTR and char* definitions is the same ***/
Take machine name
LPTSTR szhostname = new char[1024];
(*gethostname) (Szhostname);
Take operating system type
char* Szsystemtype = new char[1024];
(*getsystemtype) (Szsystemtype);
IP Address List
DWORD iplistnumber = 0;
Declarative Way One
lptstr* lpaddress = new lptstr[256];
Declaration Mode Two
char** lpaddress = new char*[256];
for (int i=0;i<256;i++)
{
Lpaddress[i] = NULL;
}
(*getipaddresslist) (Lpaddress,&iplistnumber);
Display in the interface
M_sethostname.setwindowtext (Szhostname);
M_setsystemtype.setwindowtext (Szsystemtype);
Add IP to List
for (int i=0;i<iplistnumber;i++)
{
M_iplist.addstring (Lpaddress[i]);
}
}
The results of the compilation run are as shown in the figure above.