delphi 怎么控制 mysql 的服务开启与关闭、重启(200)

  • 主题发起人 主题发起人 向红林
  • 开始时间 开始时间

向红林

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi 怎么控制 mysql 的服务开启与关闭、重启。我要自己做个 定时运行的mysql服务器与关闭。并增加ftp或者其他文件传输功能(此代码已有idyftp完成)
 
你自己再调试一下试试。uses ComCtrls;procedure TFormMain.ShowStat;var osc, os: SC_Handle; tmp: TServiceStatus;begin os := 0; osc := OpenSCManager(nil, nil, SC_MANAGER_ENUMERATE_SERVICE); if osc > 0 then os := OpenService(osc, PChar(SrvName), SERVICE_ALL_ACCESS); if os > 0 then begin QueryServiceStatus(os, tmp); if tmp.dwCurrentState = SERVICE_RUNNING then // 服务已启动 begin Done := True; actStopSrv.Enabled := True; actStartSrv.Enabled := False; lbStat.Caption := SrvName + SrvExpl + SrvState[1]; lbStat.Font.Color := clGreen; end end else begin Done := False; actStartSrv.Enabled := True; actStopSrv.Enabled := False; lbStat.Caption := SrvName + SrvExpl + SrvState[2]; lbStat.Font.Color := clRed; end;end;function ControlServer(sService: string; StartOrStop: Integer): Boolean;var schm,schs: SC_Handle; ss: TServiceStatus; tmp: PChar;begin try Result := False; schm := OpenSCManager(nil, nil, SC_MANAGER_CONNECT); // 打开服务控制管理 if schm > 0 then // > 0 则打开成功 begin schs := OpenService(schm, PChar(sService), SERVICE_ALL_ACCESS); if schs > 0 then begin case StartOrStop of 0: // Start Server begin tmp := nil; if StartService(schs, 0, tmp) then if QueryServiceStatus(schs, ss) then while (SERVICE_RUNNING <> ss.dwCurrentState) do begin Sleep(ss.dwWaitHint); if not QueryServiceStatus(schs, ss) then Break; if ss.dwCheckPoint <> 0 then Break; end; end; 1: // Stop Server begin if ControlService(schs, SERVICE_CONTROL_STOP, ss) then if QueryServiceStatus(schs, ss) then while (SERVICE_STOPPED <> ss.dwCurrentState) do begin Sleep(3000); if not QueryServiceStatus(schs, ss) then Break; if ss.dwCheckPoint <> 0 then Break; end; end; end;//case end; CloseServiceHandle(schs); end; CloseServiceHandle(schm); Result := True; end else begin ShowMessage('打开服务控制管理失败!'); Result := False; end; except On E:Exception do ShowMessage(E.Message); end;end;procedure TFormMain.StartSrv; // 启动MySQL服务begin memLog.Lines.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + ':正在启动服务...'); if ControlServer(SrvName, 0) then begin memLog.Lines.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + ':' + SrvName + '服务已经启动。'); Application.ProcessMessages; Done := True; actStopSrv.Enabled := True; actStartSrv.Enabled := False; end else memLog.Lines.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + ':无法启动' + SrvName + '服务,请稍后再试。');end;procedure TFormMain.StopSrv;begin memLog.Lines.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + ':正在停止服务...'); if ControlServer(SrvName,1) then begin memLog.Lines.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + ':' + SrvName + '服务已经停止。'); Application.ProcessMessages; Done := False; actStopSrv.Enabled := False; actStartSrv.Enabled := True; end else memLog.Lines.Add(FormatDateTime('yyyy-mm-dd hh:mm:ss', Now) + ':无法停止' + SrvName + '服务,请稍后再试。');end;
 
osc, os: SC_Handle; tmp: TServiceStatususes 了什么单元啊。如果 mysql是绿色的 不在服务内,怎么注册成服务,和反注册。。。。
 
已经搞定了 mysql服务的注册,启动、停止、注销。unit Service;interfaceuses 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); ExceptEnd;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;begins:=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;begins:=TService.Create;s.Control_Service('mysql',1) ;s.Destroy ;end;//**************//停止var s:TService;begins:=TService.Create;s.Control_Service('mysql',4) ;s.Destroy ;end;//**********************注销procedure TForm1.Button4Click(Sender: TObject);var s:TService;begins:=TService.Create;s.Delete_AService('mysql') ;s.Destroy ;end;
 
恭喜楼主。
 
LZ可以结贴了。
 
等等在研究怎么 另外建立 mysql的服务名称。。
 
可以在命令行完成,临时创建.bat文件,执行完就删除,服务名称任意定,可以运行多个mysql。安装:H:cd /Server/mysql/mysql-5.5.5/bin/H:/Server/mysql/mysql-5.5.5/bin/mysqld.exe --install mysql55 --defaults-file=H:/Server/mysql/mysql-5.5.5/my.ini卸载服务:H:cd /Server/mysql/mysql-5.5.5/bin/H:/Server/mysql/mysql-5.5.5/bin/mysqld.exe --remove mysql55启动服务:net start mysql55停止服务:net stop mysql55重启动服务:net stop mysql55net start mysql55
 
系统 禁止了 cmd.exe (cmd.exe改名了。不知道改了什么。)怎么运行 bat. (或者 cmd )。
 
安装、卸载服务可以用Shellexecute直接执行mysqld.exe,启动、停止服务参照楼上的方法,不过我没有试。
 
多人接受答案了。
 
后退
顶部