Skip to content

Commit

Permalink
move where the service description is set
Browse files Browse the repository at this point in the history
  • Loading branch information
freedom7341 committed Jul 27, 2024
1 parent 544a704 commit cab94ca
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 93 deletions.
97 changes: 9 additions & 88 deletions CscdCfg/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,6 @@ BOOL WINAPI DoEnableSvc()
SC_HANDLE schSCManager;
SC_HANDLE schService;

// Update svc desc TODO: do this elsewhere
DoUpdateSvcDesc();

// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
Expand Down Expand Up @@ -369,64 +366,6 @@ BOOL WINAPI DoEnableSvc()
return TRUE;
}

//
// Purpose:
// Updates the service description to "This is a test description".
//
// Parameters:
// None
//
// Return value:
// None
//
VOID WINAPI DoUpdateSvcDesc()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_DESCRIPTION sd;
LPTSTR szDesc = TEXT("Cascades Theme Utility");

// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights

if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}

// Get a handle to the service.
schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
SERVICE_CHANGE_CONFIG); // need change config access

if (schService == NULL)
{
printf("OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}

// Change the service description.
sd.lpDescription = szDesc;

if (!ChangeServiceConfig2(
schService, // handle to service
SERVICE_CONFIG_DESCRIPTION, // change: description
&sd)) // new description
{
printf("ChangeServiceConfig2 failed\n");
}
else printf("Service description updated successfully.\n");

CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}

//
// Purpose:
// Deletes a service from the SCM database
Expand Down Expand Up @@ -528,11 +467,7 @@ BOOL WINAPI DoStartSvc()
DWORD dwWaitTime;
DWORD dwBytesNeeded;

// Update svc desc TODO: do this elsewhere
DoUpdateSvcDesc();

// Get a handle to the SCM database.

// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // servicesActive database
Expand All @@ -545,7 +480,6 @@ BOOL WINAPI DoStartSvc()
}

// Get a handle to the service.

schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
Expand All @@ -558,8 +492,7 @@ BOOL WINAPI DoStartSvc()
return FALSE;
}

// Check the status in case the service is not stopped.

// Check the status in case the service is not stopped.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
Expand All @@ -573,9 +506,8 @@ BOOL WINAPI DoStartSvc()
return FALSE;
}

// Check if the service is already running. It would be possible
// to stop the service here, but for simplicity this example just returns.

