Imbeginner
asked on
Shut down/Stop/Running/Restarting IIS 5 From Delphi 7 ?
Hi,
Do this work available for our administrators To
Shut down/stop/restart/start +ing IIS 5 From his application ?
OS:windows 2000 advanced server
Thanks in advance for your Helps.
Do this work available for our administrators To
Shut down/stop/restart/start +ing IIS 5 From his application ?
OS:windows 2000 advanced server
Thanks in advance for your Helps.
ASKER
one problem
when you start service, you can t say that your web site is started too.your coding only fire servicing for a special service.
when you start service, you can t say that your web site is started too.your coding only fire servicing for a special service.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
workshop_alex:
hi, I used delphi7 for my works,
I think this program has some errors in WindowProc Function
On these lines
1-
line-196
else if (uMsg = WM_DESTROY) then begin
StopApp := True; <---- left side cannot be assigned
2-
line-217
ServiceWndClass.lpfnWndPro c := @WindowProc; <---- left side can not be assigned.
___________________
another thing is:
suppose that the IIS was started,sometimes when we openend iis manager in administrator
folder we saw that our default web site was not started(remain in stopped state) ,
do any way existed for starting my web default site or restarting IIS ?
hi, I used delphi7 for my works,
I think this program has some errors in WindowProc Function
On these lines
1-
line-196
else if (uMsg = WM_DESTROY) then begin
StopApp := True; <---- left side cannot be assigned
2-
line-217
ServiceWndClass.lpfnWndPro
___________________
another thing is:
suppose that the IIS was started,sometimes when we openend iis manager in administrator
folder we saw that our default web site was not started(remain in stopped state) ,
do any way existed for starting my web default site or restarting IIS ?
unit ServiceManager;
interface
uses Windows, Sysutils, WinSvc;
{
this class requires the following access rights :
SERVICE_CHANGE_CONFIG
SERVICE_START
SERVICE_STOP
SC_MANAGER_CONNECT
SC_MANAGER_ENUMERATE_SERVI CE
SC_MANAGER_QUERY_LOCK_STAT US
you must be an administrator
}
type
TServiceManager = class (TObject)
private
FServiceHandle: SC_Handle;
FServiceManagerHandle: Int64;
FServiceName: string;
procedure SetServiceName(Value: string);
public
constructor Create;
destructor Destroy; override;
procedure SetServiceDescription(desc ription: String);
procedure StartService;
procedure StopService;
property ServiceName: string read FServiceName write SetServiceName;
end;
LPSERVICE_DESCRIPTIONA = ^SERVICE_DESCRIPTIONA;
{$EXTERNALSYM LPSERVICE_DESCRIPTIONA}
_SERVICE_DESCRIPTIONA = record
lpDescription: LPSTR;
end;
{$EXTERNALSYM _SERVICE_DESCRIPTIONA}
SERVICE_DESCRIPTIONA = _SERVICE_DESCRIPTIONA;
{$EXTERNALSYM SERVICE_DESCRIPTIONA}
TServiceDescriptionA = SERVICE_DESCRIPTIONA;
PServiceDescriptionA = LPSERVICE_DESCRIPTIONA;
//
// Service description string
//
LPSERVICE_DESCRIPTIONW = ^SERVICE_DESCRIPTIONW;
{$EXTERNALSYM LPSERVICE_DESCRIPTIONW}
_SERVICE_DESCRIPTIONW = record
lpDescription: LPWSTR;
end;
{$EXTERNALSYM _SERVICE_DESCRIPTIONW}
SERVICE_DESCRIPTIONW = _SERVICE_DESCRIPTIONW;
{$EXTERNALSYM SERVICE_DESCRIPTIONW}
TServiceDescriptionW = SERVICE_DESCRIPTIONW;
PServiceDescriptionW = LPSERVICE_DESCRIPTIONW;
{$IFDEF UNICODE}
SERVICE_DESCRIPTION = SERVICE_DESCRIPTIONW;
{$EXTERNALSYM SERVICE_DESCRIPTION}
LPSERVICE_DESCRIPTION = LPSERVICE_DESCRIPTIONW;
{$EXTERNALSYM LPSERVICE_DESCRIPTION}
TServiceDescription = TServiceDescriptionW;
PServiceDescription = PServiceDescriptionW;
{$ELSE}
SERVICE_DESCRIPTION = SERVICE_DESCRIPTIONA;
{$EXTERNALSYM SERVICE_DESCRIPTION}
LPSERVICE_DESCRIPTION = LPSERVICE_DESCRIPTIONA;
{$EXTERNALSYM LPSERVICE_DESCRIPTION}
TServiceDescription = TServiceDescriptionA;
PServiceDescription = PServiceDescriptionA;
{$ENDIF}
LPVOID = Pointer;
{$EXTERNALSYM LPVOID}
const
SERVICE_CONFIG_DESCRIPTION = 1;
{$EXTERNALSYM SERVICE_CONFIG_DESCRIPTION }
implementation
{
************************** ***** TServiceManager ************************** ******
}
constructor TServiceManager.Create;
var
sMachine: string;
begin
FServiceName := '';
FServiceHandle := 0;
sMachine := ''; // localhost is used when empty
FServiceManagerHandle := OpenSCManager(PChar(sMachi ne), Nil, SC_MANAGER_CONNECT);
end;
destructor TServiceManager.Destroy;
begin
if FServiceHandle <> 0 then
CloseServiceHandle(FServic eHandle);
CloseServiceHandle(FServic eManagerHa ndle);
inherited;
end;
procedure TServiceManager.SetService Descriptio n(descript ion: String);
type
TFnc_ChangeServiceConfig2 = function(hService: SC_HANDLE; dwInfoLevel: DWORD;
lpInfo: LPVOID): BOOL; stdcall;
var
srvDesc: TServiceDescription;
csc2: TFnc_ChangeServiceConfig2;
dllHandle: THandle;
begin
if FServiceHandle = 0 then begin
Raise Exception.Create('TService Manager.Se tServiceDe scription: Service not selected');
Exit;
end;
srvDesc.lpDescription := pChar(Description);
{ this ChangeServiceConfigA doesn't exist on NT 4 and lover}
dllHandle := 0;
try
dllHandle := LoadLibrary('advapi32');
@csc2 := GetProcAddress(dllHandle, 'ChangeServiceConfig2A')
except
csc2 := nil;
end;
if Assigned(csc2) then begin
try
csc2(FServiceHandle, SERVICE_CONFIG_DESCRIPTION , @srvDesc);
except
end;
end;
try
FreeLibrary(dllHandle);
except
end;
end;
procedure TServiceManager.SetService Name(Value : string);
begin
if Value = FServiceName then
Exit;
FServiceName := Value;
if FServiceHandle <> 0 then
CloseServiceHandle(FServic eHandle);
FServiceHandle := OpenService(FServiceManage rHandle, PChar(FServiceName),
SERVICE_CHANGE_CONFIG or SERVICE_START or SERVICE_STOP);
end;
procedure TServiceManager.StartServi ce;
var
dummy: PChar;
begin
if FServiceHandle = 0 then begin
Raise Exception.Create('TService Manager.St artService : Service not selected');
Exit;
end;
winsvc.StartService(FServi ceHandle, 0, dummy);
end;
procedure TServiceManager.StopServic e;
var
stat: TServiceStatus;
begin
if FServiceHandle = 0 then begin
Raise Exception.Create('TService Manager.St opService: Service not selected');
Exit;
end;
ControlService(FServiceHan dle, SERVICE_CONTROL_STOP, stat);
end;
end.
hope it helped...
interface
uses Windows, Sysutils, WinSvc;
{
this class requires the following access rights :
SERVICE_CHANGE_CONFIG
SERVICE_START
SERVICE_STOP
SC_MANAGER_CONNECT
SC_MANAGER_ENUMERATE_SERVI
SC_MANAGER_QUERY_LOCK_STAT
you must be an administrator
}
type
TServiceManager = class (TObject)
private
FServiceHandle: SC_Handle;
FServiceManagerHandle: Int64;
FServiceName: string;
procedure SetServiceName(Value: string);
public
constructor Create;
destructor Destroy; override;
procedure SetServiceDescription(desc
procedure StartService;
procedure StopService;
property ServiceName: string read FServiceName write SetServiceName;
end;
LPSERVICE_DESCRIPTIONA = ^SERVICE_DESCRIPTIONA;
{$EXTERNALSYM LPSERVICE_DESCRIPTIONA}
_SERVICE_DESCRIPTIONA = record
lpDescription: LPSTR;
end;
{$EXTERNALSYM _SERVICE_DESCRIPTIONA}
SERVICE_DESCRIPTIONA = _SERVICE_DESCRIPTIONA;
{$EXTERNALSYM SERVICE_DESCRIPTIONA}
TServiceDescriptionA = SERVICE_DESCRIPTIONA;
PServiceDescriptionA = LPSERVICE_DESCRIPTIONA;
//
// Service description string
//
LPSERVICE_DESCRIPTIONW = ^SERVICE_DESCRIPTIONW;
{$EXTERNALSYM LPSERVICE_DESCRIPTIONW}
_SERVICE_DESCRIPTIONW = record
lpDescription: LPWSTR;
end;
{$EXTERNALSYM _SERVICE_DESCRIPTIONW}
SERVICE_DESCRIPTIONW = _SERVICE_DESCRIPTIONW;
{$EXTERNALSYM SERVICE_DESCRIPTIONW}
TServiceDescriptionW = SERVICE_DESCRIPTIONW;
PServiceDescriptionW = LPSERVICE_DESCRIPTIONW;
{$IFDEF UNICODE}
SERVICE_DESCRIPTION = SERVICE_DESCRIPTIONW;
{$EXTERNALSYM SERVICE_DESCRIPTION}
LPSERVICE_DESCRIPTION = LPSERVICE_DESCRIPTIONW;
{$EXTERNALSYM LPSERVICE_DESCRIPTION}
TServiceDescription = TServiceDescriptionW;
PServiceDescription = PServiceDescriptionW;
{$ELSE}
SERVICE_DESCRIPTION = SERVICE_DESCRIPTIONA;
{$EXTERNALSYM SERVICE_DESCRIPTION}
LPSERVICE_DESCRIPTION = LPSERVICE_DESCRIPTIONA;
{$EXTERNALSYM LPSERVICE_DESCRIPTION}
TServiceDescription = TServiceDescriptionA;
PServiceDescription = PServiceDescriptionA;
{$ENDIF}
LPVOID = Pointer;
{$EXTERNALSYM LPVOID}
const
SERVICE_CONFIG_DESCRIPTION
{$EXTERNALSYM SERVICE_CONFIG_DESCRIPTION
implementation
{
**************************
}
constructor TServiceManager.Create;
var
sMachine: string;
begin
FServiceName := '';
FServiceHandle := 0;
sMachine := ''; // localhost is used when empty
FServiceManagerHandle := OpenSCManager(PChar(sMachi
end;
destructor TServiceManager.Destroy;
begin
if FServiceHandle <> 0 then
CloseServiceHandle(FServic
CloseServiceHandle(FServic
inherited;
end;
procedure TServiceManager.SetService
type
TFnc_ChangeServiceConfig2 = function(hService: SC_HANDLE; dwInfoLevel: DWORD;
lpInfo: LPVOID): BOOL; stdcall;
var
srvDesc: TServiceDescription;
csc2: TFnc_ChangeServiceConfig2;
dllHandle: THandle;
begin
if FServiceHandle = 0 then begin
Raise Exception.Create('TService
Exit;
end;
srvDesc.lpDescription := pChar(Description);
{ this ChangeServiceConfigA doesn't exist on NT 4 and lover}
dllHandle := 0;
try
dllHandle := LoadLibrary('advapi32');
@csc2 := GetProcAddress(dllHandle, 'ChangeServiceConfig2A')
except
csc2 := nil;
end;
if Assigned(csc2) then begin
try
csc2(FServiceHandle, SERVICE_CONFIG_DESCRIPTION
except
end;
end;
try
FreeLibrary(dllHandle);
except
end;
end;
procedure TServiceManager.SetService
begin
if Value = FServiceName then
Exit;
FServiceName := Value;
if FServiceHandle <> 0 then
CloseServiceHandle(FServic
FServiceHandle := OpenService(FServiceManage
SERVICE_CHANGE_CONFIG or SERVICE_START or SERVICE_STOP);
end;
procedure TServiceManager.StartServi
var
dummy: PChar;
begin
if FServiceHandle = 0 then begin
Raise Exception.Create('TService
Exit;
end;
winsvc.StartService(FServi
end;
procedure TServiceManager.StopServic
var
stat: TServiceStatus;
begin
if FServiceHandle = 0 then begin
Raise Exception.Create('TService
Exit;
end;
ControlService(FServiceHan
end;
end.
hope it helped...
I found this code on the web (http://www.elists.org/pipermail/delphi/2003-April/023621.html) and have tested it on 2K and XP and it seems to work quite well. (Used Messenger service as I do not run IIS)
You will need to include WinSvc.pas and the user must have the appropriate priveledges for this to work.
//------------------------
function IsServiceRunning(ServiceNa
var
ServerStatus : SERVICE_STATUS;
SCMHandle : SC_HANDLE;
QryResult : Integer;
begin
Result := False;
try
{ Open the Service Control Manager }
SCMHandle := OpenSCManager(nil, nil, GENERIC_EXECUTE);
if (SCMHandle <> 0) then
begin
{ Open and Query the specified Service }
QryResult := OpenService(SCMHandle,
ServiceName,SERVICE_QUERY_
QueryServiceStatus(QryResu
if (ServerStatus.dwCurrentSta
Result := True
else
Result := False;
CloseServiceHandle(QryResu
CloseServiceHandle(SCMHand
end
else
(*MessageDlg('Error opening Service Manager.', mtError, [mbOK], 0);*)
except
(*MessageDlg('Error checking status of service', mtError, [mbOK], 0);*)
end;
end;
//------------------------
function ServiceStart(sMachine, sService: String) : Boolean;
var
schm,
schs: SC_Handle;
ss: TServiceStatus;
psTemp: PChar;
dwChkP: DWord;
begin
ss.dwCurrentState := 0;
schm := OpenSCManager(PChar(sMachi
if (schm>0) then
begin
schs := OpenService(schm, PChar(sService), SERVICE_START or
SERVICE_QUERY_STATUS);
if (schs>0) then
begin
psTemp := nil;
if (StartService(schs, 0, psTemp)) then
if (QueryServiceStatus(schs, ss)) then
while (SERVICE_RUNNING<>ss.dwCur
begin
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
if (not QueryServiceStatus(schs, ss)) then
Break;
if (ss.dwCheckPoint < dwChkP) then
Break;
end;
CloseServiceHandle(schs);
end;
CloseServiceHandle(schm);
end;
Result := SERVICE_RUNNING=ss.dwCurre
end;
//------------------------
function ServiceStop(sMachine, sService: String) : Boolean;
var
schm,
schs: SC_Handle;
ss: TServiceStatus;
dwChkP: DWord;
begin
schm := OpenSCManager(PChar(sMachi
if (schm>0) then
begin
schs := OpenService(schm, PChar(sService), SERVICE_STOP or
SERVICE_QUERY_STATUS);
if (schs>0) then
begin
if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then
if (QueryServiceStatus(schs, ss)) then
while (SERVICE_STOPPED<>ss.dwCur
begin
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
if (not QueryServiceStatus(schs, ss)) then
Break;
if (ss.dwCheckPoint < dwChkP) then
Break;
end;
CloseServiceHandle(schs);
end;
CloseServiceHandle(schm);
end;
Result := SERVICE_STOPPED=ss.dwCurre
end;
Cheers.
Bedouin..