mysql服务的注册,启动、停止、注销。 [delphi代码实现]
mysql服务的注册,启动、停止、注销。
unit Service;
interface
uses Windows,Classes,SysUtils,Winsvc,winsock;
Type
{服务句柄信息}
TScmInfo=Record
HostName :String;
DataBaseName:String;
DesireAccess :DWORD;
End;
{服务信息}
TSvcInfo=Record
HscManager :Integer;
ServerName :String; //服务名
DisplayName :String; //服务的显示名
DesireAccess :DWORD; //
ServiceType :Dword; //服务的类别
StartType :Dword; //服务的启动方式
ErrorControl :Dword; //服务错误控制方式
BinaryPathName :String; //服务执行文件的具体路径
LoadOrderGroup :String;
TagId :Dword;
Dependencies :String; //依赖:有多个依赖的服务时,中间以空格隔开}
ServerStartName :String;
Password :String;
End;
//需要注册的服务信息数组
SvcArray=Array[1..20] of TSvcInfo;
Type
{服务控制类}
TService=Class(TObject)
Private
Scm_Info :TScmInfo;
Svc_Info :TSvcInfo;
ScmHandle :Integer;
{服务句柄}
SvcHandle :Integer;
Public
Constructor Create();
Destructor Destroy();Override;
Function SetScmInfo():Boolean;
Function OpenScmHandle():Integer;
Function SetServiceInfo(TmpSvc :TsvcInfo):Boolean;
{建立一服务}
Function CreateService0():Boolean;
{删除一服务}
Function Delete_AService(ServiceName :String):Boolean;
{一服务}
Function Open_Service(ServiceName :String):Boolean;
{/////////////////////////////////////////////////////////////////////}
{// 函数名称: IsSvcExists()
{// 函数功能: 判断系统中相关的服务是否存在
{// 参数信息: 需要判断的服务名
{// 返回值: TRUE : WINDOWS 中的服务已存在,FALSE :WINDOWS 中的服务不存在
{//
{/////////////////////////////////////////////////////////////////////}
Function isSvcExists(ServiceName :string):Boolean;
{/////////////////////////////////////////////////////////////////////////}
{// 函数名称: Start_Service() Control_Service
{// 函数功能: 启动相关的服务进程
{// 参数信息: 需要启动的服务名
{// 返回值: TRUE :服务已经启动;FALSE:服务启动失败;
{/////////////////////////////////////////////////////////////////////////}
Function Control_Service(ServiceName : String; controlType :integer):Boolean;
{/////////////////////////////////////////////////////////////////////////}
{// 函数名称: IsServiceStart()
{// 函数功能: 判断服务是否启动
{// 参数信息: 需要判断的服务名
{// 返回值: TRUE :服务已经启动; FALSE :服务已停止
{/////////////////////////////////////////////////////////////////////////}
Function IsServiceStart(ServiceName:String):Boolean;
End;
implementation
{ TService }
constructor TService.Create;
begin
try
ScmHandle :=0;
SetScmInfo();
ScmHandle:=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
Except
End;
end;
{建立一服务}
function TService.CreateService0(): Boolean;
Var i :Integer;
b:SC_HANDLE;
begin
Try
OpenScmHandle();
// svc_info.HscManager := ScmHandle;
svc_info.HscManager:= OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);
Result :=False;
// SvcHandle:=
b:=CreateService(svc_Info.HscManager,
Pchar(svc_Info.ServerName),
Pchar(Svc_Info.DisplayName),
SERVICE_ALL_ACCESS, // Svc_info.DesireAccess
SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS, // svc_INfo.ServiceType,
SERVICE_AUTO_START, // svc_INfo.ServiceType,
SERVICE_ERROR_NORMAL, // Svc_Info.ErrorControl,
pchar(Svc_Info.BinaryPathName),
nil,nil,nil,nil,nil);
{ //原形
b:=CreateService(svc_Info.HscManager,
Pchar(svc_Info.ServerName),
Pchar(Svc_Info.DisplayName),
Svc_info.DesireAccess,
svc_INfo.ServiceType,
Svc_Info.StartType ,
Svc_Info.ErrorControl,
pchar(Svc_Info.BinaryPathName),
pchar(''),
LPDWORD(0),
Pchar(Svc_Info.Dependencies),
Pchar(Svc_info.ServerStartName),
Pchar(Svc_Info.Password)); }
// If SvcHandle <> 0 Then
If b <> 0 Then
Begin
Result :=True;
CloseServiceHandle(SvcHandle);
End ;
I:=i+1;
Except
End;
end;
{注销一服务}
function TService.Delete_AService(ServiceName: String): Boolean;
Var RHandle:Integer; rc :Boolean;
begin
Try
OpenScmHandle();
Result :=False;
If ScmHandle<>0 Then
Begin
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
Rc :=DeleteService(Rhandle);
Result :=Rc;
{关闭句柄}
CloseServiceHandle(RHandle);
End Else Result :=True;
End;
Except
End;
end;
destructor TService.Destroy;
begin
try
CloseServiceHandle(ScmHandle);
Except
End;
end;
{打开数据库的句柄}
function TService.isSvcExists(ServiceName: string): Boolean;
Var RHandle:Integer;
rc :Boolean;
begin
Try
OpenScmHandle();
Result :=False;
If ScmHandle<>0 Then
Begin
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
Result :=true;
{关闭句柄}
CloseServiceHandle(RHandle);
End;
End;
Except
End;
end;
function TService.OpenScmHandle: Integer;
begin
try
SetScmInfo();
if ScmHandle <>0 Then CloseServiceHandle(ScmHandle);
ScmHandle := 0;
ScmHandle :=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
if ScmHandle = 0 then
ScmHandle :=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
Except
End;
end;
{打开一服务}
function TService.Open_Service(ServiceName: String): Boolean;
Var
SHandle :Integer;
begin
Try
Result :=False;
sHandle := 0;
SHandle :=OpenScmHandle();
If SHandle<>0 Then
Begin
SvcHandle :=OpenService(SHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If SvcHandle <>0 Then Result :=True;
End;
Except
End;
end;
function TService.SetScmInfo(): Boolean;
Var
hostName : array [0..255] of char;
wsdata : TWSAData;
begin
try
WSAStartup ($0101, wsdata);
Result :=False;
gethostname (hostName,sizeof(hostName));
If HostName ='' Then
Exit;
scm_Info.HostName :=HostName;
Scm_Info.DesireAccess :=SC_MANAGER_ALL_ACCESS;
Except
End;
end;
//设置需要注册的WINDOWS 服务的具体信息
function TService.SetServiceInfo(TmpSvc: TsvcInfo): Boolean;
begin
try
Result :=False;
If ScmHandle >0 Then
Begin
Svc_Info :=TmpSvc;
Svc_Info.HscManager :=ScmHandle;
Result :=True;
End;
Except
End;
end;
//控制WINDOWS 系统内部的服务
function TService.Control_Service(ServiceName: String;controlType : Integer): Boolean;
Var
RHandle:Integer;
rc :Boolean;
Rec_status :TServiceStatus;
serviceArg :Pchar;
i :Integer;
begin
i :=0;
serviceArg := Pchar('');
Try
Result :=False;
If ScmHandle <>0 Then
Begin
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
Case controlType of
1: //启动服务器
Begin
//ControlService(rHandle,SERVICE_CONTROL_START,rec_status);
StartService(rHandle,0,servicearg);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_RUNNING then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
sleep(1000);
i:=i+1;
End;
End;
2: //暂停服务进程
Begin
ControlService(rHandle,SERVICE_CONTROL_PAUSE,rec_status);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_PAUSED then
Begin
result :=true;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
3: //唤醒服务进程
Begin
ControlService(rHandle,SERVICE_CONTROL_CONTINUE,rec_status);
i:=0;
While i<30 do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_RUNNING then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
4: //停止服务进程
begin
ControlService(rHandle,SERVICE_CONTROL_STOP,rec_status);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_STOPPED then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
5: //关闭服务程序 SERVICE_CONTROL_SHUTDOWN
Begin
rec_status.dwCurrentState :=SERVICE_RUNNING;
ControlService(rHandle,SERVICE_CONTROL_STOP,rec_status);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_STOPPED then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
end;
Result :=true;
{关闭句柄}
if rHandle <> 0 then
CloseServiceHandle(RHandle);
End;
End;
Except
End;
end;
function TService.IsServiceStart(ServiceName: String): Boolean;
Var
RHandle:Integer;
rc :Boolean;
Rec_status :TServiceStatus;
begin
Try
OpenScmHandle();
Result :=False;
If ScmHandle<>0 Then
Begin
RHandle := 0;
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_RUNNING then
result :=True;
{关闭句柄}
CloseServiceHandle(RHandle);
End;
End;
Except
End;
end;
end.
程序中使用
uses Service;
///***********************************
//注册 必须把my.ini放在 mysqld-nt.exe路径或者 系统windows 下
procedure TForm1.Button1Click(Sender: TObject);
var s:TService;
a:TScmInfo ;
b:TSvcInfo;
begin
s:=TService.Create;
b.ServerName :='mysql';
b.DisplayName :='sdfss';
b.BinaryPathName :='D:\APMXE5\mysql60\bin\mysqld-nt.exe'; //路径和exe文件
s.SetScmInfo ;
s.SetServiceInfo(b);
s.CreateService0;
s.Destroy ;
end;
//******************
//运行
procedure TForm1.Button2Click(Sender: TObject);
var s:TService;
begin
s:=TService.Create;
s.Control_Service('mysql',1) ;
s.Destroy ;
end;
//**************
//停止
var s:TService;
begin
s:=TService.Create;
s.Control_Service('mysql',4) ;
s.Destroy ;
end;
//**********************
注销
procedure TForm1.Button4Click(Sender: TObject);
var s:TService;
begin
s:=TService.Create;
s.Delete_AService('mysql') ;
s.Destroy ;
end;
unit Service;
interface
uses Windows,Classes,SysUtils,Winsvc,winsock;
Type
{服务句柄信息}
TScmInfo=Record
HostName :String;
DataBaseName:String;
DesireAccess :DWORD;
End;
{服务信息}
TSvcInfo=Record
HscManager :Integer;
ServerName :String; //服务名
DisplayName :String; //服务的显示名
DesireAccess :DWORD; //
ServiceType :Dword; //服务的类别
StartType :Dword; //服务的启动方式
ErrorControl :Dword; //服务错误控制方式
BinaryPathName :String; //服务执行文件的具体路径
LoadOrderGroup :String;
TagId :Dword;
Dependencies :String; //依赖:有多个依赖的服务时,中间以空格隔开}
ServerStartName :String;
Password :String;
End;
//需要注册的服务信息数组
SvcArray=Array[1..20] of TSvcInfo;
Type
{服务控制类}
TService=Class(TObject)
Private
Scm_Info :TScmInfo;
Svc_Info :TSvcInfo;
ScmHandle :Integer;
{服务句柄}
SvcHandle :Integer;
Public
Constructor Create();
Destructor Destroy();Override;
Function SetScmInfo():Boolean;
Function OpenScmHandle():Integer;
Function SetServiceInfo(TmpSvc :TsvcInfo):Boolean;
{建立一服务}
Function CreateService0():Boolean;
{删除一服务}
Function Delete_AService(ServiceName :String):Boolean;
{一服务}
Function Open_Service(ServiceName :String):Boolean;
{/////////////////////////////////////////////////////////////////////}
{// 函数名称: IsSvcExists()
{// 函数功能: 判断系统中相关的服务是否存在
{// 参数信息: 需要判断的服务名
{// 返回值: TRUE : WINDOWS 中的服务已存在,FALSE :WINDOWS 中的服务不存在
{//
{/////////////////////////////////////////////////////////////////////}
Function isSvcExists(ServiceName :string):Boolean;
{/////////////////////////////////////////////////////////////////////////}
{// 函数名称: Start_Service() Control_Service
{// 函数功能: 启动相关的服务进程
{// 参数信息: 需要启动的服务名
{// 返回值: TRUE :服务已经启动;FALSE:服务启动失败;
{/////////////////////////////////////////////////////////////////////////}
Function Control_Service(ServiceName : String; controlType :integer):Boolean;
{/////////////////////////////////////////////////////////////////////////}
{// 函数名称: IsServiceStart()
{// 函数功能: 判断服务是否启动
{// 参数信息: 需要判断的服务名
{// 返回值: TRUE :服务已经启动; FALSE :服务已停止
{/////////////////////////////////////////////////////////////////////////}
Function IsServiceStart(ServiceName:String):Boolean;
End;
implementation
{ TService }
constructor TService.Create;
begin
try
ScmHandle :=0;
SetScmInfo();
ScmHandle:=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
Except
End;
end;
{建立一服务}
function TService.CreateService0(): Boolean;
Var i :Integer;
b:SC_HANDLE;
begin
Try
OpenScmHandle();
// svc_info.HscManager := ScmHandle;
svc_info.HscManager:= OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);
Result :=False;
// SvcHandle:=
b:=CreateService(svc_Info.HscManager,
Pchar(svc_Info.ServerName),
Pchar(Svc_Info.DisplayName),
SERVICE_ALL_ACCESS, // Svc_info.DesireAccess
SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS, // svc_INfo.ServiceType,
SERVICE_AUTO_START, // svc_INfo.ServiceType,
SERVICE_ERROR_NORMAL, // Svc_Info.ErrorControl,
pchar(Svc_Info.BinaryPathName),
nil,nil,nil,nil,nil);
{ //原形
b:=CreateService(svc_Info.HscManager,
Pchar(svc_Info.ServerName),
Pchar(Svc_Info.DisplayName),
Svc_info.DesireAccess,
svc_INfo.ServiceType,
Svc_Info.StartType ,
Svc_Info.ErrorControl,
pchar(Svc_Info.BinaryPathName),
pchar(''),
LPDWORD(0),
Pchar(Svc_Info.Dependencies),
Pchar(Svc_info.ServerStartName),
Pchar(Svc_Info.Password)); }
// If SvcHandle <> 0 Then
If b <> 0 Then
Begin
Result :=True;
CloseServiceHandle(SvcHandle);
End ;
I:=i+1;
Except
End;
end;
{注销一服务}
function TService.Delete_AService(ServiceName: String): Boolean;
Var RHandle:Integer; rc :Boolean;
begin
Try
OpenScmHandle();
Result :=False;
If ScmHandle<>0 Then
Begin
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
Rc :=DeleteService(Rhandle);
Result :=Rc;
{关闭句柄}
CloseServiceHandle(RHandle);
End Else Result :=True;
End;
Except
End;
end;
destructor TService.Destroy;
begin
try
CloseServiceHandle(ScmHandle);
Except
End;
end;
{打开数据库的句柄}
function TService.isSvcExists(ServiceName: string): Boolean;
Var RHandle:Integer;
rc :Boolean;
begin
Try
OpenScmHandle();
Result :=False;
If ScmHandle<>0 Then
Begin
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
Result :=true;
{关闭句柄}
CloseServiceHandle(RHandle);
End;
End;
Except
End;
end;
function TService.OpenScmHandle: Integer;
begin
try
SetScmInfo();
if ScmHandle <>0 Then CloseServiceHandle(ScmHandle);
ScmHandle := 0;
ScmHandle :=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
if ScmHandle = 0 then
ScmHandle :=OpenSCManager(PChar(Scm_Info.HostName),Nil,Scm_Info.DesireAccess);
Except
End;
end;
{打开一服务}
function TService.Open_Service(ServiceName: String): Boolean;
Var
SHandle :Integer;
begin
Try
Result :=False;
sHandle := 0;
SHandle :=OpenScmHandle();
If SHandle<>0 Then
Begin
SvcHandle :=OpenService(SHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If SvcHandle <>0 Then Result :=True;
End;
Except
End;
end;
function TService.SetScmInfo(): Boolean;
Var
hostName : array [0..255] of char;
wsdata : TWSAData;
begin
try
WSAStartup ($0101, wsdata);
Result :=False;
gethostname (hostName,sizeof(hostName));
If HostName ='' Then
Exit;
scm_Info.HostName :=HostName;
Scm_Info.DesireAccess :=SC_MANAGER_ALL_ACCESS;
Except
End;
end;
//设置需要注册的WINDOWS 服务的具体信息
function TService.SetServiceInfo(TmpSvc: TsvcInfo): Boolean;
begin
try
Result :=False;
If ScmHandle >0 Then
Begin
Svc_Info :=TmpSvc;
Svc_Info.HscManager :=ScmHandle;
Result :=True;
End;
Except
End;
end;
//控制WINDOWS 系统内部的服务
function TService.Control_Service(ServiceName: String;controlType : Integer): Boolean;
Var
RHandle:Integer;
rc :Boolean;
Rec_status :TServiceStatus;
serviceArg :Pchar;
i :Integer;
begin
i :=0;
serviceArg := Pchar('');
Try
Result :=False;
If ScmHandle <>0 Then
Begin
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
Case controlType of
1: //启动服务器
Begin
//ControlService(rHandle,SERVICE_CONTROL_START,rec_status);
StartService(rHandle,0,servicearg);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_RUNNING then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
sleep(1000);
i:=i+1;
End;
End;
2: //暂停服务进程
Begin
ControlService(rHandle,SERVICE_CONTROL_PAUSE,rec_status);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_PAUSED then
Begin
result :=true;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
3: //唤醒服务进程
Begin
ControlService(rHandle,SERVICE_CONTROL_CONTINUE,rec_status);
i:=0;
While i<30 do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_RUNNING then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
4: //停止服务进程
begin
ControlService(rHandle,SERVICE_CONTROL_STOP,rec_status);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_STOPPED then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
5: //关闭服务程序 SERVICE_CONTROL_SHUTDOWN
Begin
rec_status.dwCurrentState :=SERVICE_RUNNING;
ControlService(rHandle,SERVICE_CONTROL_STOP,rec_status);
i:=0;
While i<30 Do
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_STOPPED then
Begin
result :=True;
CloseServiceHandle(RHandle);
Break;
End;
Sleep(1000);
i:=i+1;
End;
End;
end;
Result :=true;
{关闭句柄}
if rHandle <> 0 then
CloseServiceHandle(RHandle);
End;
End;
Except
End;
end;
function TService.IsServiceStart(ServiceName: String): Boolean;
Var
RHandle:Integer;
rc :Boolean;
Rec_status :TServiceStatus;
begin
Try
OpenScmHandle();
Result :=False;
If ScmHandle<>0 Then
Begin
RHandle := 0;
RHandle :=OpenService(ScmHandle,PChar(ServiceName),SERVICE_ALL_ACCESS );
If RHandle <>0 Then
Begin
QueryServiceStatus(RHandle,Rec_status);
if rec_status.dwCurrentState =SERVICE_RUNNING then
result :=True;
{关闭句柄}
CloseServiceHandle(RHandle);
End;
End;
Except
End;
end;
end.
程序中使用
uses Service;
///***********************************
//注册 必须把my.ini放在 mysqld-nt.exe路径或者 系统windows 下
procedure TForm1.Button1Click(Sender: TObject);
var s:TService;
a:TScmInfo ;
b:TSvcInfo;
begin
s:=TService.Create;
b.ServerName :='mysql';
b.DisplayName :='sdfss';
b.BinaryPathName :='D:\APMXE5\mysql60\bin\mysqld-nt.exe'; //路径和exe文件
s.SetScmInfo ;
s.SetServiceInfo(b);
s.CreateService0;
s.Destroy ;
end;
//******************
//运行
procedure TForm1.Button2Click(Sender: TObject);
var s:TService;
begin
s:=TService.Create;
s.Control_Service('mysql',1) ;
s.Destroy ;
end;
//**************
//停止
var s:TService;
begin
s:=TService.Create;
s.Control_Service('mysql',4) ;
s.Destroy ;
end;
//**********************
注销
procedure TForm1.Button4Click(Sender: TObject);
var s:TService;
begin
s:=TService.Create;
s.Delete_AService('mysql') ;
s.Destroy ;
end;