如何列举出网络(局域网)上所有的SQL Server服务器100分

代码客 2002-07-17 05:27:22
加精
如题。
...全文
109 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
NowCan 2002-08-20
  • 打赏
  • 举报
回复
我觉得应该是用NetServerEnum,这是我从MSDN里抄的。

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
LPSERVER_INFO_101 pBuf = NULL;
LPSERVER_INFO_101 pTmpBuf;
DWORD dwLevel = 101;
DWORD dwPrefMaxLen = -1;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwTotalCount = 0;
DWORD dwServerType = SV_TYPE_SERVER; // all servers,据说改成SV_TYPE_SQLSERVER就可以了
DWORD dwResumeHandle = 0;
NET_API_STATUS nStatus;
LPTSTR pszServerName = NULL;
DWORD i;

if (argc > 2)
{
fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
exit(1);
}
// The server is not the default local computer.
//
if (argc == 2)
pszServerName = argv[1];
//
// Call the NetServerEnum function to retrieve information
// for all servers, specifying information level 101.
//
nStatus = NetServerEnum(pszServerName,
dwLevel,
(LPBYTE *) &pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries,
dwServerType,
NULL,
&dwResumeHandle);
//
// If the call succeeds,
//
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
//
// Loop through the entries and
// print the data for all server types.
//
for (i = 0; i < dwEntriesRead; i++)
{
assert(pTmpBuf != NULL);

if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}

printf("\tPlatform: %d\n", pTmpBuf->sv101_platform_id);
wprintf(L"\tName: %s\n", pTmpBuf->sv101_name);
printf("\tVersion: %d.%d\n",
pTmpBuf->sv101_version_major,
pTmpBuf->sv101_version_minor);
printf("\tType: %d", pTmpBuf->sv101_type);
//
// Check to see if the server is a domain controller;
// if so, identify it as a PDC or a BDC.
//
if (pTmpBuf->sv101_type & SV_TYPE_DOMAIN_CTRL)
wprintf(L" (PDC)");
else if (pTmpBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL)
wprintf(L" (BDC)");

printf("\n");
//
// Also print the comment associated with the server.
//
wprintf(L"\tComment: %s\n\n", pTmpBuf->sv101_comment);

pTmpBuf++;
dwTotalCount++;
}
// Display a warning if all available entries were
// not enumerated, print the number actually
// enumerated, and the total number available.

if (nStatus == ERROR_MORE_DATA)
{
fprintf(stderr, "\nMore entries available!!!\n");
fprintf(stderr, "Total entries: %d", dwTotalEntries);
}

printf("\nEntries enumerated: %d\n", dwTotalCount);
}
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Free the allocated buffer.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);

return 0;
}

代码客 2002-08-20
  • 打赏
  • 举报
回复
希望各位继续给出一个详细的答案
whhome1981 2002-08-20
  • 打赏
  • 举报
回复
搜索机器的端口
aawolf 2002-08-19
  • 打赏
  • 举报
回复
是呀,Sql Server客户端连接向导中的那个列表不会也是这么做的吧?
fenglic 2002-08-19
  • 打赏
  • 举报
回复
1、搜索所有机器的机器名,这个功能很多人已经做出范例;
2、一般Sql Server的端口是1433,探测这个端口,看看返回是否Sql Server;假如你狠一点连其他端口都扫描的话,安装程序就有点慢了;

不过,感觉有点做hack-program的味道啊
aawolf 2002-08-19
  • 打赏
  • 举报
回复
谁能解决这个问题我愿意再出200分!
代码客 2002-08-19
  • 打赏
  • 举报
回复
我要在我的程序里面提供有这个功能怎么办?

我想在安装软件的时候,把它们列出来给客户选择。
lingbin 2002-07-21
  • 打赏
  • 举报
回复
up,说清一点好吗.
  • 打赏
  • 举报
回复
用流光搜索sql主机阿....:)

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