Windows 2000/XPå2003çæ¯æä¸ç§å«å"æå¡ç¨åº"çä¸è¥¿.ç¨åºä½ä¸ºæå¡å¯å¨æ以ä¸å 个好å¤:Â
   (1)ä¸ç¨ç»éè¿ç³»ç»å³å¯è¿è¡.
   (2)å ·æSYSTEMç¹æ.æä»¥ä½ å¨è¿ç¨ç®¡çå¨éé¢æ¯æ æ³ç»æå®ç.
   ç¬è å¨2003年为ä¸å ¬å¸å¼åæºé¡¶ç项ç®çæ¶å,æ¾ç»åè¿è¯¾ä»¶ä¸ä¼ ååªä½æå¡,ä¸é¢å°±ä»ç»ä¸ä¸å¦ä½ç¨Delphi7å建ä¸ä¸ªServiceç¨åº.Â
   è¿è¡Delphi7,éæ©èåFile-->New-->Other--->Service Application.å°çæä¸ä¸ªæå¡ç¨åºçæ¡æ¶.å°å·¥ç¨ä¿å为ServiceDemo.dpråUnit_Main.pas,ç¶ååå°ä¸»æ¡æ¶.æ们注æå°,Serviceæå 个å±æ§.å ¶ä¸ä»¥ä¸å 个æ¯æ们æ¯è¾å¸¸ç¨ç:
   (1)DisplayName:æå¡çæ¾ç¤ºå称
   (2)Name:æå¡å称.
   æ们å¨è¿éå°DisplayNameçå¼æ¹ä¸º"Delphiæå¡æ¼ç¤ºç¨åº",Nameæ¹ä¸º"DelphiService".ç¼è¯è¿ä¸ªé¡¹ç®,å°å¾å°ServiceDemo.exe.è¿å·²ç»æ¯ä¸ä¸ªæå¡ç¨åºäº!è¿å ¥CMD模å¼,åæ¢è´å·¥ç¨æå¨ç®å½,è¿è¡å½ä»¤"ServiceDemo.exe /install",å°æ示æå¡å®è£ æå!ç¶å"net start DelphiService"å°å¯å¨è¿ä¸ªæå¡.è¿å ¥æ§å¶é¢ç-->管çå·¥å ·-->æå¡,å°æ¾ç¤ºè¿ä¸ªæå¡åå½åç¶æ.ä¸è¿è¿ä¸ªæå¡ç°å¨ä»ä¹ä¹å¹²ä¸äº,å 为æ们è¿æ²¡æå代ç :)å "net stop DelphiService"åæ¢å"ServiceDemo.exe /uninstall"å é¤è¿ä¸ªæå¡.åå°Delphi7çIDE.
   æ们ç计åæ¯ä¸ºè¿ä¸ªæå¡æ·»å ä¸ä¸ªä¸»çªå£,è¿è¡åä»»å¡æ æ¾ç¤ºç¨åºçå¾æ ,åå»å¾æ å°æ¾ç¤ºä¸»çªå£,ä¸é¢æä¸ä¸ªæé®,ç¹å»è¯¥æé®å°å®ç°Ctrl+Alt+Delåè½.
   å®é ä¸,æå¡ç¨åºè«è®¤æ¯å·¥ä½äºWinlogonæ¡é¢ç,å¯ä»¥æå¼æ§å¶é¢æ¿,æ¥çæ们åæé£ä¸ªæå¡çå±æ§-->ç»é,å ¶ä¸"å 许æå¡ä¸æ¡é¢äº¤äº"æ¯ä¸æé©ç.æä¹å?åµåµ,åå°IDE,注æé£ä¸ªå¸å°å±æ§:Interactive,å½è¿ä¸ªå±æ§ä¸ºTrueçæ¶å,该æå¡ç¨åºå°±å¯ä»¥ä¸æ¡é¢äº¤äºäº.
   File-->New-->Form为æå¡æ·»å çªå£FrmMain,åå ä¿å为Unit_FrmMain,并ä¸æè¿ä¸ªçªå£è®¾ç½®ä¸ºæå·¥å建.å®æåç代ç å¦ä¸:
