#include "Windows.h" #include "Winsvc.h" #include "time.h" #include "stdio.h" SERVICE_STATUS m_ServiceStatus; SERVICE_STATUS_HANDLE m_ServiceStatusHandle; BOOL bRunning=true; void WINAPI ServiceMain(DWORD argc, LPTSTR *argv); void WINAPI ServiceCtrlHandler(DWORD Opcode); BOOL InstallService(); BOOL DeleteService(); //自己想实现功能代码 void DoTask() { // do something here; FILE* fp; int Flen = 0; char* p; p = NULL; fp = fopen("c:\\rcv1.txt", "rb"); fseek(fp, 0, SEEK_END); Flen = ftell(fp); p= (char*)malloc( sizeof(char) * (Flen+1) ); //是这么申请 if(p == NULL)//申请后判定是否申请成功 { return; } fseek(fp, 0, SEEK_SET); //首先移动到文件开头再读取 fwrite(“123”, sizeof(“123”),1, fp);//读取大小无限制 fflush(pf); free(p); } void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { // DWORD status; // DWORD specificError; m_ServiceStatus.dwServiceType = SERVICE_WIN32; m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; m_ServiceStatus.dwWin32ExitCode = 0; m_ServiceStatus.dwServiceSpecificExitCode = 0; m_ServiceStatus.dwCheckPoint = 0; m_ServiceStatus.dwWaitHint = 0; //m_ServiceStatus.dwServiceType = SERVICE_AUTO_START; m_ServiceStatusHandle = RegisterServiceCtrlHandler("Service2",ServiceCtrlHandler); if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) { return; } m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; m_ServiceStatus.dwCheckPoint = 0; m_ServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) { } bRunning=true; while(bRunning) { Sleep(3000); //Place Your Code for processing here.... DoTask(); //Sleep(60*1000); } return; } void WINAPI ServiceCtrlHandler(DWORD Opcode) { switch(Opcode) { case SERVICE_CONTROL_PAUSE: m_ServiceStatus.dwCurrentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_STOP: m_ServiceStatus.dwWin32ExitCode = 0; m_ServiceStatus.dwCurrentState = SERVICE_STOPPED; m_ServiceStatus.dwCheckPoint = 0; m_ServiceStatus.dwWaitHint = 0; SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus); bRunning=false; break; case SERVICE_CONTROL_INTERROGATE: break; } return; } BOOL InstallService() { char strDir[1024]; HANDLE schSCManager,schService; GetCurrentDirectory(1024,strDir); strcat(strDir,"\\Debug\\servicetest.exe"); schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false; LPCTSTR lpszBinaryPathName=strDir; schService = CreateService((struct SC_HANDLE__ *)schSCManager,"Service2","MB Service", // service name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type lpszBinaryPathName, // service's binary NULL, // no load ordering group NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password if (schService == NULL) return false; CloseServiceHandle((struct SC_HANDLE__ *)schService); return true; } BOOL DeleteService() { HANDLE schSCManager; SC_HANDLE hService; schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false; hService=OpenService((struct SC_HANDLE__ *)schSCManager,"Service2",SERVICE_ALL_ACCESS); if (hService == NULL) return false; if(DeleteService(hService)==0) return false; if(CloseServiceHandle(hService)==0) return false; else return true; } int main(int argc, char* argv[]) { if(argc>1) { if(strcmp(argv[1],"-i")==0) { if(InstallService()) printf("\nMB Service Installed Sucessfully\n"); else printf("\nMB Service has been installed\n"); } else if(strcmp(argv[1],"-d")==0) { if(DeleteService()) printf("\nMB Service UnInstalled Sucessfully\n"); else printf("\nInstalled Easin Central Service Not Found\n"); } else { printf("\nUnknown Switch Usage\nFor Install use Servicetest -i\nFor UnInstall use Servicetest -d\n"); } } else { //第二次启动改程序,要由scm运行,进入这里 SERVICE_TABLE_ENTRY DispatchTable[]={{"Service2",ServiceMain},{NULL,NULL}}; StartServiceCtrlDispatcher(DispatchTable); } return 0; }
http://blog.csdn.net/g710710/article/details/7277953
http://www.vckbase.com/document/viewdoc/?id=1447
http://www.vckbase.com/code/listcode.asp?mclsid=13&sclsid=1319
http://blog.csdn.net/johe2003/article/details/4155939