那位大大帮我搞定一下?高分回赠!通过一段程序扫描到自己所在网段内机器的IP地址 使用vc++编一段应用程序,可以扫描到自己所在局域网内的所有机器(包括启动的和未启动的)的ip地址!那位大大帮我编一下!高分回馈! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这里有一段我好久以前写的程序,能够获取已经开机的局域网机器信息实现的不是很好,后来我就懒得改正了至于未开机的机子,呵呵,基本是无法得到的/***********************************LANEnum.c*枚举局域网的主机名和对应的ip*windows***********************************/#include <WINSOCK2.H>#include <stdio.h>#include <windows.h>#pragma comment (lib,"mpr.lib")#pragma comment (lib,"ws2_32.lib")int nComputers=0;int EnumerateFunc(LPNETRESOURCE lpnr){ DWORD dwResult, dwResultEnum; HANDLE hEnum; DWORD cbBuffer = 16384; // 16K is a good size DWORD cEntries = -1; // enumerate all possible entries LPNETRESOURCE lpnrLocal; // pointer to enumerated structures DWORD i; struct hostent *myhostent;//=gethostbyname(ip); struct in_addr addr; WSAData wsadata; WSAStartup(MAKEWORD(1,1),&wsadata); dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources RESOURCETYPE_DISK, // all resources 0, // enumerate all resources lpnr, // NULL first time the function is called &hEnum); // handle to the resource if (dwResult != NO_ERROR) { //printf("WNetOpenEnum() error :%d \n",GetLastError()); return FALSE; } lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer); if (lpnrLocal == NULL) { printf("GlobalAlloc() error : %d\n",GetLastError()); return FALSE; } do { ZeroMemory(lpnrLocal, cbBuffer); // // Call the WNetEnumResource function to continuethe enumeration. // dwResultEnum = WNetEnumResource(hEnum,&cEntries,lpnrLocal, &cbBuffer); if (dwResultEnum == NO_ERROR) { for(i = 0; i < cEntries; i++) { // Call an application-defined function to // display the contents of the NETRESOURCE structures. // if (lpnrLocal[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER) //是一台计算机 { nComputers++; //由于lpRemoteName是形如"\\xxx"这样的主机名字,所以要从xxx开始转换 myhostent=gethostbyname(&(lpnrLocal[i].lpRemoteName[2])); if (!myhostent) { printf("gethostbyname %s error : %d\n",lpnrLocal[i].lpRemoteName,GetLastError()); break; } CopyMemory(&addr.S_un.S_addr, myhostent->h_addr_list[0],myhostent->h_length); printf("%s : %s\n",lpnrLocal[i].lpRemoteName,inet_ntoa(addr)); } // If the NETRESOURCE structure represents a container resource, // call the EnumerateFunc function recursively. if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER)) EnumerateFunc(&lpnrLocal[i]); //继续调用枚举 } } else if (dwResultEnum != ERROR_NO_MORE_ITEMS) { printf("WNetEnumResource() error :%d \n",GetLastError()); break; } }while(dwResultEnum != ERROR_NO_MORE_ITEMS); if (lpnrLocal) GlobalFree((HGLOBAL)lpnrLocal); dwResult = WNetCloseEnum(hEnum); return TRUE;}void main(){ printf("starting....\n"); int time=GetTickCount(); EnumerateFunc(NULL); time=GetTickCount()-time; printf("%d computers found and cost %d seconds.\n",nComputers,time/1000);} 将HDC作为一个类的成员变量,在类的方法中ReleaseDC就会发生错误? 用GDI+实现翻页效果 vc++ 6.0 go to definition of "***" 后如何返回"***" 处? 调用.net开发的dll必须安装.netFrameWork吗? 驱动开发资源收集 网吧上网突破的一招 对话框和FormView的问题 怎样获取其它程序向系统发送的消息? 有对串行通讯(MODEM控制)比较熟悉的朋友吗? 帮帮我吧,很菜的问题 菜菜问题,求教if语句 activex基于win32如果创建(不基本于mfc)
实现的不是很好,后来我就懒得改正了
至于未开机的机子,呵呵,基本是无法得到的/**********************************
*LANEnum.c
*枚举局域网的主机名和对应的ip
*windows
***********************************/#include <WINSOCK2.H>
#include <stdio.h>
#include <windows.h>#pragma comment (lib,"mpr.lib")
#pragma comment (lib,"ws2_32.lib")int nComputers=0;
int EnumerateFunc(LPNETRESOURCE lpnr)
{
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384; // 16K is a good size
DWORD cEntries = -1; // enumerate all possible entries
LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
DWORD i;
struct hostent *myhostent;//=gethostbyname(ip);
struct in_addr addr; WSAData wsadata;
WSAStartup(MAKEWORD(1,1),&wsadata);
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
RESOURCETYPE_DISK, // all resources
0, // enumerate all resources
lpnr, // NULL first time the function is called
&hEnum); // handle to the resource if (dwResult != NO_ERROR)
{
//printf("WNetOpenEnum() error :%d \n",GetLastError());
return FALSE;
} lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
if (lpnrLocal == NULL)
{
printf("GlobalAlloc() error : %d\n",GetLastError());
return FALSE;
} do
{ ZeroMemory(lpnrLocal, cbBuffer);
//
// Call the WNetEnumResource function to continuethe enumeration.
//
dwResultEnum = WNetEnumResource(hEnum,&cEntries,lpnrLocal, &cbBuffer); if (dwResultEnum == NO_ERROR)
{
for(i = 0; i < cEntries; i++)
{
// Call an application-defined function to
// display the contents of the NETRESOURCE structures.
//
if (lpnrLocal[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER) //是
一台计算机
{
nComputers++;
//由于lpRemoteName是形如"\\xxx"这样的主机名字,所以要从xxx开
始转换
myhostent=gethostbyname(&(lpnrLocal[i].lpRemoteName[2]));
if (!myhostent)
{
printf("gethostbyname %s error : %d\n",lpnrLocal[i].lpRe
moteName,GetLastError());
break;
}
CopyMemory(&addr.S_un.S_addr, myhostent->h_addr_list[0],myho
stent->h_length);
printf("%s : %s\n",lpnrLocal[i].lpRemoteName,inet_ntoa(addr)
); }
// If the NETRESOURCE structure represents a container resource, // call the EnumerateFunc function recursively. if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage & RESOURCEUS
AGE_CONTAINER))
EnumerateFunc(&lpnrLocal[i]); //继续调用枚举 }
}
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
{
printf("WNetEnumResource() error :%d \n",GetLastError());
break;
} }while(dwResultEnum != ERROR_NO_MORE_ITEMS); if (lpnrLocal)
GlobalFree((HGLOBAL)lpnrLocal);
dwResult = WNetCloseEnum(hEnum);
return TRUE;
}void main()
{
printf("starting....\n");
int time=GetTickCount();
EnumerateFunc(NULL);
time=GetTickCount()-time;
printf("%d computers found and cost %d seconds.\n",nComputers,time/1000);}