将一个服务程序注册 // TestServerLoad.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <windows.h> #include <winsvc.h> int main(int argc, char* argv[]) { if(argc != 4) { printf("Help:/r/n"); printf("TestServerLoad.exe [Server Name] [Server Information] [Server Applaction Path]/r/n"); scanf("%c"); return 0; } wchar_t serverName[256] = {0}; wchar_t serverInfo[256] = {0}; wchar_t serverPath[256] = {0}; MultiByteToWideChar(CP_ACP,0,argv[1],-1,serverName,256); MultiByteToWideChar(CP_ACP,0,argv[2],-1,serverInfo,256); MultiByteToWideChar(CP_ACP,0,argv[3],-1,serverPath,256); //wchar_t* showInfo = L"sysnap's first Windows service"; //wchar_t* showName = L"sysnap"; //wchar_t* sv_Path = L"E://Test//ServiceTest.exe"; SC_HANDLE Hsysnap; SC_HANDLE hSCManager; hSCManager = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE); if(!hSCManager) { printf("OpenSCManager failed/r/n"); printf("TestServerLoad = %s/r/n[Server Name] = %s/r/n[Server Information] = %s/r/n[Server Applaction Path]= %s/r/n",argv[0],argv[1],argv[2],argv[3]); return 1; } Hsysnap = CreateService(hSCManager,(LPWSTR)(serverName), (LPWSTR)(serverInfo), SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, (LPWSTR)(serverPath), 0,0,0,0,0); if(!Hsysnap) { CloseServiceHandle(hSCManager); printf("CreateService failed/r/n"); printf("TestServerLoad = %s/r/n[Server Name] = %s/r/n[Server Information] = %s/r/n[Server Applaction Path]= %s/r/n",argv[0],argv[1],argv[2],argv[3]); return 1; } // Set service description SERVICE_DESCRIPTION ServiceDesc; wchar_t* showDescription = L"sysnap's first Windows service"; ServiceDesc.lpDescription = showDescription; ::ChangeServiceConfig2(Hsysnap,SERVICE_CONFIG_DESCRIPTION,&ServiceDesc); CloseServiceHandle(Hsysnap); CloseServiceHandle(hSCManager); printf("Create Service success!!!/r/n[Server Name] = %s/r/n",argv[1]); return 0; //修改服务描述 Create service //SC_HANDLE hService = NULL; //hService = ::CreateService(...); Set service description //SERVICE_DESCRIPTION ServiceDesc; //static TCHAR szDescription[MAX_PATH]; //_tcscpy(szDescription, _T("Your description...")); //ServiceDesc.lpDescription = szDescription; //::ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &ServiceDesc); }