// Check if the service is already running. It would be possible
// to stop the service here, but for simplicity this example just returns.
if (ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
{
printf("Cannot start the service because it is already running\n");
Expand All @@ -585,18 +517,15 @@ BOOL WINAPI DoStartSvc()
}

// Save the tick count and initial checkpoint.

dwOldCheckPoint = GetTickCount();
dwStartTickCount = ssStatus.dwCheckPoint;

// Wait for the service to stop before attempting to start it.

while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth of the wait hint but not less than 1 second
// and not more than 10 seconds.

// and not more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;

if (dwWaitTime < 1000)
Expand All @@ -606,8 +535,7 @@ BOOL WINAPI DoStartSvc()

Sleep(dwWaitTime);

// Check the status until the service is no longer stop pending.

// Check the status until the service is no longer stop pending.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
Expand All @@ -624,7 +552,6 @@ BOOL WINAPI DoStartSvc()
if (ssStatus.dwCheckPoint > dwOldCheckPoint)
{
// Continue to wait and check.

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
Expand All @@ -641,7 +568,6 @@ BOOL WINAPI DoStartSvc()
}

// Attempt to start the service.

if (!StartService(
schService, // handle to service
0, // number of arguments
Expand All @@ -654,8 +580,7 @@ BOOL WINAPI DoStartSvc()
}
else printf("Service start pending...\n");

// Check the status until the service is no longer start pending.

// Check the status until the service is no longer start pending.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
Expand All @@ -670,16 +595,14 @@ BOOL WINAPI DoStartSvc()
}

// Save the tick count and initial checkpoint.

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;

while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.

// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;

if (dwWaitTime < 1000)
Expand All @@ -689,8 +612,7 @@ BOOL WINAPI DoStartSvc()

Sleep(dwWaitTime);

// Check the status again.

// Check the status again.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
Expand All @@ -705,7 +627,6 @@ BOOL WINAPI DoStartSvc()
if (ssStatus.dwCheckPoint > dwOldCheckPoint)
{
// Continue to wait and check.

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
Expand Down
1 change: 0 additions & 1 deletion CscdCfg/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
VOID WINAPI DisplayUsage(void);

VOID WINAPI DoQuerySvc(void);
VOID WINAPI DoUpdateSvcDesc(void);
BOOL WINAPI DoDisableSvc(void);
BOOL WINAPI DoEnableSvc(void);
VOID WINAPI DoDeleteSvc(void);
Expand Down
13 changes: 9 additions & 4 deletions CscdSvc/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <strsafe.h>
#include "hook.h"
#include "thmfunc.h"
#include "svc.h"

/* Defines */
#define WM_THEMECHANGED 0x031A
Expand All @@ -41,9 +42,10 @@ __declspec(dllexport) BOOL CALLBACK InstallUserHook()
WCHAR szFullPath[MAX_PATH];
USERAPIHOOKINFO uah;

MessageBox(NULL, L"test", L"InstallUserHook called", MB_OK);
OutputDebugString(L"InstallUserHook called\n");

SvcMessageEvent(TEXT("InstallUserHook"));

// Unregister before we do anything
// TODO: kill uxtheme kill uxtheme
UnregisterUserApiHookDelay();
Expand Down Expand Up @@ -73,7 +75,8 @@ __declspec(dllexport) BOOL CALLBACK InstallUserHook()
__declspec(dllexport) BOOL CALLBACK InitUserHook(UAPIHK State, PUSERAPIHOOK puah)
{
OutputDebugString(L"InitUserHook called\n");
MessageBox(NULL, L"test", L"InitUserHook called", MB_OK);

SvcMessageEvent(TEXT("InstallUserHook called"));

// Don't initialize if the state isn't appropriate.
if (!puah || State != uahLoadInit)
Expand All @@ -82,10 +85,10 @@ __declspec(dllexport) BOOL CALLBACK InitUserHook(UAPIHK State, PUSERAPIHOOK puah
return TRUE;
}

MessageBox(NULL, L"test", L"InitUserHook initializing", MB_OK);

OutputDebugString(L"InitUserHook initializing\n");

SvcMessageEvent(TEXT("InitUserHook initializing"));

/* Store the original functions from user32 */
g_user32ApiHook = *puah;

Expand Down Expand Up @@ -207,6 +210,8 @@ BOOL WINAPI RegisterUserApiHookDelay(HINSTANCE hInstance, PUSERAPIHOOKINFO ApiHo

FreeLibrary(hLib);

SvcMessageEvent(TEXT("RegisterUserApiHook"));

return bRet;
}

Expand Down
13 changes: 13 additions & 0 deletions CscdSvc/svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ VOID SvcInstall()
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szUnquotedPath[MAX_PATH];
SERVICE_DESCRIPTION sd;
LPTSTR szDesc = TEXT("Hosts and maintains the Cascades Theme Utility.");

if (!GetModuleFileName(NULL, szUnquotedPath, MAX_PATH))
{
Expand Down Expand Up @@ -128,6 +130,17 @@ VOID SvcInstall()
}
else printf("Service installed successfully\n");

// Change the service description.
sd.lpDescription = szDesc;

if (!ChangeServiceConfig2(
schService, // handle to service
SERVICE_CONFIG_DESCRIPTION, // change: description
&sd)) // new description
{
printf("ChangeServiceConfig2 failed\n");
}

CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);

Expand Down

0 comments on commit cab94ca

Please sign in to comment.