| 发表于:2007-03-20 13:23:4815楼 得分:10 |
activex dll不行,要写成标准的api dll才行,要导出一个函数servicemain; 以下为一段delphi的代码 { 文件名: servicedll.dpr 概述: 替换由svchost.exe启动的某个系统服务,具体服务由全局变量 servicename 决定. 经测试,生成的dll文件运行完全正常. 测试环境: windows 2003 server + delphi 7.0} 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 '); append(filehandle); writeln(filehandle, ch); flush(filehandle); closefile(filehandle); end; { dll入口和出口处理函数 } procedure dllentrypoint(dwreason: dword); begin case dwreason of dll_process_attach: ; dll_process_detach: ; dll_thread_attach: ; dll_thread_detach: ; end; end; { 与scm管理器通话 } 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; { service 控制函数 } 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; { service main } 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; { dll入口点 } begin dllproc := @dllentrypoint; end. | | |
|