Obtain Nic information through ipconfig in Windows

Source: Internet
Author: User

In network programming, we may sometimes use MAC addresses. If we use VC ++, we can useUchar NetBIOS (
PNCB
PNCB/* Pointer to the network control block */)Obtain;However, in actual application, I found that it does not work every time. in a notebook with a wireless network card, I can't find a mac on a notebook with a virtual machine. Maybe I have no idea about the usage of this function!

I used a Java program to obtain Mac information from a common method and ipconfig to obtain information about all NICs. Next we will look at the specific method.

First, we define a structure to save the NIC information:

Typedef struct _ tagadapterinfo {
Cstring description; // Nic description
Cstring physicaladdr; // Mac
Cstring ipaddr [64]; // a maximum of 64 IP addresses
Cstring subnetmask [64]; // each IP Address has a maximum of 64 masks.
Cstring gateway; // The default gateway.
Long lipnum; // number of IP addresses corresponding to the NIC
} Adapterinfo, * padapterinfo;

We define a method to obtain information about all NICs on the local machine:

int GetAllAdapterInfo(CArray<AdapterInfo,AdapterInfo>& AdapterInfoList,

CString& Error);

Let's take a look at the implementation below:

int CTestDlg::GetAllAdapterInfo(

Carray <adapterinfo, adapterinfo> & adapterinfolist, cstring error)
{
Handle hread, hwrite; // defines the input and output handle.
Cstring strerror; // error message
Cstring strcon, strbuf;
Bool nreturn;
Char buffer [2048] = {0 };
Lpstr P = "ipconfig/all ";
Unsigned long lnreadnum;
Int nindex = 0, from = 0, To = 0, nloop = 0;
Startupinfo SA;
Process_information pinfo;
Security_attributes sapipe;
Sapipe. nlength = sizeof (security_attributes );
Sapipe. lpsecuritydescriptor = NULL;
Sapipe. binherithandle = true;
If (! Createpipe (& hread, & hwrite, & sapipe, 0) // create an MPS queue
{
Strerror. Format ("create pipe errorcode: % d", getlasterror ());
Error = strerror;
Return 1;
}
Memset (& SA, 0, sizeof (SA ));
SA. cb = sizeof (SA );
SA. dwflags = startf_useshowwindow | startf_usestdhandles;
SA. wshowwindow = sw_hide;
SA. hstdoutput = hwrite;
SA. hstderror = hwrite;

// Create a process
Nreturn =: CreateProcess (null, P, & sapipe, & sapipe,
True, 0, null, null, & SA, & pinfo );
If (! Nreturn)
{
Strerror. Format ("create process errorcode: % d", getlasterror ());
Error = strerror;
Return 2;
}
// Sleep (1, 1000 );
Closehandle (pinfo. hthread );
Closehandle (pinfo. hprocess );
Closehandle (hwrite );
// Read is the output of the MPs queue.
Do {
Memset (buffer, 0,2048 );
Nreturn = readfile (hread, buffer, max_path, & lnreadnum, null );
If (nreturn)
{
Strbuf. Format ("% s", buffer );
Strcon + = strbuf;
}

} While (nreturn & lnreadnum );

Adapterinfo Info;

// Find the NIC information in the output information

Do
{
Nindex = strcon. Find ("Description", nindex );
If (nindex <=-1)
{
Break;
}
From = strcon. Find (":", nindex) + 2;
To = strcon. Find ("/N", from );
Info. Description = strcon. mid (from, to-from-1 );
Nindex =;
Nindex = strcon. Find ("physical address", nindex );
From = strcon. Find (":", nindex) + 2;
To = strcon. Find ("/N", from );
Info. physicaladdr = strcon. mid (from, to-from-1 );
Nindex =;
Nloop = 0;
While (nloop <64)
{
Nindex = strcon. Find ("ip address", nindex );
If (nindex <=-1)
{
Break;
}
From = strcon. Find (":", nindex) + 2;
To = strcon. Find ("/N", from );
Info. ipaddr [nloop] = strcon. mid (from, to-from-1 );
Nindex =;
Nindex = strcon. Find ("subnet mask", nindex );
From = strcon. Find (":", nindex) + 2;
To = strcon. Find ("/N", from );
Info. subnetmask [nloop] = strcon. mid (from, to-from-1 );
Nloop ++;
}
Info. lipnum = nloop;
Nindex =;
Nindex = strcon. Find ("Default Gateway", nindex );
From = strcon. Find (":", nindex) + 2;
To = strcon. Find ("/N", from );
Info. Gateway = strcon. mid (from, to-from-1 );
Adapterinfolist. Add (Info );
} While (true );
Return 0;
}
The method is just a basic application of pipelines. Let's share it with you! In addition, we can use the IP help function to obtain the IP address.

 

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.