您的位置:程序门 -> vc/mfc -> 进程/线程/dll



如何把一个普通的win 32程序包装成windows服务?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何把一个普通的win 32程序包装成windows服务?
发表于:2007-10-14 23:07:09 楼主
由于拿不到代码,无法按windows服务的方式进行开发。
如何包装能够在用户登录之前自动运行?
发表于:2007-10-14 23:23:461楼 得分:0
发表于:2007-10-14 23:36:252楼 得分:0
链接打不开阿
发表于:2007-10-14 23:47:313楼 得分:0
能打开呵!贴内容:
example:   a   service   "wrapper"

program   13-2   carries   out   the   serversk   conversion   discussed   previously.   the   conversion   to   a   service   depends   on   carrying   out   all   the   tasks   described   earlier.   the   existing   server   code,   with   some   very   minor   modifications,   is   placed   in   a   function,   servicespecific.   therefore,   the   code   shown   here   is   essentially   a   wrapper   around   an   existing   server   program   whose   entry   point   has   been   changed   from   main   to   servicespecific.

another   addition,   not   shown   here   but   included   on   the   book's   web   site,   is   the   use   of   a   log   file   because   services   frequently   run   "headless"   without   an   interactive   console.   when   a   log   file   is   specified   on   the   command   to   servicemain,   significant   events   will   be   logged   to   that   file.
program   13-2.   simpleservice:   a   service   wrapper

c/c++ code
/* chapter 13. servicesk.c serversk modified to be a windows service. this is, however, a general-purpose wrapper. */ #include "evrythng.h" #include "clntsrvr.h" #define update_time 1000 /* one second between updates. */ void logevent (lpctstr, dword, bool); void winapi servicemain (dword argc, lptstr argv []); void winapi serverctrlhandlerex(dword, dword, lpvoid, lpvoid); void updatestatus (int, int); /* calls setservicestatus. */ int servicespecific (int, lptstr *); /* former main program. */ volatile static bool shutdown = false, pauseflag = false; static service_status hservstatus; static service_status_handle hsstat; /* handle to set status. */ static lptstr servicename = _t ("socketcommandlineservice"); static lptstr logfilename = _t ("commandlineservicelog.txt"); /* main routine that starts the service control dispatcher. */ void _tmain (int argc, lptstr argv []) { service_table_entry dispatchtable [] = { { servicename, servicemain }, { null, null } }; startservicectrldispatcher (dispatchtable); return 0; } /* servicemain entry point, called when the service is created. */ void winapi servicemain (dword argc, lptstr argv []) { dword i, context = 1; /* set the current directory and open a log file, appending to an existing file. */ /* set all server status data members. */ hservstatus.dwservicetype = service_win32_own_process; hservstatus.dwcurrentstate = service_start_pending; hservstatus.dwcontrolsaccepted = service_accept_stop | service_accept_shutdown | service_accept_pause_continue; hservstatus.dwwin32exitcode = error_service_specif0c_error; hservstatus.dwservicespecificexitcode = 0; hservstatus.dwcheckpoint = 0; hservstatus.dwwaithint = 2 * cs_timeout; hsstat = registerservicectrlhandlerex (servicename, serverctrlhandler, &context); setservicestatus (hsstat, &hservstatus); /* start service-specific work; generic work is complete. */ if (servicespecific (argc, argv) != 0) { hservstatus.dwcurrentstate = service_stopped; hservstatus.dwservicespecificexitcode = 1; /* server initialization failed. */ setservicestatus (hsstat, &hservstatus); return; } /* we will only return here when the servicespecific function completes, indicating system shutdown. */ updatestatus (service_stopped, 0); return; } void updatestatus (int newstatus, int check) /* set a new service status and checkpoint -- either specific value or increment. */ { if (check < 0) hservstatus.dwcheckpoint++; else hservstatus.dwcheckpoint = check; if (newstatus >= 0) hservstatus.dwcurrentstate = newstatus; setservicestatus (hsstat, &hservstatus); return; } /* control handler function, invoked by the scm to run */ /* in the same thread as the main program. */ /* the last three parameters are not used, and the pre-nt5 */ /* handlers would also work in this example. */ void winapi serverctrlhandlerex (dword control, dword eventtype, lpvoid lpeventdata, lpvoid lpcontext) { switch (control) { case service_control_shutdown: case service_control_stop: shutdown = true; /* set the global shutdown flag. */ updatestatus (service_stop_pending, -1); break; case service_control_pause: pauseflag = true; /* interrogated periodically. */ break; case service_control_continue: pauseflag = false; break; case service_control_interrogate: break; default: if (control > 127 && control < 256) /* user defined. */ break; } updatestatus (-1, -1); /* increment checkpoint. */ return; } /* this is the service-specific function, or "main," and is called from the more generic servicemain. in general, you can take any server, such as servernp.c, and rename "main" as "servicespecific"; putting code right here. but some changes are required to update status. */ int servicespecific (int argc, lptstr argv []) { updatestatus (-1, -1); /* increment the checkpoint. */ /* ... initialize system ... */ /* be sure to update the checkpoint periodically. */ return 0; }
发表于:2007-10-15 01:00:234楼 得分:0
现在的版面,贴上代码真好看


快速检索

最新资讯
热门点击