Last Updated 2005/08/04
Programming Tips Visual C++ API  索 引 
コンピュータ名列挙
2005/08/04

コンピュータ名を列挙するサンプルプログラムである.


        LPNETRESOURCE lpnrDrv;
        DWORD dwResult;
        HANDLE hEnum;
        DWORD cbBuffer = 16384;
        DWORD cEntries = 0xFFFFFFFF;
        DWORD i;

        lpnrDrv = (LPNETRESOURCE)GlobalAlloc( GPTR, cbBuffer );

        lpnrDrv->dwScope = cEntries;
        lpnrDrv->dwType = RESOURCETYPE_ANY;
        lpnrDrv->dwDisplayType = 0x00000001;
        lpnrDrv->dwUsage = 0x00000002;
        lpnrDrv->lpLocalName = NULL;
        lpnrDrv->lpRemoteName = "LM4FADOMAIN";
        lpnrDrv->lpComment = NULL;
        lpnrDrv->lpProvider = "Microsoft Windows Network";
        
        dwResult = WNetOpenEnum( RESOURCE_GLOBALNET,
                                 RESOURCETYPE_DISK,
                                 0,
                                 lpnrDrv,
                                 &hEnum );

        if( dwResult != NO_ERROR ) {
                cout << endl << "列挙不可" << endl;
                cout << "ErrorCode : " << GetLastError() << endl;
                Sleep(3000);
                return;
        }

        dwResult = WNetEnumResource( hEnum, 
                                     &cEntries, 
                                     lpnrDrv, 
                                     &cbBuffer );

        if( dwResult == NO_ERROR ) {
                for( i = 0; i < cEntries; i++ ) {
                        cout << lpnrDrv[i].lpRemoteName << endl;
                }
        } else if( dwResult != ERROR_NO_MORE_ITEMS ) {
                cout << "列挙不可" << endl;
        }

        GlobalFree( (HGLOBAL) lpnrDrv );

        Sleep(DSP_INTERVAL_TIME * 5);

        WNetCloseEnum(hEnum);


参照
GetComputerName
前後のTips
コンピュータ名列挙

DSS ProgrammingTipsCGI Ver2.01