程序能把自己加入服务吗
参考Windows API 编程,把程序1加入服务是通过程序2来执行的。用什么方法可以把自身加入服务?
int main(int argc,char *argv[]) { if(argc<2) main_2(); else main_1(); system("pause"); return 0; } void main_1( ) { // 设置SERVICE_TABLE_ENTRY 数据结构,以NULL 结构结束, // 作为StartServiceCtrlDispatcher 函数的参数。 SERVICE_TABLE_ENTRY DispatchTable[] = { { "Sample_Srv", (LPSERVICE_MAIN_FUNCTION) SplSrvServiceStart }, { NULL, NULL } }; if (!StartServiceCtrlDispatcher( DispatchTable)) { SvcDebugOut(" [SPLSRV_SERVICE] StartServiceCtrlDispatcher (%d)\n", GetLastError()); } } void main_2( ) { TCHAR szBinFilePath[MAX_PATH]; PTCHAR pTemp; DWORD dwStopError; // 构造服务可执行程序的路径 GetModuleFileName(NULL,szBinFilePath,MAX_PATH); pTemp = szBinFilePath+lstrlen(szBinFilePath); // 打开 SCM schSCManager = OpenSCManager( NULL, // local machine NULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) printf("OpenSCManager failed (%d)\n", GetLastError()); // 创建服务 CreateSampleService(schSCManager, szBinFilePath, szServiceName); // 启动服务 StartSampleService(schSCManager,szServiceName); // 发送请求控制 ControlSampleService(SERVICE_CONTROL_INTERROGATE); ControlSampleService(SERVICE_CONTROL_CONTINUE); // 停止服务 dwStopError = StopService( schSCManager, szServiceName, TRUE, 1000); if(ERROR_SUCCESS == dwStopError) { printf("Service Stoped\n"); } else { printf("Service stoped error (%u)\n",dwStopError); } // 删除服务 DeleteSampleService(szServiceName); CloseServiceHandle(schSCManager); }