1

我想枚举工作组中网络中所有基于 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;
}
4

1 回答 1

0

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

于 2020-12-07T13:34:26.583 回答