?? traverselan.cpp
字號(hào):
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <Iphlpapi.h>
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "WS2_32.lib")
static PIP_ADAPTER_INFO pAdapter_Info[10] ;
static int adapter_num = 0;
static HWND hwnd_list;
static int index = -1;
static HANDLE hThread = NULL;
static int list_i = 0;
static HWND hwnd_combobox ;
static HWND hwnd_button;
BOOL GetAllAdapterInfo(PIP_ADAPTER_INFO pAdapterInfo)
{
pAdapter_Info[++adapter_num - 1] = pAdapterInfo;
if (pAdapterInfo->Next != NULL)
{
GetAllAdapterInfo(pAdapterInfo->Next);
}
return TRUE;
}
DWORD WINAPI traverse(PVOID pParam)//線程函數(shù)
{
DWORD szDestIP = ntohl( inet_addr(pAdapter_Info[index]->IpAddressList.IpMask.String) &
inet_addr(pAdapter_Info[index]->GatewayList.IpAddress.String));//所在LAN第一個(gè)ip地址
int ip_count = ~ ntohl( inet_addr(pAdapter_Info[index]->IpAddressList.IpMask.String)); //取得所在LAN的IP個(gè)數(shù)
char buff[100];
for (int i = 0;i<ip_count;i++)
{
// 發(fā)送ARP請(qǐng)求,并將返回值IP Address,MAC Address加入list顯示出來(lái)
u_char arDestMac[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
ULONG ulLen = 6;
DWORD temp = szDestIP;
temp = temp + i;
DWORD realip = htonl(temp);
in_addr in;
in.S_un.S_addr = realip;
if(SendARP(realip, 0, (ULONG*)arDestMac, &ulLen) == NO_ERROR)
{
u_char *p = arDestMac;
sprintf(buff,"IP Address : %-16s MAC Address : %02X-%02X-%02X-%02X-%02X-%02X",inet_ntoa(in),p[0], p[1], p[2], p[3], p[4], p[5]);
SendMessage(hwnd_list,LB_ADDSTRING,0,(LPARAM)buff);
SendMessage(hwnd_list,LB_SETCURSEL,list_i++,0);
}
}
SetWindowText(hwnd_button,TEXT("開(kāi)始掃描"));
EnableWindow(hwnd_combobox,true);
hThread = NULL;
return 0;
}
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("局域網(wǎng)IP探測(cè) Version.1"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
640, // initial x size
600, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
char chooseadp[] = "請(qǐng)選擇一個(gè)網(wǎng)卡:";
char ip[] = "IP地址:";
char ip_mask[] = "子網(wǎng)掩碼:";
char ip_gate[] = "網(wǎng)關(guān)地址:";
//static HWND hwnd_combobox ; //about adapter info
int ID_COMBOBOX = 12;
static char * adapter_name[10] ;
int i = 1;
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulLen = 0;
static HWND ip_address[3];//about ip info
int ID_IP[3] = {13,14,15};
int m = 0;
//static HWND hwnd_list;
//static HWND hwnd_button;
int ID_BT = 16;
switch (message)
{
case WM_CREATE:
{
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_WIN95_CLASSES;
if(!InitCommonControlsEx(&icex))
{
MessageBox(NULL,TEXT("Fail to init common controls!"),TEXT("Test"),MB_OK);
break;
}
}
for (m = 0;m<3;m++)
{
ip_address[m] = CreateWindow (WC_IPADDRESS, // window class name
TEXT(""),// window caption
WS_CHILD | WS_VISIBLE, // window style
15, // initial x position
80+60*(m+1), // initial y position
130, // initial x size
25, // initial y size
hwnd, // parent window handle
(HMENU)ID_IP[m], // window menu handle
((LPCREATESTRUCT)lParam)->hInstance, // program instance handle
NULL) ;
}
hwnd_combobox = CreateWindow (TEXT("combobox"), // window class name
TEXT("combobox"),// window caption
CBS_DROPDOWNLIST | WS_CHILD | WS_VISIBLE |CBS_AUTOHSCROLL |
WS_VSCROLL|WS_HSCROLL|CBS_HASSTRINGS, // window style
15, // initial x position
40, // initial y position
600, // initial x size
100, // initial y size
hwnd, // parent window handle
(HMENU)ID_COMBOBOX, // window menu handle
((LPCREATESTRUCT)lParam)->hInstance, // program instance handle
NULL) ;
hwnd_list = CreateWindow (TEXT("listbox"), // window class name
TEXT("combobox"),// window caption
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL , // window style
160, // initial x position
80, // initial y position
450, // initial x size
480, // initial y size
hwnd, // parent window handle
NULL, // window menu handle
((LPCREATESTRUCT)lParam)->hInstance, // program instance handle
NULL) ;
hwnd_button = CreateWindow (TEXT("button"), // window class name
TEXT("開(kāi)始掃描"),// window caption
WS_CHILD | WS_VISIBLE , // window style
65, // initial x position
300, // initial y position
80, // initial x size
30, // initial y size
hwnd, // parent window handle
(HMENU)ID_BT, // window menu handle
((LPCREATESTRUCT)lParam)->hInstance, // program instance handle
NULL) ;
EnableWindow(ip_address[0],false);
EnableWindow(ip_address[1],false);
EnableWindow(ip_address[2],false);
// 為適配器結(jié)構(gòu)申請(qǐng)內(nèi)存
GetAdaptersInfo(pAdapterInfo,&ulLen);
pAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, ulLen);
if(GetAdaptersInfo(pAdapterInfo,&ulLen) == ERROR_SUCCESS)
GetAllAdapterInfo(pAdapterInfo);
int k;
for (k = 0;pAdapter_Info[k]!=NULL;k++)
{
SendMessage(hwnd_combobox,CB_ADDSTRING,0,(LPARAM) (LPCTSTR) (pAdapter_Info[k]->Description));
}
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
TextOut(hdc,15,10,chooseadp,16);
TextOut(hdc,15,120,ip,8);
TextOut(hdc,15,180,ip_mask,10);
TextOut(hdc,15,240,ip_gate,10);
GetClientRect (hwnd, &rect) ;
EndPaint (hwnd, &ps) ;
break ;
case WM_NOTIFY:
break;
case WM_COMMAND:
if (LOWORD(wParam) == ID_COMBOBOX)
{
if (HIWORD(wParam) == CBN_SELCHANGE)
{
index = SendMessage(hwnd_combobox,CB_GETCURSEL,0,0);
SendMessage(ip_address[0],IPM_SETADDRESS,0,ntohl(inet_addr(pAdapter_Info[index]->IpAddressList.IpAddress.String)));
SendMessage(ip_address[1],IPM_SETADDRESS,0,ntohl(inet_addr(pAdapter_Info[index]->IpAddressList.IpMask.String)));
SendMessage(ip_address[2],IPM_SETADDRESS,0,ntohl(inet_addr(pAdapter_Info[index]->GatewayList.IpAddress.String)));
}
}
if (LOWORD(wParam) == ID_BT)
{
if (index != -1)
{
if (hThread != NULL)
{
TerminateThread(hThread,1);
SetWindowText(hwnd_button,TEXT("開(kāi)始掃描"));
EnableWindow(hwnd_combobox,true);
hThread = NULL;
break;
}
hThread = CreateThread(NULL,0,traverse,NULL,0,0);
SetWindowText(hwnd_button,TEXT("結(jié)束掃描"));
EnableWindow(hwnd_combobox,false);
}
else
MessageBox(hwnd,TEXT("請(qǐng)先選擇一個(gè)網(wǎng)卡!"),TEXT("警告"),0);
}
break;
case WM_DESTROY:
PostQuitMessage (0) ;
break ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -