使用vc++编一段应用程序,可以扫描到自己所在局域网内的所有机器(包括启动的和未启动的)的ip
地址!那位大大帮我编一下!高分回馈!

解决方案 »

  1.   

    这里有一段我好久以前写的程序,能够获取已经开机的局域网机器信息
    实现的不是很好,后来我就懒得改正了
    至于未开机的机子,呵呵,基本是无法得到的/**********************************
    *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);}