Salvador
Salvador

Reputation: 16472

How can retrieve the SQL Server Protocols Network Configuration?

I want to retrieve the SQL Server Protocols Network Configuration.

enter image description here

I tried searching for a windows registry or any config file related to this, but the only sample which I found is for powershell Enable or Disable a Server Network Protocol (SQL Server PowerShell), and uses .Net classes.

So the question is : How can retrieve the SQL Server Protocols Network Configuration using delphi?

Upvotes: 2

Views: 223

Answers (1)

RRUZ
RRUZ

Reputation: 136391

You can use the ServerNetworkProtocol WMI class and also connect to the correct namespace depending of the SQL Server Version.

  • SQL Server 2005 = ComputerManagement
  • SQL Server 2008 = ComputerManagement10
  • SQL Server 2012 = ComputerManagement11

Try this sample

{$APPTYPE CONSOLE}


uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

type
  TMSSQlServerType=(MSSQL2005, MSSQL2008, MSSQL2012);


procedure  GetServerNetworkProtocolInfo(SQlServerType: TMSSQlServerType; const InstanceName: string);
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
  MSSQLNameSpaces  : array [TMSSQlServerType] of string = ('ComputerManagement','ComputerManagement10','ComputerManagement11');
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, Format('root\Microsoft\SqlServer\%s',[MSSQLNameSpaces[SQlServerType]]), WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM ServerNetworkProtocol Where InstanceName="%s"', [InstanceName]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    Writeln(Format('Enabled                        %s',[String(FWbemObject.Enabled)]));// Boolean
    Writeln(Format('MultiIpConfigurationSupport    %s',[String(FWbemObject.MultiIpConfigurationSupport)]));// Boolean
    Writeln(Format('ProtocolDisplayName            %s',[String(FWbemObject.ProtocolDisplayName)]));// String
    Writeln(Format('ProtocolName                   %s',[String(FWbemObject.ProtocolName)]));// String
    Writeln;
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetServerNetworkProtocolInfo(MSSQL2008,'MSSQLSERVER');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

Upvotes: 5

Related Questions