Vc. NET to define and use MFC DLLs (II)

Source: Internet
Author: User
Tags exit printf

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.