/*
  addsvc -add name file "desc" 
  addsvc -del name
*/

#include <windows.h>
#include <winnt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void show_useage(void);
void svc_add(char *name, char *file, char *desc);
void svc_del(char *name);

static	SC_HANDLE sc,sc2;
int main(int argc, char *argv[])
{
	sc = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
	if (sc==NULL) {printf("Open scmanager failed\n"); return 0;}

	if (argc<2) {show_useage(); return 0;}
	if (strcmp(argv[1],"-add")==0) {
		if (argc!=5) {show_useage(); return 0;}
		svc_add(argv[2],argv[3],argv[4]);
	} else {
		if (argc!=3) {show_useage(); return 0;}
		svc_del(argv[2]);
	}
	CloseServiceHandle(sc);
	return 0;
}
void svc_add(char *name, char *fname, char *desc)
{	
	int r;
	char sysdir[MAX_PATH];
	char binpath[MAX_PATH];
	char cmd[200],key[200];
	HKEY hkey;

	GetSystemDirectory((LPSTR) sysdir,MAX_PATH);
	if (strchr(fname,'\\')!=NULL) strncpy(binpath,fname,MAX_PATH-1);
	else sprintf(binpath,"%s\\%s",sysdir,fname);

	printf("Installing service {%s} to start image {%s} \n",name,binpath);

	sc2 = CreateService(
    	sc, // handle of service control manager database  
    	(LPCSTR) name,	// address of name of service to start 
    	(LPCSTR) desc,	// address of display name 
		SERVICE_ALL_ACCESS,	// type of access to service 
		SERVICE_WIN32_OWN_PROCESS,	// type of service 
		SERVICE_AUTO_START,			// when to start service 
    	SERVICE_ERROR_NORMAL, 		// severity if service fails to start 
		(LPCSTR) binpath,	// address of name of binary file 
		"",		// address of name of load ordering group 
		NULL,		// address of variable to get tag identifier 
    	NULL,// address of array of dependency names 
    	NULL,// address of account name of service 
    	NULL 	// address of password for service account 
   );	


	

#define err(x,y) if (r==x) printf(y)
   if (sc2==NULL) {
   		printf("Creation of service failed \n");
	 	r = GetLastError();
		err(ERROR_ACCESS_DENIED,"Access Denied\n");
		err(ERROR_CIRCULAR_DEPENDENCY,"Cicular dependency\n");
		err(ERROR_DUP_NAME,"Dup Name\n");
		err(ERROR_INVALID_HANDLE,"Invalid handle \n");
		err(ERROR_INVALID_NAME,"Invalid name\n");
		err(ERROR_INVALID_PARAMETER,"Invalid Parameter\n");
		err(ERROR_INVALID_SERVICE_ACCOUNT,"Invalid service\n");
		err(ERROR_SERVICE_EXISTS,"Service exists\n");
	}
    else {
    	printf("Services %s successfully added to local database\n",name);

		strcpy(cmd,"xmit ipmodem.marc.cri.nz -at *5 *");
		sprintf(key,"System\\CurrentControlSet\\Services\\%s\\args",name);

		CloseServiceHandle(sc2);
/*		r = RegCreateKey(HKEY_LOCAL_MACHINE,key,&hkey);
		if (r!=ERROR_SUCCESS) printf("failure: RegCreateKey (0x%02x)\n", GetLastError());
   		r = RegSetValue(hkey,NULL,REG_SZ,cmd,strlen(cmd));
		if (r!=ERROR_SUCCESS) printf("failure: RegSetValue (0x%02x)\n", GetLastError());
*/
	}
}
void svc_del(char *name)
{
    int ret;

    sc2 = OpenService(sc, name, SERVICE_ALL_ACCESS);
    if (sc2 == NULL) {
        printf("failure: OpenService (0x%02x)\n", GetLastError());
        return;
    }
    ret = DeleteService(sc2);
	CloseServiceHandle(sc2);

    if (ret)
        printf("Successfully deleted service {%s}\n",name);
    else
        printf("failure: DeleteService (0x%02x)\n", GetLastError());
}

void show_useage(void)
{
  printf("addsvc -add name file \"desc\" \n");
  printf("addsvc -del name\n");
}




