Obtain the IP address and MAC address

Source: Internet
Author: User

# Include "stdafx. H"

# Include <winsock2.h>
// This header file defines the socket programming function
# Include <stdio. h>
// This header file declares the input/output stream function.
# Include <stdlib. h> // This header file defines some common functions.
# Include # Include <windef. h> // This header file defines all basic data states of windows.
# Include <nb30.h> // This header file declares all NetBIOS functions.
# Pragma comment (Lib, "ws2_32.lib") // connect to the ws2_32.lib library. ws2_32.lib is used as long as the Winsock API function is used in the program.
# Pragma comment (Lib, "netapi32.lib") // connect to the netapi32.lib library. netapi32.dll is used in MAC retrieval.
// Using namespace system;

Void checkip (void) // defines the checkip function, used to retrieve the local IP Address
{
Wsadata;
Char name [155];
Char * IP;
 
Phostent hostinfo;

// Call makeword () to obtain the correct value of winsocl. It is used to load the winscok library below.
If (wsastartup (makeword (2, 0), & wsadata) = 0)
{
// Load the Winsock database. If the return value of the wsastartup () function is 0, the load is successful and the program can continue to run.
If (gethostname (name, sizeof (name) = 0)
{
// If successful, save the local host name to the buffer specified by the name parameter
If (hostinfo = gethostbyname (name ))! = NULL)
{
// This is to obtain the host. If the host name is successfully obtained, a pointer pointing to hostinfo and hostinfo being a phostent variable will be returned.
IP = inet_ntoa (* (struct in_addr *) * hostinfo-> h_addr_list );
// The inet_addr () function converts an address string to an IP address.
// Call the inet_ntoa () function to convert h_addr_list in the hostinfo structure variable to a standard IP address (for example, 202.197.11.12 .)
Printf ("IP Address: % s \ n", ip); // output IP Address
}
}
Wsacleanup (); // uninstall the Winsock library and release all resources
}
}

// Implemented through the built-in netapi32.dll function in WindowsNT/Win2000. First, you can obtain
// The number and the internal number of each Nic. Then, the ncbastat command is sent to obtain the MAC address of each Nic.
Int getmac (char * Mac) // use netapi to obtain the MAC address of the NIC
{
NCB;
// Define an NCB (Network Control Block) type struct variable NCB
Typedef struct _ astat _
{
Adapter_status adapt;
Name_buffer namebuff [30];
} Astat, * pastat;
Astat adapter;

Typedef struct _ lana_enum
{
Uchar length;
Uchar Lana [max_lana];
} Lana_enum;
Lana_enum;

// Obtain the NIC Information List
Uchar uretcode;
Memset (& NCB, 0, sizeof (NCB); // set the NCB value to 0.
Memset (& lana_enum, 0, sizeof (lana_enum); // clear the variable lana_enum of the structure type and assign the value 0

// Assign a value to the struct variable NCB
NCB. ncb_command = ncbenum; // count the number of NICs IN THE SYSTEM
NCB. ncb_buffer = (unsigned char *) & lana_enum; // The ncb_buffer Member points to the buffer filled by the lana_enum structure.
NCB. ncb_length = sizeof (lana_enum );

// Send the ncbenum command to the NIC to obtain the NIC information of the current machine. For example, the number of each NIC (MAC address)
Uretcode = NetBIOS (& NCB); // call netbois (NCB) to obtain the NIC serial number
If (uretcode! = Nrc_goodret)
Return uretcode;

// Enter the ID of each Nic to obtain its MAC address.
For (INT Lana = 0; Lana <lana_enum.length; Lana ++)
{
NCB. ncb_command = ncbreset; // sends the ncbreset command to the NIC for initialization.
NCB. ncb_lana_num = lana_enum.lana [Lana];
Uretcode = NetBIOS (& NCB );
If (uretcode = nrc_goodret)
Break;
}
If (uretcode! = Nrc_goodret)
Return uretcode;

// Prepare to obtain the MAC address of the Status block of the Interface Card
Memset (& NCB, 0, sizeof (NCB ));
NCB. ncb_command = ncbastat; // send the ncbstat command to the NIC to obtain the NIC information.
NCB. ncb_lana_num = lana_enum.lana [0]; // specify the NIC ID
Strcpy (char *) NCB. ncb_callname, "*"); // assign the remote system name *
NCB. ncb_buffer = (unsigned char *) & adapter; // specify the variable for storing the returned information
NCB. ncb_length = sizeof (adapter );

// Then send the ncbastat command to obtain the NIC information.
Uretcode = NetBIOS (& NCB );

// Obtain the information of the NIC. If the NIC works normally, the standard colon separation format is returned.
If (uretcode! = Nrc_goodret)
Return uretcode;

Sprintf (MAC, "% 02x-% 02x-% 02x-% 02x-% 02x-% 02x ",
Adapter. Adapt. adapter_address [0],
Adapter. Adapt. adapter_address [1],
Adapter. Adapt. adapter_address [2],
Adapter. Adapt. adapter_address [3],
Adapter. Adapt. adapter_address [4],
Adapter. Adapt. adapter_address [5]
);
Return 0;
}

Int main ()
{

Checkip ();
Char Mac [200];
Getmac (MAC );
Printf ("MAC address: % s \ n", Mac );
System ("pause ");
Return 0;
}

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.