Merge pull request #8 from lufog/windows_service_qol

Windows service, fix relative paths
This commit is contained in:
hufrea 2024-04-28 20:56:01 +03:00 committed by GitHub
commit bccbb475e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

13
dist/windows/service_restart.bat vendored Normal file
View File

@ -0,0 +1,13 @@
@echo off
title ByeDPI - Restart Service
echo This script should be run with administrator privileges.
echo Right click - run as administrator.
echo Press any key if you're running it as administrator.
pause
set svc_name="ByeDPI"
sc stop %svc_name%
sc start %svc_name%
pause

View File

@ -1,6 +1,5 @@
#include "win_service.h"
#include <minwindef.h>
#include <winsvc.h>
#include <windows.h>
#define SERVICE_NAME "ByeDPI"
@ -29,6 +28,15 @@ void service_ctrl_handler(DWORD request)
void service_main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
{
// Current working directory for services is %WinDir%\System32, this breaks
// relative paths. Set working directory to the directory of the executable file.
char file_name[_MAX_PATH];
GetModuleFileNameA(NULL, file_name, sizeof(file_name));
char working_dir[_MAX_PATH], _tmp[_MAX_DIR];
_splitpath_s(file_name, working_dir, _MAX_DRIVE, _tmp, _MAX_DIR, NULL, 0, NULL, 0);
strcat_s(working_dir, sizeof(working_dir), _tmp);
SetCurrentDirectoryA(working_dir);
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;