activex dll不行,要寫成標準的api dll才行,要匯出乙個函式servicemain;
以下為一段delphi的**
library servicedll;
uses
sysutils,
classes,
winsvc,
system,
windows;
var
svcstatshandle: service_status_handle; // 服務控制資訊控制代碼
dwcurrstate: dword; // 儲存服務狀態
servicename: pchar = 'bits '; // 服務名稱
procedure outputtext(ch: pchar);
var
filehandle: textfile;
ff: integer;
begin
try
if not fileexists( 'zztestdll.txt ') then
ff := filecreate( 'zztestdll.txt ');
finally
if ff > 0 then fileclose(ff);
end;
assignfile(filehandle, 'zztestdll.txt ');
writeln(filehandle, ch);
flush(filehandle);
closefile(filehandle);
end;
procedure dllentrypoint(dwreason: dword);
begin
case dwreason of
dll_process_attach:
; dll_process_detach:
; dll_thread_attach:
; dll_thread_detach:
; end;
end;
function tellscm(dwstate: dword; dwexitcode: dword; dwprogress: dword): longbool;
var srvstatus: service_status;
begin
srvstatus.dwservicetype := service_win32_share_process;
dwcurrstate := dwstate;
srvstatus.dwcurrentstate := dwstate;
srvstatus.dwcontrolsaccepted := service_accept_stop or service_accept_pause_continue or service_accept_shutdown;
srvstatus.dwwin32exitcode := dwexitcode;
srvstatus.dwservicespecificexitcode := 0;
srvstatus.dwcheckpoint := dwprogress;
srvstatus.dwwaithint := 3000;
result := setservicestatus(svcstatshandle, srvstatus);
end;
procedure servicehandler(fdwcontrol: integer); stdcall;
begin
case fdwcontrol of
service_control_stop:
begin
tellscm(service_stop_pending, 0, 1);
sleep(10);
tellscm(service_stopped, 0, 0);
end;
service_control_pause:
begin
tellscm(service_pause_pending, 0, 1);
tellscm(service_paused, 0, 0);
end;
service_control_continue:
begin
tellscm(service_continue_pending, 0, 1);
tellscm(service_running, 0, 0);
end;
service_control_interrogate:
tellscm(dwcurrstate, 0, 0);
service_control_shutdown:
tellscm(service_stopped, 0, 0);
end;
end;
procedure servicemain(argc: integer; var argv: pchar); stdcall;
begin
// 註冊控制函式
svcstatshandle := registerservicectrlhandler(servicename, @servicehandler);
if (svcstatshandle = 0) then
begin
outputtext( 'error in registerservicectrlhandler ');
exit;
end else freeconsole();
// 啟動服務
tellscm(service_start_pending, 0, 1);
tellscm(service_running, 0, 0);
outputtext( 'service is running ');
// 這裡可以執行我們真正要作的**
while ((dwcurrstate <> service_stop_pending) and (dwcurrstate <> service_stopped)) do
begin
sleep(1000);
end;
outputtext( 'service exit ');
end;
// 匯出函式列表
exports
servicemain;
begin
dllproc := @dllentrypoint;
end.
windows服務的編寫
windows服務的應用場合 因為其執行穩定,可以設定為開機自動啟動,可以設定合理的使用者許可權,恢復策略 服務出現異常 適合應用於7x24小時執行的後台程式。服務編碼過程中的一些要點 2.執行緒的關閉不贊成直接丟擲異常的方式,最好用通知方式,讓執行緒體自行結束。3.服務除錯,可以設定臨時用編譯開關...
編寫c 服務
服務程式主函式。include stdafx.h include windows.h define szservicename servicesample 標識服務的內部名 內部變數 bool bdebugserver false service status ssstatus service st...
編寫服務程式
編寫服務程式會用到這些函式 服務主函式 servicemain startservicectrldispatcher dispatcher n.排程員 計 排程程式 計 分配器 服務控制處理函式 servicectrlhandle registerservicectrlhandle 首先建立乙個wi...