Board index » delphi » Find MS SQL Server on a network

Find MS SQL Server on a network


2004-08-05 10:38:37 PM
delphi51
Hi to all
I want to know how i find in a network all sql servers. Users must may have
the option to select which server they want to connect. I find an exemple
but i does not work on W98 because it use netapi32.dll.
// broadcasted so that any single unit can expose method to handle
//enumerating
// their type of server
type TServerInfo101 = record
platform_id: DWORD;
name: PWideChar;
version_major: DWORD;
version_minor: DWORD;
server_type: DWORD;
comment: PWideChar;
end;
PServerInfo101 = ^TServerInfo101;
function NetServerEnum(const ServerName: PWideString;
level: DWORD;
var Buffer: pointer;
PrefMaxLen: DWORD;
var EntriesRead: DWORD;
var TotalEntries: DWORD;
ServerType: DWORD;
const Domain: PWideChar;
var ResumeHandle: DWORD): DWORD; stdcall; external
'netapi32.dll';
function NetApiBufferFree(Buffer: pointer): DWORD; stdcall; external
'netapi32.dll';
function GetServerNames(const ServerType:DWORD):TStringList;
const
NERR_SUCCESS = 0;
MAX_PREFERRED_LENGTH = DWORD(-1);
SV_TYPE_WORKSTATION = $00000001;
SV_TYPE_SERVER = $00000002;
SV_TYPE_SQLSERVER = $00000004;
SV_TYPE_DOMAIN_CTRL = $00000008;
SV_TYPE_DOMAIN_BAKCTRL = $00000010;
SV_TYPE_TIME_SOURCE = $00000020;
SV_TYPE_AFP = $00000040;
SV_TYPE_NOVELL = $00000080;
SV_TYPE_DOMAIN_MEMBER = $00000100;
SV_TYPE_PRINTQ_SERVER = $00000200;
SV_TYPE_DIALIN_SERVER = $00000400;
SV_TYPE_XENIX_SERVER = $00000800;
SV_TYPE_SERVER_UNIX = SV_TYPE_XENIX_SERVER;
SV_TYPE_NT = $00001000;
SV_TYPE_WFW = $00002000;
SV_TYPE_SERVER_MFPN = $00004000;
SV_TYPE_SERVER_NT = $00008000;
SV_TYPE_POTENTIAL_BROWSER = $00010000;
SV_TYPE_BACKUP_BROWSER = $00020000;
SV_TYPE_MASTER_BROWSER = $00040000;
SV_TYPE_DOMAIN_MASTER = $00080000;
SV_TYPE_SERVER_OSF = $00100000;
SV_TYPE_SERVER_VMS = $00200000;
SV_TYPE_WINDOWS = $00400000; // Windows95 and above
SV_TYPE_DFS = $00800000; // Root of a DFS tree
SV_TYPE_CLUSTER_NT = $01000000; // NT Cluster
SV_TYPE_DCE = $10000000; // IBM DSS (Directory and Security Services) or
equivalent
SV_TYPE_ALTERNATE_XPORT = $20000000; // return list for alternate
transport
SV_TYPE_LOCAL_LIST_ONLY = $40000000; // Return local list only
SV_TYPE_DOMAIN_ENUM = $80000000;
SV_TYPE_ALL = $FFFFFFFF; // handy for NetServerEnum2
function GetServerNames(const ServerType:DWORD):TStringList;
var
Buffer: pointer;
EntriesRead,i,ErrCode,ResumeHandle,TotalEntries: DWORD;
PDomainUnicode: PWideChar;
ServerInfo: PServerInfo101;
slServerNames: TStringList;
begin
result := nil;
slServerNames := TStringList.Create;
ResumeHandle := 0;
PDomainUnicode := nil;
errCode := NetServerEnum(nil, 101, Buffer, MAX_PREFERRED_LENGTH,
EntriesRead, TotalEntries, ServerType, PDomainUnicode,
ResumeHandle);
if (errCode <>NERR_SUCCESS) then
slServerNames.Add('Can''t enumerate servers!')
else begin
try
ServerInfo := Buffer;
for i := 1 to EntriesRead do
begin
slServerNames.Add(ServerInfo^.name);
Inc(ServerInfo);
end;
if slServerNames.Count = 0 then
slServerNames.Add('No servers available!');
finally
NetApiBufferFree(Buffer);
Result := slServerNames;
end; // end of try finally
end;
end;
function GetServerNames(const ServerType:DWORD):TStringList;
var
Buffer: pointer;
EntriesRead,i,ErrCode,ResumeHandle,TotalEntries: DWORD;
PDomainUnicode: PWideChar;
ServerInfo: PServerInfo101;
slServerNames: TStringList;
begin
result := nil;
slServerNames := TStringList.Create;
ResumeHandle := 0;
PDomainUnicode := nil;
errCode := NetServerEnum(nil, 101, Buffer, MAX_PREFERRED_LENGTH,
EntriesRead, TotalEntries, ServerType, PDomainUnicode,
ResumeHandle);
if (errCode <>NERR_SUCCESS) then
slServerNames.Add('Can''t enumerate servers!')
else begin
try
ServerInfo := Buffer;
for i := 1 to EntriesRead do
begin
slServerNames.Add(ServerInfo^.name);
Inc(ServerInfo);
end;
if slServerNames.Count = 0 then
slServerNames.Add('No servers available!');
finally
NetApiBufferFree(Buffer);
Result := slServerNames;
end; // end of try finally
end;
end;
 
 

Re:Find MS SQL Server on a network

Thanks!
"Brian Bushay TeamB" <XXXX@XXXXX.COM>escreveu na mensagem
Quote

>I want to know how i find in a network all sql servers. Users must may
have
>the option to select which server they want to connect. I find an exemple
>but i does not work on W98 because it use netapi32.dll.

Well if you have SQLDMO installed you can use code like this

var
dmo, servers: OleVariant;
i: Integer;
begin
dmo := CreateOleObject('SQLDMO.Application');
servers := dmo.ListAvailableSQlServers;
for i := 1 to servers.Count do
yourStringList.Add(servers.Item(i));
servers := UnAssigned;
dmo := UnAssigned;
end;

www.delphi32.co.yu/preuzeti_tekstovi/exploiting_sql_server_1.htm

--
Brian Bushay (TeamB)
XXXX@XXXXX.COM