1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
| #define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
/* Real name of the service */
char *ServiceName = "Service";
char *ServiceDescription = "Description";
/* Startup type */
long ServiceStartFlag = SERVICE_AUTO_START;
/* The service should start as soon as it is installed or not */
long ServiceStartRightNow = TRUE;
/* Type of service */
long ServiceTypeFlag = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
/* --------------- Service datas */
STARTUPINFO si;
PROCESS_INFORMATION pi;
SC_HANDLE ServiceHandle;
SC_HANDLE ServicesDatabase;
OSVERSIONINFO OsVer;
DWORD ServiceCurrentStatus;
HANDLE ServiceEvent;
HANDLE hServiceThread;
SERVICE_DESCRIPTION ServiceDesc;
SERVICE_STATUS_HANDLE ServiceStatus;
SERVICE_TABLE_ENTRY ServiceTable[2];
SERVICE_STATUS ServiceStatusTable;
char FileName[MAX_PATH + 1];
char *ErrStartMsg = "Can't initialize control dispatcher.";
char *ErrServiceDBMsg = "Can't open services database.";
char *ErrCreateServiceMsg = "Impossible de créer le service.";
char *ErrOpenServiceMsg = "Impossible d'ouvrir le service.";
char *ErrRemoveServiceMsg = "Impossible de désinstaller le service.";
char *ServiceInstalledMsg = "Service installé.";
char *ServiceRemovedMsg = "Service désinstallé.";
/* Note: the produced file will auto-install or remove itself when run.
To start it go to the service manager of Windows NT/2000/XP. */
/* --------------- Perform tasks right before service effective creation
Out: 0 = Stop install process
1 = Proceed with install
---------------
ServicesDatabase variable is initialized
--------------- */
long ServiceStart(void) {
return(1);
}
/* --------------- Perform tasks right before service effective removal
Out: 0 = Stop removal process
1 = Proceed with removal
---------------
ServicesDatabase variable is initialized
ServiceHandle variable is initialized
--------------- */
long ServiceRemove(void) {
return(1);
}
/* --------------- Background Thread (infinite) of the service */
DWORD __stdcall ServiceThread(LPVOID Param) {
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
siStartupInfo.dwFlags = STARTF_USESTDHANDLES || STARTF_USESHOWWINDOW;
siStartupInfo.wShowWindow = SW_MAXIMIZE;
siStartupInfo.cb = sizeof(si);
HANDLE hUserToken;
LogonUser("user", "domain", "pass", LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, &hUserToken);
char commande[2100]={0};
sprintf(commande, "C:\\test\\notepad.exe");
CreateProcessAsUser(hUserToken, commande, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}
/* --------------- Functions Declarations */
void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
/* --------------- Display an error and terminate process */
void RaiseError(char *ErrorMsg) {
MessageBox(NULL, ErrorMsg, ServiceName, MB_OK | MB_ICONERROR);
return;
}
/* --------------- Display an informative message */
void RaiseInformation(char *InfoMsg) {
MessageBox(NULL, InfoMsg, ServiceName, MB_OK | MB_ICONINFORMATION);
return;
}
/* --------------- Service entry point
Must run 3 in ways with the same procedure:
1. Install
2. Start
3. Remove */
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
ServicesDatabase = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if(!ServicesDatabase) {
RaiseError(ErrServiceDBMsg);
return(0);
}
/* Check if the service is in starting state */
ServiceHandle = OpenService(ServicesDatabase, ServiceName, SERVICE_ALL_ACCESS);
if(!ServiceHandle) {
/* Call user procedure */
if(ServiceStart()) goto InstallProceed;
CloseServiceHandle(ServicesDatabase);
return(0);
}
if(QueryServiceStatus(ServiceHandle, &ServiceStatusTable)) {
/* Feed service manager with our thread if starting state */
if(ServiceStatusTable.dwCurrentState == SERVICE_START_PENDING) {
CloseServiceHandle(ServiceHandle);
CloseServiceHandle(ServicesDatabase);
ServiceTable[0].lpServiceName = ServiceName;
ServiceTable[0].lpServiceProc = &ServiceMain;
if(!StartServiceCtrlDispatcher(&ServiceTable[0])) RaiseError(ErrStartMsg);
return(0);
}
}
CloseServiceHandle(ServiceHandle);
InstallProceed:
GetModuleFileName(0, FileName, MAX_PATH);
/* Try to install */
ServiceHandle = CreateService(ServicesDatabase, ServiceName, ServiceName, SERVICE_ALL_ACCESS, ServiceTypeFlag, ServiceStartFlag, SERVICE_ERROR_NORMAL, FileName, NULL, NULL, NULL, NULL, NULL);
if(ServiceHandle) {
OsVer.dwOSVersionInfoSize = sizeof(OsVer);
if(GetVersionEx(&OsVer) != 0) {
if(OsVer.dwMajorVersion >= 5) {
// Add a description if OS >= Win2k
if(OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT) {
ServiceDesc.lpDescription = ServiceDescription;
ChangeServiceConfig2(ServiceHandle, SERVICE_CONFIG_DESCRIPTION, &ServiceDesc);
}
}
}
if(ServiceStartRightNow) StartService(ServiceHandle, 0, 0);
CloseServiceHandle(ServiceHandle);
CloseServiceHandle(ServicesDatabase);
RaiseInformation(ServiceInstalledMsg);
return(0);
}
if(GetLastError() != ERROR_SERVICE_EXISTS) {
CloseServiceHandle(ServicesDatabase);
RaiseError(ErrCreateServiceMsg);
return(0);
}
/* Perform removal */
ServiceHandle = OpenService(ServicesDatabase, ServiceName, SERVICE_ALL_ACCESS | DELETE);
if(!ServiceHandle) {
CloseServiceHandle(ServicesDatabase);
RaiseError(ErrOpenServiceMsg);
return(0);
}
QueryServiceStatus(ServiceHandle, &ServiceStatusTable);
if(ServiceStatusTable.dwCurrentState != SERVICE_STOPPED) {
ControlService(ServiceHandle, SERVICE_CONTROL_STOP, &ServiceStatusTable);
Sleep(500);
}
/* Call user procedure */
if(!ServiceRemove()) {
CloseServiceHandle(ServiceHandle);
CloseServiceHandle(ServicesDatabase);
return(0);
}
if(DeleteService(ServiceHandle)) {
CloseServiceHandle(ServiceHandle);
CloseServiceHandle(ServicesDatabase);
RaiseInformation(ServiceRemovedMsg);
return(0);
}
CloseServiceHandle(ServiceHandle);
CloseServiceHandle(ServicesDatabase);
RaiseError(ErrRemoveServiceMsg);
return(0);
}
/* --------------- Initialize service thread */
long InitServiceThread(void) {
DWORD ThreadID;
hServiceThread = CreateThread(0, 0, &ServiceThread, 0, 0, &ThreadID);
if(hServiceThread) {
ServiceCurrentStatus |= 1;
return(1);
}
return(0);
}
/* --------------- Resume service */
void ResumeService(void) {
ServiceCurrentStatus &= 0xfffffffd;
ResumeThread(hServiceThread);
return;
}
/* --------------- Pause service */
void PauseService(void) {
ServiceCurrentStatus |= 2;
SuspendThread(hServiceThread);
return;
}
/* --------------- Stop service */
void StopService(void) {
ServiceCurrentStatus &= 0xfffffffe;
SetEvent(ServiceEvent);
return;
}
/* --------------- Send message to system */
long SendStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint) {
ServiceStatusTable.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatusTable.dwCurrentState = dwCurrentState;
if(dwCurrentState == SERVICE_START_PENDING) ServiceStatusTable.dwControlsAccepted = 0;
else ServiceStatusTable.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
if(dwServiceSpecificExitCode == 0) ServiceStatusTable.dwWin32ExitCode = dwWin32ExitCode;
else ServiceStatusTable.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
ServiceStatusTable.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
ServiceStatusTable.dwCheckPoint = dwCheckPoint;
ServiceStatusTable.dwWaitHint = dwWaitHint;
SetServiceStatus(ServiceStatus, &ServiceStatusTable);
return(1);
}
/* --------------- Terminate service */
long TerminateService(DWORD ProvidedErr) {
if(ServiceEvent) CloseHandle(ServiceEvent);
if(ServiceStatus) SendStatus(SERVICE_STOPPED, ProvidedErr, 0, 0, 0);
if(hServiceThread) CloseHandle(hServiceThread);
return(0);
}
/* --------------- Answer to system messages */
void __stdcall CtrlHandler(DWORD CtrlCode) {
DWORD StatetoSend = 0;
switch(CtrlCode) {
case SERVICE_CONTROL_STOP:
SendStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
StopService();
StatetoSend = SERVICE_STOPPED;
break;
case SERVICE_CONTROL_PAUSE:
if(ServiceCurrentStatus == 1) {
SendStatus(SERVICE_PAUSE_PENDING, NO_ERROR, 0, 1, 1000);
PauseService();
StatetoSend = SERVICE_PAUSED;
}
break;
case SERVICE_CONTROL_CONTINUE:
if(ServiceCurrentStatus == 3) {
SendStatus(SERVICE_CONTINUE_PENDING, NO_ERROR, 0, 1, 1000);
ResumeService();
StatetoSend = SERVICE_RUNNING;
}
break;
case SERVICE_CONTROL_SHUTDOWN:
return;
}
SendStatus(StatetoSend, NO_ERROR, 0, 0, 0);
}
/* --------------- Service main handler */
void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
ServiceStatus = RegisterServiceCtrlHandler(ServiceName, &CtrlHandler);
if(!ServiceStatus) {
TerminateService(GetLastError());
return;
}
SendStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 5000);
ServiceEvent = CreateEvent(0, TRUE, FALSE, 0);
if(!ServiceEvent) {
TerminateService(GetLastError());
return;
}
SendStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
SendStatus(SERVICE_START_PENDING, NO_ERROR, 0, 3, 5000);
if(!InitServiceThread()) {
TerminateService(GetLastError());
return;
}
SendStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
/* Endless loop */
WaitForSingleObject(ServiceEvent, INFINITE);
TerminateService(0);
return;
} |