Retrieve All SQL Server Name....

        I'm on project concern of programming SQL Server Application utilizing Delphi 5.0 as a complier. What I just want to know is How to retrieve all SQL Server Name and have it shown in Listbox on my form...for their convenience... I dont' want them to ask Administrator or to remember SQL Server name without necessary. Just like when I double click on TADOConnection and select provider to be OLE SQL Server then SQL Server Name Shown on ListBox above.
         Show me some code to do that, I'll reward you my points...
LVL 1
DOOM_DAEMONAsked:
Who is Participating?
 
BartonMConnect With a Mentor Commented:
Hi Doom Daemon,

put this code in a unit:

unit uDomainInfo;

interface

uses
 Windows, Classes, Sysutils;

const
   NetApi32 = 'netapi32.dll';
   ERROR_SUCCESS = 0;
   ERROR_MORE_DATA = 234;
   SV_TYPE_SERVER = $2;//   'Server type mask, all types of servers
   SIZE_SI_101 = 24;

type

   PSERVER_INFO_101 = ^SERVER_INFO_101;

   SERVER_INFO_101 = record
     dwPlatformId : integer;
     lpszServerName: LPWSTR;
     dwVersionMajor: integer;
     dwVersionMinor: integer;
     dwType: integer;
     lpszComment: LPWSTR;
  end;


//function NetApiBufferFree(BufPtr : pointer): integer; stdcall;
procedure GetServers(filter : Dword; const domain : string; list : TStrings);
Var
NetAPI: THandle;

_NetServerEnum: function(serverName : PWideChar; level : Integer;
   var BufPtr : Pointer; prefMaxLen : Integer; var entriesRead,
   totalEntries : Integer; servertype : Integer; domain : PWideChar;
   var resume_handle : Integer) : integer; stdcall;

_NetApiBufferFree: function(BufPtr : pointer): integer; stdcall;

_SQLBrowseConnect: function(SQLHDBCConnectionHandle: THandle;
InConnectionString: string; StringLength1: integer;
OutConnectionString: PWideChar;
BufferLength: integer;
StringLength2Ptr: integer) :integer; stdcall;
implementation


procedure GetServers(filter : Dword; const domain : string; list : TStrings);
var
 wServerName : WideString;
 wDomain : WideString;
 pwDomain : PWideChar;
 rv : Dword;
 p : PSERVER_INFO_101;
 buffer : pointer;
 i, entriesRead,
 totalEntries, MAX_PREFERRED_LENGTH,
 NIL_HANDLE : Integer;
 ServerName : string;

begin
 list.Clear;
 ServerName := '';
 wServerName := ServerName;
 MAX_PREFERRED_LENGTH := -1;
 wDomain := domain;
 if domain = '' then
   pwDomain := Nil
 else
   pwDomain := PWideChar (wDomain);
 NetAPI := SafeLoadLibrary(NetApi32);
 if NetAPI <> 0 then
 begin
   @_NetServerEnum := GetProcAddress(NetAPI, 'NetServerEnum');
   @_NetAPIBufferFree := GetProcAddress(NetAPI, 'NetApiBufferFree');
 end
 else
   Exit;
 rv := _NetServerEnum (PWideChar (wServerName), 101, buffer, MAX_PREFERRED_LENGTH,
                     entriesRead,  totalEntries, filter, pwDomain, NIL_HANDLE);
 if rv = ERROR_SUCCESS then
   try
     p := PSERVER_INFO_101 (buffer);
     for i := 0 to entriesRead - 1 do
     begin
       list.AddObject (p^.lpszServerName , TObject ( p^.dwPlatformId));
       Inc(p)
     end
   finally
     _NetAPIBufferFree(buffer);
   end
 else
   raise Exception.CreateRes(rv);
end;

end.

And use this code to fill a combobox with the servernames:

procedure x
var lst : TStringList;
begin

  lst := TStringList.Create;
  GetServers($4, DOMAINNAME', lst);
  ComboBox1.Items := lst;
end;

BartonM
0
 
simonetCommented:
following...
(nice code!)
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20110714.html
http://www.experts-exchange.com/questions/Q.20173874.html
http://www.experts-exchange.com/questions/Q.20170377.html
http://www.experts-exchange.com/questions/Q.20178176.html
http://www.experts-exchange.com/questions/Q.20178177.html
http://www.experts-exchange.com/questions/Q.20229473.html
http://www.experts-exchange.com/questions/Q.20279642.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20220086.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 
DOOM_DAEMONAuthor Commented:
    Sorry for took it so long,... I've been aboard and so busy....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.