原来做过:
function GetSqlServerStatus(lpszComputerName: LPCTSTR): Integer;
var
ssStatus: SERVICE_STATUS;
dwOldCheckPoint: DWORD;
dwStartTickCount: DWORD;
dwWaitTime: DWORD;
dwStatus: DWORD;
lpszServiceName: LPCTSTR;
schSCManager: SC_HANDLE;
schService: SC_HANDLE;
begin
if (lpszComputerName <> nil) and
((StrComp(lpszComputerName, '127.0.0.1') = 0) or (StrComp(lpszComputerName, '.') = 0)) then
lpszComputerName := nil;
lpszServiceName := 'MSSQLServer';
schSCManager := OpenSCManager(
lpszComputerName, //Computer name
nil, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if schSCManager = 0 then
GetSqlServerStatus := -1; //Machine not exists
schService := OpenService(
schSCManager, // SCM database
lpszServiceName, // service name
SERVICE_ALL_ACCESS);
if schService = 0 then begin
CloseServiceHandle(schService);
GetSqlServerStatus := -2; //SqlServer Service not Exists
end;
if not QueryServiceStatus(
schService, // handle to service
ssStatus) then begin // address of status information structure
CloseServiceHandle(schService);
GetSqlServerStatus := -3; //MyErrorExit('QueryServiceStatus');
end;
Result := ssStatus.dwCurrentState;
end;
//---------------------------------------------------------------------------