在LAN中使用Windows操作系统枚举计算机

时间:2013-08-30 06:12:49

标签: c++ c netapi32

我想枚举工作组中网络中所有基于Windows的计算机。为此,我正在使用NetApi32.dll功能。在我的网络中,我安装了windows,mac和linux操作系统。我们如何才能过滤掉Windows机器。以下是我正在使用的代码。如果我有任何错误,或者有任何其他方法可以实现,请发表评论。

BOOLEAN
LocalEnumServers(const WCHAR *pcwszDomainName) {
    DWORD dwCount;
    DWORD dwLevel = 101;
    NET_API_STATUS Status;
    HRESULT hResult = S_OK;
    DWORD dwEntriesRead = 0;
    BOOLEAN bResult = FALSE;
    DWORD dwTotalEntries = 0;
    DWORD dwResumeHandle = 0;
    LPSERVER_INFO_101 pTmpBuf;
    LPSERVER_INFO_101 pBuf = NULL;
    TCHAR szOSName[_MAX_PATH] = _T("");
    DWORD dwServerType = SV_TYPE_SERVER;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;

    if (NULL == pcwszDomainName)
    {
        return FALSE;
    }

    pBuf = NULL;

    Status = NetServerEnum(
                        NULL,
                        dwLevel,
                        (LPBYTE *)&pBuf,
                        dwPrefMaxLen,
                        &dwEntriesRead,
                        &dwTotalEntries,
                        dwServerType,
                        /*(LPCSTR)*/pcwszDomainName,
                        &dwResumeHandle
                        );
    if ((NERR_Success == Status || Status == ERROR_MORE_DATA) && pBuf != NULL) {
        pTmpBuf = (SERVER_INFO_101 *)pBuf;
        for (dwCount = 0; dwCount < dwEntriesRead; dwCount++) {
            wprintf(L"Server name = %15s, Server type = 0x%08X, Verson = (%u:%u), Comment = %s\n", pTmpBuf[dwCount].sv101_name, pTmpBuf[dwCount].sv101_type, pTmpBuf[dwCount].sv101_version_major & MAJOR_VERSION_MASK, pTmpBuf[dwCount].sv101_version_minor, pTmpBuf[dwCount].sv101_comment);
            //wprintf(L"Server name -> %s, Server type -> 0x%X, Platform ID -> 0x%X, Verson -> (%u:%u)\n", pTmpBuf[dwCount].sv101_name, pTmpBuf[dwCount].sv101_type, pTmpBuf[dwCount].sv101_platform_id, pTmpBuf[dwCount].sv101_version_major & MAJOR_VERSION_MASK, pTmpBuf[dwCount].sv101_version_minor);

            if (
                SV_TYPE_WINDOWS != (SV_TYPE_WINDOWS & pTmpBuf[dwCount].sv101_type)      &&
                SV_TYPE_SERVER_NT != (SV_TYPE_SERVER_NT & pTmpBuf[dwCount].sv101_type)  &&
                SV_TYPE_NT != (SV_TYPE_NT & pTmpBuf[dwCount].sv101_type)
                ) {
                wprintf(L"Flag check (%u, %u, %u)\n", SV_TYPE_WINDOWS != (SV_TYPE_WINDOWS & pTmpBuf[dwCount].sv101_type),
                    SV_TYPE_SERVER_NT != (SV_TYPE_SERVER_NT & pTmpBuf[dwCount].sv101_type),
                    SV_TYPE_NT != (SV_TYPE_NT & pTmpBuf[dwCount].sv101_type));

                continue;
            }
        }
    }
    else {
        wprintf(L"NetServerEnum failed (Err -> %u)\n", GetLastError());
    }

    if (pBuf != NULL) {
        NetApiBufferFree(pBuf);
    }

    return TRUE;
}

int _tmain(int argc, _TCHAR* argv[]) {
    LocalEnumServers(L"workgroup");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

对于UNIX类型的系统,您可以添加if条件并使用unix系统和sv类型的预定义宏执行AND。但是对于MAC来说是个问题。