?? unit1.pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,WinSvc;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
cc:hwnd;
implementation
{$R *.dfm}
function ServiceGetStatus(sMachine, sService: PChar): DWORD;
{*** Parameters: ***}
{*** sService: specifies the name of the service to open
{*** sMachine: specifies the name of the target computer
{*** Return Values: ***}
{*** -1 = Error opening service ***}
{*** 1 = SERVICE_STOPPED ***}
{*** 2 = SERVICE_START_PENDING ***}
{*** 3 = SERVICE_STOP_PENDING ***}
{*** 4 = SERVICE_RUNNING ***}
{*** 5 = SERVICE_CONTINUE_PENDING ***}
{*** 6 = SERVICE_PAUSE_PENDING ***}
{*** 7 = SERVICE_PAUSED ***}
var
SCManHandle, SvcHandle: SC_Handle;
SS: TServiceStatus;
dwStat: DWORD;
begin
dwStat := 0;
// Open service manager handle.
SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT);
if (SCManHandle > 0) then
begin
SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS);
// if Service installed
if (SvcHandle > 0) then
begin
// SS structure holds the service status (TServiceStatus);
if (QueryServiceStatus(SvcHandle, SS)) then
dwStat := ss.dwCurrentState;
CloseServiceHandle(SvcHandle);
end;
CloseServiceHandle(SCManHandle);
end;
Result := dwStat;
end;
function ServiceRunning(sMachine, sService: PChar): Boolean;
begin
Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService);
end;
procedure StartServiceExecute(ServerServiceName:string);
Var
SCH: SC_HANDLE;
SvcSCH: SC_HANDLE;
arg: PChar;
begin
SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
If SCH=0 then
Begin
MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);
Exit;
End;
Screen.Cursor:= crHourGlass;
try
SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);
If SvcSCH=0 then
Begin
MessageDlg('Error: OpenService', mtError, [mbOK], 0);
Exit;
End;
try
arg:= nil;
If Not StartService(SvcSCH, 0, arg) then
Begin
Case GetLastError of
ERROR_ACCESS_DENIED :MessageDlg('The specified handlewas not opened with SERVICE_START access.', mtError, [mbOK], 0);
ERROR_INVALID_HANDLE :MessageDlg('The specified handleis invalid.', mtError, [mbOK], 0);
ERROR_PATH_NOT_FOUND :MessageDlg('The service binaryfile could not be found.', mtError, [mbOK], 0);
ERROR_SERVICE_ALREADY_RUNNING :MessageDlg('An instance of theservice is already running.', mtError, [mbOK], 0);
ERROR_SERVICE_DATABASE_LOCKED :MessageDlg('The database islocked.', mtError, [mbOK], 0);
ERROR_SERVICE_DEPENDENCY_DELETED :MessageDlg('The service dependson a service that does not exist or has been marked for deletion.', mtError,[mbOK], 0);
ERROR_SERVICE_DEPENDENCY_FAIL :MessageDlg('The service dependson another service that has failed to start.', mtError, [mbOK], 0);
ERROR_SERVICE_DISABLED :MessageDlg('The service has beendisabled.', mtError, [mbOK], 0);
ERROR_SERVICE_LOGON_FAILED :MessageDlg('The service couldnot be logged on. This error occurs if the service was started from anaccount that does not have the "Log on as a service" right.', mtError,[mbOK], 0);
ERROR_SERVICE_MARKED_FOR_DELETE :MessageDlg('The service has beenmarked for deletion.', mtError, [mbOK], 0);
ERROR_SERVICE_NO_THREAD :MessageDlg('A thread could notbe created for the service.', mtError, [mbOK], 0);
ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for theservice was started, but it did not call StartServiceCtrlDispatcher, or thethread that called StartServiceCtrlDispatcher may be blocked in a controlhandler function.', mtError, [mbOK], 0);
Else MessageDlg('Error:StartService', mtError, [mbOK], 0);
End;{CASE}
End;
//CheckServiceState;
finally
CloseServiceHandle(SvcSCH);
end;
finally
Screen.Cursor:= crDefault;
CloseServiceHandle(SCH);
end;
end;
procedure StopServiceExecute(ServerServiceName:string);
Var
SCH: SC_HANDLE;
SvcSCH: SC_HANDLE;
ss: TServiceStatus;
begin
SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
If SCH=0 then
Begin
MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);
Exit;
End;
Screen.Cursor:= crHourGlass;
try
SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);
If SvcSCH=0 then
Begin
MessageDlg('Error: OpenService', mtError, [mbOK], 0);
Exit;
End;
try
If Not ControlService(SvcSCH, SERVICE_CONTROL_STOP, ss) then
Begin
Case GetLastError of
ERROR_ACCESS_DENIED :MessageDlg('The specifiedhandle was not opened with the necessary access.', mtError, [mbOK], 0);
ERROR_DEPENDENT_SERVICES_RUNNING :MessageDlg('The service cannotbe stopped because other running services are dependent on it.', mtError,[mbOK], 0);
ERROR_INVALID_HANDLE :MessageDlg('The specifiedhandle was not obtained using CreateService or OpenService, or the handle isno longer valid.', mtError, [mbOK], 0);
ERROR_INVALID_PARAMETER :MessageDlg('The requestedcontrol code is undefined.', mtError, [mbOK], 0);
ERROR_INVALID_SERVICE_CONTROL :MessageDlg('The requestedcontrol code is not valid, or it is unacceptable to the service.', mtError,[mbOK], 0);
ERROR_SERVICE_CANNOT_ACCEPT_CTRL :MessageDlg('The requestedcontrol code cannot be sent to the service because the state of the serviceis SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.',mtError, [mbOK], 0);
ERROR_SERVICE_NOT_ACTIVE :MessageDlg('The service has notbeen started.', mtError, [mbOK], 0);
ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for theservice was started, but it did not call StartServiceCtrlDispatcher, or thethread that called StartServiceCtrlDispatcher may be blocked in a controlhandler function.', mtError, [mbOK], 0);
ERROR_SHUTDOWN_IN_PROGRESS :MessageDlg('The system isshutting down.', mtError, [mbOK], 0);
Else MessageDlg('Error:ControlService', mtError, [mbOK], 0);
End;
End;
//CheckServiceState;
finally
CloseServiceHandle(SvcSCH);
end;
finally
Screen.Cursor:= crDefault;
CloseServiceHandle(SCH);
end;
end;
(*
procedure PostVKey(hWindow: HWND; key: Word);
begin
if iswindow(hWindow) then begin
PostMessage(hWindow, WM_KEYDOWN, key, MakeLong(0, MapVirtualKey(key, 0)));
PostMessage(hWindow, WM_KEYUP, key, MakeLong(1, MapVirtualKey(key, 0) or $C000));
end;
end;*)
function enumchildproc(hwnd:HWnd;lparam:LPARAM):Boolean; stdcall;
var
clsname:array[0..31] of char;
clslen:integer;
Str:array[0..512] of char;
begin
//getclassname(hwnd,clsname,31);
//if clsname='Edit' then
//Form1.ListBox2.Items.Add(clsname);
Result := True;
// GetClassName(hwnd, clsname, 400);
SendMessage (hWnd,WM_GETTEXT,255,longint(@str));
//if StrPas(clsname) = 'Edit' then begin
if trim(str) ='是(&Y)' then
//Form1.ListBox2.Items.Add(clsname);
// Form1.ListBox2.Items.Add(inttostr(GetWindowLong(hwnd,GWL_STYLE)));
// form1.ListBox2.items.Add(inttostr(hwnd));
// Form1.ListBox2.Items.Add(str);
// Form1.ListBox2.Items.Add(' ');
//MessageBox(0, '44444', '信息', MB_ICONINFORMATION + MB_OK);
//end;
begin
PostMessage(hwnd, WM_CHAR, Ord('Y'), 0);
//SetForegroundWindow(hwnd);
//PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
//PostMessage(hWnd, WM_KEYUP, VK_RETURN, 0);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
dd:hwnd;
begin
StopServiceExecute(edit1.Text);
while true do
begin
sleep(18);
application.ProcessMessages ;
dd :=findwindow(nil,'瑞星提示');
cc:=dd;
if dd <> 0 then
begin
enumchildwindows(dd,@enumchildproc,0);
break;
//showmessage('hh');
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
StartServiceExecute(edit1.Text );
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if ServiceRunning(nil, pchar(edit1.text)) then
ShowMessage('Eventlog Service Running')
else
ShowMessage('Eventlog Service not Running')
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -