添加服務的C++代碼
//文件名addserver.cpp
#include <stdio.h>
#include <windows.h>
#include <winsvc.h>
char servername[100];
char serverinfo[100];
char serverpath[100];
int main(void)
{
printf("請輸入服務名:");
scanf("%s",&servername);
printf("請輸入服務描述:");
scanf("%s",&serverinfo);
printf("請輸入程序路徑:");
scanf("%s",&serverpath);
SC_HANDLE manager=NULL;
SC_HANDLE service=NULL;
if((manager=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
{
printf("服務打開出錯");
}
service=CreateService(manager,servername,serverinfo,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
serverpath, 0, 0, 0, 0, 0 );
if(service)
printf("創建成功
");
else
printf("創建失敗
");
CloseServiceHandle(service);
CloseServiceHandle(manager);
return 0;
}