⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traverselan.cpp

📁 扫描局域网IP 扫描局域网IP
💻 CPP
字号:
#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)//线程函数
{
	DWORD szDestIP =  ntohl( inet_addr(pAdapter_Info[index]->IpAddressList.IpMask.String) & 
		              inet_addr(pAdapter_Info[index]->GatewayList.IpAddress.String));//所在LAN第一个ip地址
	int ip_count = ~ ntohl( inet_addr(pAdapter_Info[index]->IpAddressList.IpMask.String)); //取得所在LAN的IP个数
	char buff[100];
	for (int i = 0;i<ip_count;i++)
	{
		// 发送ARP请求,并将返回值IP Address,MAC Address加入list显示出来
		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("开始扫描"));
	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 ("局域网IP探测 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[] = "请选择一个网卡:";
	char ip[] = "IP地址:";
	char ip_mask[] = "子网掩码:";
	char ip_gate[] = "网关地址:";
	
	//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("开始扫描"),// 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);
		
		
		// 为适配器结构申请内存
		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("开始扫描"));
					EnableWindow(hwnd_combobox,true);
					hThread = NULL;
					break;
				}
				
				hThread = CreateThread(NULL,0,traverse,NULL,0,0);
				SetWindowText(hwnd_button,TEXT("结束扫描"));
				EnableWindow(hwnd_combobox,false);
			}
			else
				MessageBox(hwnd,TEXT("请先选择一个网卡!"),TEXT("警告"),0);
		}
		break;
		
	case WM_DESTROY:
		PostQuitMessage (0) ;
		break ;
		}
		return DefWindowProc (hwnd, message, wParam, lParam) ;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -