Revision 6f662bc7
Added by Hamish Coleman over 17 years ago
- ID 6f662bc750ab96cb9ab31a8169d53c301dfa3ea1
| Makefile | ||
|---|---|---|
|
CFLAGS:=-Wall
|
||
|
CC:=gcc
|
||
|
|
||
|
wconsd.c: debug.h scm.h
|
||
|
win-scm.c: scm.h
|
||
|
|
||
|
wconsd.exe: wconsd.o win-scm.o
|
||
|
$(CC) -o $@ $^ -lws2_32
|
||
|
|
||
|
portenum.exe: portenum.c
|
||
|
$(CC) -o $@ portenum.c -lwinspool -lsetupapi
|
||
|
$(CC) $(CFLAGS) -o $@ portenum.c -lwinspool -lsetupapi
|
||
|
|
||
|
test: wconsd.exe
|
||
|
./wconsd.exe -d
|
||
| debug.h | ||
|---|---|---|
|
/*
|
||
|
*
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
|
||
|
#define DD printf("debug: %s(%i)\n",__FILE__,__LINE__);
|
||
|
|
||
| scm.h | ||
|---|---|---|
|
};
|
||
|
|
||
|
int SCM_Start(struct SCM_def *);
|
||
|
int SCM_Install(struct SCM_def *);
|
||
|
char *SCM_Install(struct SCM_def *);
|
||
|
int SCM_Remove(struct SCM_def *);
|
||
|
|
||
|
#define SVC_OK 0
|
||
| wconsd.c | ||
|---|---|---|
|
|
||
|
if (argc>1) {
|
||
|
if (strcmp(argv[1],"-i")==0) {
|
||
|
// request service installation
|
||
|
SCM_Install(&sd);
|
||
|
/* request service installation */
|
||
|
char *path = SCM_Install(&sd);
|
||
|
if (!path) {
|
||
|
printf("Service installation failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
printf("Service '%s' installed, binary path '%s'\n",sd.name,path);
|
||
|
printf("You should now start the service using the service manager.\n");
|
||
|
return 0;
|
||
|
} else if (strcmp(argv[1],"-r")==0) {
|
||
|
// request service removal
|
||
|
SCM_Remove(&sd);
|
||
|
if (SCM_Remove(&sd)==0) {
|
||
|
printf("Deleted service '%s'\n",sd.name);
|
||
|
} else {
|
||
|
printf("Service removal failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
return 0;
|
||
|
} else if (strcmp(argv[1],"-p")==0) {
|
||
|
console_application=1;
|
||
| win-scm.c | ||
|---|---|---|
|
/* try to run as a service */
|
||
|
if (StartServiceCtrlDispatcher(ServiceTable)==0) {
|
||
|
int err = GetLastError();
|
||
|
printf("StartServiceCtrlDispatcher error = %d\n", err);
|
||
|
|
||
|
if (err == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
|
||
|
/* TODO - could run the console service from here */
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
/* any other error, assume fatal */
|
||
|
printf("StartServiceCtrlDispatcher failed %d\n", err);
|
||
|
return SVC_FAIL;
|
||
|
}
|
||
|
return SVC_OK;
|
||
|
}
|
||
|
|
||
|
int SCM_Install(struct SCM_def *sd) {
|
||
|
char *SCM_Install(struct SCM_def *sd) {
|
||
|
SC_HANDLE schSCManager, schService;
|
||
|
|
||
|
char path[MAX_PATH];
|
||
|
static char path[MAX_PATH];
|
||
|
|
||
|
if( !GetModuleFileName( NULL, path, MAX_PATH ) ) {
|
||
|
printf("Cannot install service (%d)\n", GetLastError());
|
||
|
return -1;
|
||
|
printf("GetModuleFileName failed %d\n", GetLastError());
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
||
| ... | ... | |
|
if (schService == NULL) {
|
||
|
printf("CreateService failed\n");
|
||
|
CloseServiceHandle(schService);
|
||
|
return -1;
|
||
|
} else {
|
||
|
printf("Created service '%s', binary path %s\n",sd->name,path);
|
||
|
printf("You should now start the service using the service manager.\n");
|
||
|
CloseServiceHandle(schService);
|
||
|
return 0;
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
CloseServiceHandle(schService);
|
||
|
return (char *)&path;
|
||
|
}
|
||
|
|
||
|
int SCM_Remove(struct SCM_def *sd) {
|
||
| ... | ... | |
|
return -1;
|
||
|
}
|
||
|
|
||
|
printf("Deleted service '%s'\n",sd->name);
|
||
|
|
||
|
CloseServiceHandle(schService);
|
||
|
return 0;
|
||
|
}
|
||
Also available in: Unified diff
Clean up scm helper