ââview plainâââ
âââprintâââ
âââ?ââ
1. unit
2.
3. interface
4.
5. uses
6. Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, Unit_FrmMain;
7.
8. type
9. TDelphiService = class(TService)
10. procedure ServiceContinue(Sender: TService; var
11. procedure
12. procedure ServicePause(Sender: TService; var
13. procedure
14. procedure ServiceStart(Sender: TService; var
15. procedure ServiceStop(Sender: TService; var
16. private
17. { Private declarations }
18. public
19. function
20. { Public declarations }
21. end;
22.
23. var
24. DelphiService: TDelphiService;
25. FrmMain: TFrmMain;
26. implementation
27.
28. {$R *.DFM}
29.
30. procedure
31. begin
32. .Controller(CtrlCode);
33. end;
34.
35. function TDelphiService.GetServiceController: TServiceController;
36. begin
37. Result := ServiceController;
38. end;
39.
40. procedure TDelphiService.ServiceContinue(Sender: TService;
41. var
42. begin
43. while not Terminated do
44. begin
45. 10);
46. .ProcessRequests(False);
47. end;
48. end;
49.
50. procedure TDelphiService.ServiceExecute(Sender: TService);
51. begin
52. while not Terminated do
53. begin
54. 10);
55. .ProcessRequests(False);
56. end;
57. end;
58.
59. procedure TDelphiService.ServicePause(Sender: TService;
60. var
61. begin
62. Paused := True;
63. end;
64.
65. procedure TDelphiService.ServiceShutdown(Sender: TService);
66. begin
67. true;
68. .Free;
69. Status := csStopped;
70. ReportStatus();
71. end;
72.
73. procedure TDelphiService.ServiceStart(Sender: TService;
74. var
75. begin
76. Started := True;
77. .Application.CreateForm(TFrmMain, FrmMain);
78. gbCanClose := False;
79. .Hide;
80. end;
81.
82. procedure TDelphiService.ServiceStop(Sender: TService;
83. var
84. begin
85. Stopped := True;
86. gbCanClose := True;
87. .Free;
88. end;
89.
90. end.
主çªå£åå å¦ä¸:
ââview plainâââ
âââprintâââ
âââ?ââ
åºç¨ç¨åºï¼ServiceDemo
1. unit
2.
3. interface
4.
5. uses
6. Windows, Messages, SysUtils, Variants, Classes, ShellApi, Graphics, Controls, Forms,
7. Dialogs, ExtCtrls, StdCtrls;
8.
9. const
10. WM_TrayIcon = WM_USER + 1234;
11. type
12. TFrmMain = class(TForm)
13. Timer1: TTimer;
14. Button1: TButton;
15. procedure
16. procedure FormCloseQuery(Sender: TObject; var
17. procedure
18. procedure
19. procedure
20. private
21. { Private declarations }
22. IconData: TNotifyIconData;
23. procedure
24. procedure
25. procedure TrayIconMessage(var
26. procedure SysButtonMsg(var
27. public
28. { Public declarations }
29. end;
30.
31. var
32. FrmMain: TFrmMain;
33. gbCanClose: Boolean;
34. implementation
35.
36. {$R *.dfm}
37.
38. procedure TFrmMain.FormCreate(Sender: TObject);
39. begin
40. {çªå£æå}
41. .Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW); {ä¸å¨ä»»å¡æ æ¾ç¤º}
42. gbCanClose := False;
43. .Interval := 1000;
44. .Enabled := True;
45. end;
46.
47. procedure TFrmMain.FormCloseQuery(Sender: TObject; var
48. begin
49. CanClose := gbCanClose;
50. if not CanClose then
51. begin
52. Hide;
53. end;
54. end;
55.
56. procedure TFrmMain.FormDestroy(Sender: TObject);
57. begin
58. .Enabled := False;
59. DelIconFromTray;
60. end;
61.
62. procedure TFrmMain.AddIconToTray;
63. begin
64. ZeroMemory(@IconData, SizeOf(TNotifyIconData));
65. .cbSize := SizeOf(TNotifyIconData);
66. .Wnd := Handle;
67. .uID := 1;
68. .uFlags := NIF_MESSAGE or NIF_ICON or
69. .uCallbackMessage := WM_TrayIcon;
70. .hIcon := Application.Icon.Handle;
71. .szTip := 'Delphiæå¡æ¼ç¤ºç¨åº';
72. Shell_NotifyIcon(NIM_ADD, @IconData);
73. end;
74.
75. procedure TFrmMain.DelIconFromTray;
76. begin
77. Shell_NotifyIcon(NIM_DELETE, @IconData);
78. end;
79.
80. procedure TFrmMain.SysButtonMsg(var
81. begin
82. if (Msg.wParam = SC_CLOSE) or
83. .wParam = SC_MINIMIZE) then
84. else inherited; // æ§è¡é»è®¤å¨ä½
85. end;
86.
87. procedure TFrmMain.TrayIconMessage(var
88. begin
89. if (Msg.LParam = WM_LBUTTONDBLCLK) then
90. end;
91.
92. procedure TFrmMain.Timer1Timer(Sender: TObject);
93. begin
94. AddIconToTray;
95. end;
96.
97. procedure
98. var
99. HDesk_WL: HDESK;
100. begin
101. 'Winlogon', 0, False, DESKTOP_JOURNALPLAYBACK);
102. if (HDesk_WL <> 0) then
103. if (SetThreadDesktop (HDesk_WL) = True) then
104. 0, MAKELONG (MOD_ALT or
105. end;
106.
107. procedure TFrmMain.Button1Click(Sender: TObject);
108. var
109. dwThreadID : DWORD;
110. begin
111. nil, 0, @SendHokKey, nil, 0, dwThreadID);
112. end;
113.
114. end.
ââview plainâââ
âââprintâââ
âââ?ââ
1. program
2.
3. uses
4. SvcMgr,
5. Unit_Main in 'Unit_Main.pas' {DelphiService: TService},
6. Unit_frmMain in 'Unit_frmMain.pas' {frmMain};
7.
8. {$R *.RES}
9.
10. begin
11. .Initialize;
12. .CreateForm(TDelphiService, DelphiService);
13. .Run;
14. end.
çªä½ä»£ç å¦ä¸:
ââview plainâââ
âââprintâââ
âââ?ââ
1. object
2. OldCreateOrder = False
3. DisplayName = 'Delphiæå¡æ¼ç¤ºç¨åº'
4. Interactive = True
5. OnContinue = ServiceContinue
6. OnExecute = ServiceExecute
7. OnPause = ServicePause
8. OnShutdown = ServiceShutdown
9. OnStart = ServiceStart
10. OnStop = ServiceStop
11. Left = 261
12. Top = 177
13. Height = 150
14. Width = 215
15. end
16.
17. object
18. Left = 192
19. Top = 107
20. Width = 696
21. Height = 480
22. Caption = 'æçæå¡æµè¯ç¨åº'
23. Color = clBtnFace
24. Font.Charset = DEFAULT_CHARSET
25. Font.Color = clWindowText
26. Font.Height = -11
27. Font.Name = 'MS Sans Serif'
28. Font.Style = []
29. OldCreateOrder = False
30. OnCloseQuery = FormCloseQuery
31. OnCreate = FormCreate
32. OnDestroy = FormDestroy
33. PixelsPerInch = 96
34. TextHeight = 13
35. object
36. Left = 296
37. Top = 264
38. Width = 75
39. Height = 25
40. Caption = 'Button1'
41. TabOrder = 0
42. OnClick = Button1Click
43. end
44. object
45. OnTimer = Timer1Timer
46. Left = 120
47. Top = 192
48. end
49. end
è¡¥å :
(1)å ³äºæ´å¤æå¡ç¨åºçæ¼ç¤ºç¨åº,请访é®ä»¥ä¸Url:http://www.torry.net/pages.php?id=226,ä¸é¢å å«äºå¤ä¸ªæ¼ç¤ºå¦ä½æ§å¶å管çç³»ç»æå¡ç代ç .
(2)请åè®°:Windowså®é ä¸åå¨å¤ä¸ªæ¡é¢.ä¾å¦å±å¹ä¼ è¾ä¼åºç°ç½å±,å¯è½æ两个åå :ä¸æ¯ç³»ç»å¤äºéå®ææªç»éæ¡é¢,äºæ¯å¤äºå±å¹ä¿æ¤æ¡é¢.è¿æ¶åè¦å°å½åæ¡é¢åæ¢å°è¯¥æ¡é¢æè½æå±.
(3)å ³äºæå¡ç¨åºä¸æ¡é¢äº¤äº,è¿æç§å¨æåæ¢æ¹æ³.大æ¦åå å¦ä¸:
ââview plainâââ
âââprintâââ
âââ?ââ
1. unit
2.
3. interface
4.
5. function InitServiceDesktop: boolean;
6. procedure
7.
8. implementation
9.
10. uses
11.
12. const
13. DefaultWindowStation = WinSta0;
14. DefaultDesktop = Default;
15. var
16. hwinstaSave: HWINSTA;
17. hdeskSave: HDESK;
18. hwinstaUser: HWINSTA;
19. hdeskUser: HDESK;
20. function InitServiceDesktop: boolean;
21. var
22. dwThreadId: DWORD;
23. begin
24. dwThreadId := GetCurrentThreadID;
25. // Ensure connection to service window station and desktop, and
26. // save their handles.
27. hwinstaSave := GetProcessWindowStation;
28. hdeskSave := GetThreadDesktop(dwThreadId);
29.
30.
31. hwinstaUser := OpenWindowStation(DefaultWindowStation, FALSE, MAXIMUM_ALLOWED);
32. if hwinstaUser = 0 then
33. begin
34. OutputDebugString(PChar(OpenWindowStation failed + SysErrorMessage(GetLastError)));
35. Result := false;
36. exit;
37. end;
38.
39. if not SetProcessWindowStation(hwinstaUser) then
40. begin
41. OutputDebugString(SetProcessWindowStation failed);
42. Result := false;
43. exit;
44. end;
45.
46. hdeskUser := OpenDesktop(DefaultDesktop, 0, FALSE, MAXIMUM_ALLOWED);
47. if hdeskUser = 0 then
48. begin
49. OutputDebugString(OpenDesktop failed);
50. SetProcessWindowStation(hwinstaSave);
51. CloseWindowStation(hwinstaUser);
52. Result := false;
53. exit;
54. end;
55. Result := SetThreadDesktop(hdeskUser);
56. if not Result then
57. OutputDebugString(PChar(SetThreadDesktop + SysErrorMessage(GetLastError)));
58. end;
59.
60. procedure
61. begin
62. // Restore window station and desktop.
63. SetThreadDesktop(hdeskSave);
64. SetProcessWindowStation(hwinstaSave);
65. if hwinstaUser <> 0 then
66. CloseWindowStation(hwinstaUser);
67. if hdeskUser <> 0 then
68. CloseDesktop(hdeskUser);
69. end;
70.
71. initialization
72. InitServiceDesktop;
73. finalization
74. DoneServiceDesktop;
75. end.
æ´è¯¦ç»çæ¼ç¤ºä»£ç 请åç:ââhttp://www.torry.net/samples/samples/os/isarticle.zipââ
Â
(4)å ³äºå®è£ æå¡å¦ä½æ·»å æå¡æè¿°.æ两ç§æ¹æ³:ä¸æ¯ä¿®æ¹æ³¨å表.æå¡ç详ç»ä¿¡æ¯é½ä½äºHKEY_LOCAL_MACHINE/SYSTEM/ ControlSet001/Services/ä¸é¢,ä¾å¦æ们åæé£ä¸ªæå¡å°±ä½äºHKEY_LOCAL_MACHINE/SYSTEM/ ControlSet001/Services/DelphiServiceä¸.第äºç§æ¹æ³å°±æ¯å ç¨QueryServiceConfig2å½æ°è·åæå¡ä¿¡æ¯,ç¶åChangeServiceConfig2æ¥æ¹åæè¿°.ç¨Delphiå®ç°çè¯,åå å¦ä¸:
ââview plainâââ
âââprintâââ
âââ?ââ
1. unit
2.
3. interface
4.
5. uses
6.
7. const
8. //
9. // Service config info levels
10. //
11. SERVICE_CONFIG_DESCRIPTION = 1;
12. SERVICE_CONFIG_FAILURE_ACTIONS = 2;
13. //
14. // DLL name of imported functions
15. //
16. AdvApiDLL = advapi32.dll;
17. type
18. //
19. // Service description string
20. //
21. PServiceDescriptionA = ^TServiceDescriptionA;
22. PServiceDescriptionW = ^TServiceDescriptionW;
23. PServiceDescription = PServiceDescriptionA;
24. {$EXTERNALSYM _SERVICE_DESCRIPTIONA}
25. _SERVICE_DESCRIPTIONA = record
26. lpDescription : PAnsiChar;
27. end;
28. {$EXTERNALSYM _SERVICE_DESCRIPTIONW}
29. _SERVICE_DESCRIPTIONW = record
30. lpDescription : PWideChar;
31. end;
32. {$EXTERNALSYM _SERVICE_DESCRIPTION}
33. _SERVICE_DESCRIPTION = _SERVICE_DESCRIPTIONA;
34. {$EXTERNALSYM SERVICE_DESCRIPTIONA}
35. SERVICE_DESCRIPTIONA = _SERVICE_DESCRIPTIONA;
36. {$EXTERNALSYM SERVICE_DESCRIPTIONW}
37. SERVICE_DESCRIPTIONW = _SERVICE_DESCRIPTIONW;
38. {$EXTERNALSYM SERVICE_DESCRIPTION}
39. SERVICE_DESCRIPTION = _SERVICE_DESCRIPTIONA;
40. TServiceDescriptionA = _SERVICE_DESCRIPTIONA;
41. TServiceDescriptionW = _SERVICE_DESCRIPTIONW;
42. TServiceDescription = TServiceDescriptionA;
43.
44. //
45. // Actions to take on service failure
46. //
47. {$EXTERNALSYM _SC_ACTION_TYPE}
48. _SC_ACTION_TYPE = (SC_ACTION_NONE, SC_ACTION_RESTART, SC_ACTION_REBOOT, SC_ACTION_RUN_COMMAND);
49. {$EXTERNALSYM SC_ACTION_TYPE}
50. SC_ACTION_TYPE = _SC_ACTION_TYPE;
51.
52. PServiceAction = ^TServiceAction;
53. {$EXTERNALSYM _SC_ACTION}
54. _SC_ACTION = record
55. aType : SC_ACTION_TYPE;
56. Delay : DWORD;
57. end;
58. {$EXTERNALSYM SC_ACTION}
59. SC_ACTION = _SC_ACTION;
60. TServiceAction = _SC_ACTION;
61.
62. PServiceFailureActionsA = ^TServiceFailureActionsA;
63. PServiceFailureActionsW = ^TServiceFailureActionsW;
64. PServiceFailureActions = PServiceFailureActionsA;
65. {$EXTERNALSYM _SERVICE_FAILURE_ACTIONSA}
66. _SERVICE_FAILURE_ACTIONSA = record
67. dwResetPeriod : DWORD;
68. lpRebootMsg : LPSTR;
69. lpCommand : LPSTR;
70. cActions : DWORD;
71. lpsaActions : ^SC_ACTION;
72. end;
73. {$EXTERNALSYM _SERVICE_FAILURE_ACTIONSW}
74. _SERVICE_FAILURE_ACTIONSW = record
75. dwResetPeriod : DWORD;
76. lpRebootMsg : LPWSTR;
77. lpCommand : LPWSTR;
78. cActions : DWORD;
79. lpsaActions : ^SC_ACTION;
80. end;
81. {$EXTERNALSYM _SERVICE_FAILURE_ACTIONS}
82. _SERVICE_FAILURE_ACTIONS = _SERVICE_FAILURE_ACTIONSA;
83. {$EXTERNALSYM SERVICE_FAILURE_ACTIONSA}
84. SERVICE_FAILURE_ACTIONSA = _SERVICE_FAILURE_ACTIONSA;
85. {$EXTERNALSYM SERVICE_FAILURE_ACTIONSW}
86. SERVICE_FAILURE_ACTIONSW = _SERVICE_FAILURE_ACTIONSW;
87. {$EXTERNALSYM SERVICE_FAILURE_ACTIONS}
88. SERVICE_FAILURE_ACTIONS = _SERVICE_FAILURE_ACTIONSA;
89. TServiceFailureActionsA = _SERVICE_FAILURE_ACTIONSA;
90. TServiceFailureActionsW = _SERVICE_FAILURE_ACTIONSW;
91. TServiceFailureActions = TServiceFailureActionsA;
92.
93. ///
94. // API Function Prototypes
95. ///
96. TQueryServiceConfig2 = function (hService : SC_HANDLE; dwInfoLevel : DWORD; lpBuffer : pointer;
97. cbBufSize : DWORD; var
98. TChangeServiceConfig2 = function (hService : SC_HANDLE; dwInfoLevel : DWORD; lpInfo : pointer) : BOOL; stdcall;
99.
100. var
101. hDLL : THandle ;
102. LibLoaded : boolean
103.
104. var
105. OSVersionInfo : TOSVersionInfo;
106.
107. {$EXTERNALSYM QueryServiceConfig2A}
108. QueryServiceConfig2A : TQueryServiceConfig2;
109. {$EXTERNALSYM QueryServiceConfig2W}
110. QueryServiceConfig2W : TQueryServiceConfig2;
111. {$EXTERNALSYM QueryServiceConfig2}
112. QueryServiceConfig2 : TQueryServiceConfig2;
113.
114. {$EXTERNALSYM ChangeServiceConfig2A}
115. ChangeServiceConfig2A : TChangeServiceConfig2;
116. {$EXTERNALSYM ChangeServiceConfig2W}
117. ChangeServiceConfig2W : TChangeServiceConfig2;
118. {$EXTERNALSYM ChangeServiceConfig2}
119. ChangeServiceConfig2 : TChangeServiceConfig2;
120.
121. implementation
122.
123. initialization
124. OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
125. GetVersionEx(OSVersionInfo);
126. if (OSVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT) and (OSVersionInfo.dwMajorVersion >= 5) then
127. begin
128. if hDLL = 0 then
129. begin
130. hDLL:=GetModuleHandle(AdvApiDLL);
131. LibLoaded := False;
132. if hDLL = 0 then
133. begin
134. hDLL := LoadLibrary(AdvApiDLL);
135. LibLoaded := True;
136. end;
137. end;
138.
139. if hDLL <> 0 then
140. begin
141. @QueryServiceConfig2A := GetProcAddress(hDLL, QueryServiceConfig2A);
142. @QueryServiceConfig2W := GetProcAddress(hDLL, QueryServiceConfig2W);
143. @QueryServiceConfig2 := @QueryServiceConfig2A;
144. @ChangeServiceConfig2A := GetProcAddress(hDLL, ChangeServiceConfig2A);
145. @ChangeServiceConfig2W := GetProcAddress(hDLL, ChangeServiceConfig2W);
146. @ChangeServiceConfig2 := @ChangeServiceConfig2A;
147. end;
148. end
149. else
150. begin
151. @QueryServiceConfig2A := nil;
152. @QueryServiceConfig2W := nil;
153. @QueryServiceConfig2 := nil;
154. @ChangeServiceConfig2A := nil;
155. @ChangeServiceConfig2W := nil;
156. @ChangeServiceConfig2 := nil;
157. end;
158.
159. finalization
160. if (hDLL <> 0) and LibLoaded then
161. FreeLibrary(hDLL);
162.
163. end.
ââview plainâââ
âââprintâââ
âââ?ââ
1. unit
2.
3. interface
4.
5. uses
6. Windows,WinSvc,WinSvcEx;
7.
8. function InstallService(const strServiceName,strDisplayName,strDescription,strFilename: string):Boolean;
9. //eg:InstallService(æå¡å称,æ¾ç¤ºå称,æè¿°ä¿¡æ¯,æå¡æ件);
10. procedure UninstallService(strServiceName:string);
11. implementation
12.
13. function StrLCopy(Dest: PChar; const
14. asm
15. PUSH EDI
16. PUSH ESI
17. PUSH EBX
18. MOV ESI,EAX
19. MOV EDI,EDX
20. MOV EBX,ECX
21. XOR AL,AL
22. TEST ECX,ECX
23. JZ @@1
24. REPNE SCASB
25. JNE @@1
26. INC ECX
27. @@1: SUB EBX,ECX
28. MOV EDI,ESI
29. MOV ESI,EDX
30. MOV EDX,EDI
31. MOV ECX,EBX
32. SHR ECX,2
33. REP MOVSD
34. MOV ECX,EBX
35. AND ECX,3
36. REP MOVSB
37. STOSB
38. MOV EAX,EDX
39. POP EBX
40. POP ESI
41. POP EDI
42. end;
43.
44. function StrPCopy(Dest: PChar; const Source: string): PChar;
45. begin
46. Result := StrLCopy(Dest, PChar(Source), Length(Source));
47. end;
48.
49. function InstallService(const strServiceName,strDisplayName,strDescription,strFilename: string):Boolean;
50. var
51. //ss : TServiceStatus;
52. //psTemp : PChar;
53. hSCM,hSCS:THandle;
54.
55. srvdesc : PServiceDescription;
56. desc : string;
57. //SrvType : DWord;
58.
59. lpServiceArgVectors:pchar;
60. begin
61. Result:=False;
62. //psTemp := nil;
63. //SrvType := SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS;
64. hSCM:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);//è¿æ¥æå¡æ°æ®åº
65. if hSCM=0 then Exit;//MessageBox(hHandle,Pchar(SysErrorMessage(GetLastError)),æå¡ç¨åºç®¡çå¨,MB_ICONERROR+MB_TOPMOST);
66.
67.
68. hSCS:=CreateService( //å建æå¡å½æ°
69. hSCM, // æå¡æ§å¶ç®¡çå¥æ
70. Pchar(strServiceName), // æå¡å称
71. Pchar(strDisplayName), // æ¾ç¤ºçæå¡å称
72. SERVICE_ALL_ACCESS, // ååæå©
73. SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,// æå¡ç±»å SERVICE_WIN32_SHARE_PROCESS
74. SERVICE_AUTO_START, // å¯å¨ç±»å
75. SERVICE_ERROR_IGNORE, // é误æ§å¶ç±»å
76. Pchar(strFilename), // æå¡ç¨åº
77. nil, // ç»æå¡å称
78. nil, // ç»æ è¯
79. nil, // ä¾èµçæå¡
80. nil, // å¯å¨æå¡å¸å·
81. nil); // å¯å¨æå¡å£ä»¤
82. if hSCS=0 then Exit;//MessageBox(hHandle,Pchar(SysErrorMessage(GetLastError)),Pchar(Application.Title),MB_ICONERROR+MB_TOPMOST);
83.
84. if Assigned(ChangeServiceConfig2) then
85. begin
86. desc := Copy(strDescription,1,1024);
87. GetMem(srvdesc,SizeOf(TServiceDescription));
88. GetMem(srvdesc^.lpDescription,Length(desc) + 1);
89. try
90. StrPCopy(srvdesc^.lpDescription, desc);
91. ChangeServiceConfig2(hSCS,SERVICE_CONFIG_DESCRIPTION,srvdesc);
92. finally
93. FreeMem(srvdesc^.lpDescription);
94. FreeMem(srvdesc);
95. end;
96. end;
97. lpServiceArgVectors := nil;
98. if not StartService(hSCS, 0, lpServiceArgVectors) then //å¯å¨æå¡
99. Exit; //MessageBox(hHandle,Pchar(SysErrorMessage(GetLastError)),Pchar(Application.Title),MB_ICONERROR+MB_TOPMOST);
100. CloseServiceHandle(hSCS); //å
³éå¥æ
101. Result:=True;
102. end;
103.
104. procedure UninstallService(strServiceName:string);
105. var
106. SCManager: SC_HANDLE;
107. Service: SC_HANDLE;
108. Status: TServiceStatus;
109. begin
110. SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
111. if SCManager = 0 then
112. try
113. Service := OpenService(SCManager, Pchar(strServiceName), SERVICE_ALL_ACCESS);
114. ControlService(Service, SERVICE_CONTROL_STOP, Status);
115. DeleteService(Service);
116. CloseServiceHandle(Service);
117. finally
118. CloseServiceHandle(SCManager);
119. end;
120. end;
121.
122. end.
(5)å¦ä½æ´åå ³éä¸ä¸ªæå¡ç¨åº,å®ç°æ们以åé£ä¸ª"NTå·¥å ·ç®±"çåè½?é¦å ,æ ¹æ®è¿ç¨å称æ¥ææ»è¿ç¨æ¯ç¨ä»¥ä¸å½æ°:
ââview plainâââ
âââprintâââ
âââ?ââ
使ç¨æ¹æ³:
1. uses
2.
3. function KillTask(ExeFileName: string): Integer;
4. const
5. PROCESS_TERMINATE = 01;
6. var
7. ContinueLoop: BOOL;
8. FSnapshotHandle: THandle;
9. FProcessEntry32: TProcessEntry32;
10. begin
11. Result := 0;
12. FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
13. FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
14. ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
15.
16. while Integer(ContinueLoop) <> 0 do
17. begin
18. if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
19. UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
20. UpperCase(ExeFileName))) then
21. Result := Integer(TerminateProcess(
22. OpenProcess(PROCESS_TERMINATE,
23. BOOL(0),
24. FProcessEntry32.th32ProcessID),
25. 0));
26. ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
27. end;
28. CloseHandle(FSnapshotHandle);
29. end;
30.
31. ä½æ¯å¯¹äºæå¡ç¨åº,å®ä¼æ示"æç»è®¿é®".å
¶å®åªè¦ç¨åºæ¥æDebugæéå³å¯:
32. function
33. function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
34. var
35. TP: TOKEN_PRIVILEGES;
36. Dummy: Cardinal;
37. begin
38. TP.PrivilegeCount := 1;
39. LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
40. if bEnable then
41. TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
42. else TP.Privileges[0].Attributes := 0;
43. AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
44. Result := GetLastError = ERROR_SUCCESS;
45. end;
46.
47. var
48. hToken: Cardinal;
49. begin
50. OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
51. result:=EnablePrivilege(hToken, SeDebugPrivilege, True);
52. CloseHandle(hToken);
53. end;