N
Neffpb
Unregistered / Unconfirmed
GUEST, unregistred user!
小弟参考论坛的相关帖子,编写了以下service代码,但启动服务后,仅触发了TaskManager,未能预期触发Ctrl+Alt+Del的效果。请大家帮忙看看哪里有问题,谢谢啦!
具体做法是:在Delphi6中新建一个Service Application,设置服务的interactive为True,其它的代码如下:
==================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, unit2;
type
TService1 = class(TService)
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
Service1: TService1;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service1.Controller(CtrlCode);
end;
function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
function StimulateCtrlAltDel(): integer;
var
hdeskCurrent: HDESK;
hdeskNew: HDESK;
hwinstaCurrent: HWINSTA;
hwinstaNew: HWINSTA;
begin
Result := 0;
// Save the current Window station //
hwinstaCurrent := GetProcessWindowStation();
if hwinstaCurrent = 0 then
begin
Result := GetLastError();
exit;
end;
// Save the current desktop //
hdeskCurrent := GetThreadDesktop(GetCurrentThreadId());
if hdeskCurrent = 0 then
begin
Result := GetLastError();
end;
// Obtain a handle to WinSta0 - service must be running
// in the LocalSystem account //
hwinstaNew := OpenWindowStation('winsta0', FALSE,
WINSTA_ACCESSCLIPBOARD or
WINSTA_ACCESSGLOBALATOMS or
WINSTA_CREATEDESKTOP or
WINSTA_ENUMDESKTOPS or
WINSTA_ENUMERATE or
WINSTA_EXITWINDOWS or
WINSTA_READATTRIBUTES or
WINSTA_READSCREEN or
WINSTA_WRITEATTRIBUTES);
if hwinstaNew = 0 then
begin
Result := GetLastError();
end;
// Set the windowstation to be winsta0 //
if not SetProcessWindowStation(hwinstaNew) then
begin
Result := GetLastError();
end;
// // Get the default desktop on winsta0 //
hdeskNew := OpenDesktop('Winlogon', 0, FALSE,
DESKTOP_CREATEMENU or
DESKTOP_CREATEWINDOW or
DESKTOP_ENUMERATE or
DESKTOP_HOOKCONTROL or
DESKTOP_JOURNALPLAYBACK or
DESKTOP_JOURNALRECORD or
DESKTOP_READOBJECTS or
DESKTOP_SWITCHDESKTOP or
DESKTOP_WRITEOBJECTS);
if hdeskNew = 0 then
begin
Result := GetLastError();
end;
// Set the desktop to be "default" //
if not SetThreadDesktop(hdeskNew) then
begin
Result := GetLastError();
end;
// Winlogon uses hotkeys to trap Ctrl-Alt-Del
PostMessage(HWND_BROADCAST,WM_HOTKEY,0,MAKELPARAM(MOD_ALT or MOD_CONTROL,VK_DELETE));
// Reset the Window station and desktop //
SetProcessWindowStation(hwinstaCurrent);
SetThreadDesktop(hdeskCurrent);
// Close the windowstation and desktop handles //
CloseWindowStation(hwinstaNew);
CloseDesktop(hdeskNew);
Result := 0;
end;
procedure TService1.ServiceExecute(Sender: TService);
var
dwThreadID : DWORD;
begin
sleep(10000);
StimulateCtrlAltDel();
end;
end.
具体做法是:在Delphi6中新建一个Service Application,设置服务的interactive为True,其它的代码如下:
==================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, unit2;
type
TService1 = class(TService)
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
Service1: TService1;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service1.Controller(CtrlCode);
end;
function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
function StimulateCtrlAltDel(): integer;
var
hdeskCurrent: HDESK;
hdeskNew: HDESK;
hwinstaCurrent: HWINSTA;
hwinstaNew: HWINSTA;
begin
Result := 0;
// Save the current Window station //
hwinstaCurrent := GetProcessWindowStation();
if hwinstaCurrent = 0 then
begin
Result := GetLastError();
exit;
end;
// Save the current desktop //
hdeskCurrent := GetThreadDesktop(GetCurrentThreadId());
if hdeskCurrent = 0 then
begin
Result := GetLastError();
end;
// Obtain a handle to WinSta0 - service must be running
// in the LocalSystem account //
hwinstaNew := OpenWindowStation('winsta0', FALSE,
WINSTA_ACCESSCLIPBOARD or
WINSTA_ACCESSGLOBALATOMS or
WINSTA_CREATEDESKTOP or
WINSTA_ENUMDESKTOPS or
WINSTA_ENUMERATE or
WINSTA_EXITWINDOWS or
WINSTA_READATTRIBUTES or
WINSTA_READSCREEN or
WINSTA_WRITEATTRIBUTES);
if hwinstaNew = 0 then
begin
Result := GetLastError();
end;
// Set the windowstation to be winsta0 //
if not SetProcessWindowStation(hwinstaNew) then
begin
Result := GetLastError();
end;
// // Get the default desktop on winsta0 //
hdeskNew := OpenDesktop('Winlogon', 0, FALSE,
DESKTOP_CREATEMENU or
DESKTOP_CREATEWINDOW or
DESKTOP_ENUMERATE or
DESKTOP_HOOKCONTROL or
DESKTOP_JOURNALPLAYBACK or
DESKTOP_JOURNALRECORD or
DESKTOP_READOBJECTS or
DESKTOP_SWITCHDESKTOP or
DESKTOP_WRITEOBJECTS);
if hdeskNew = 0 then
begin
Result := GetLastError();
end;
// Set the desktop to be "default" //
if not SetThreadDesktop(hdeskNew) then
begin
Result := GetLastError();
end;
// Winlogon uses hotkeys to trap Ctrl-Alt-Del
PostMessage(HWND_BROADCAST,WM_HOTKEY,0,MAKELPARAM(MOD_ALT or MOD_CONTROL,VK_DELETE));
// Reset the Window station and desktop //
SetProcessWindowStation(hwinstaCurrent);
SetThreadDesktop(hdeskCurrent);
// Close the windowstation and desktop handles //
CloseWindowStation(hwinstaNew);
CloseDesktop(hdeskNew);
Result := 0;
end;
procedure TService1.ServiceExecute(Sender: TService);
var
dwThreadID : DWORD;
begin
sleep(10000);
StimulateCtrlAltDel();
end;
end.