ReactOS  0.4.11-dev-946-g431643b
thmserver.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS kernel
4  * FILE: base/services/thmsvc/thmserver.c
5  * PURPOSE: Themes server
6  * PROGRAMMER: Giannis Adamopoulos
7  */
8 
9 /*
10  * NOTE:
11  * ThemeWaitForServiceReady and ThemeWatchForStart are called from msgina
12  * so all the functions in this file run in the context of winlogon
13  */
14 
15 /* INCLUDES *****************************************************************/
16 
17 #include <windows.h>
18 #include <uxtheme.h>
19 #include <uxundoc.h>
20 #include <wine/debug.h>
21 
23 
24 /* GLOBALS ******************************************************************/
25 
26 static WCHAR ServiceName[] = L"Themes";
27 
30 
32 
33 /* FUNCTIONS *****************************************************************/
34 
35 static
36 HANDLE
38 {
39  SC_HANDLE scm, service;
41  DWORD dummy;
42  HANDLE ret;
43 
44  if (!(scm = OpenSCManagerW( NULL, NULL, 0 )))
45  {
46  ERR( "failed to open service manager\n" );
47  return NULL;
48  }
49  if (!(service = OpenServiceW( scm, ServiceName, SERVICE_QUERY_STATUS )))
50  {
51  ERR( "failed to open themes service\n" );
52  CloseServiceHandle( scm );
53  return NULL;
54  }
55 
57  (BYTE *)&status, sizeof(status), &dummy ))
58  {
59  ERR("QueryServiceStatusEx failed\n");
60  CloseServiceHandle( service );
61  CloseServiceHandle( scm );
62  return NULL;
63  }
64 
65  ret = OpenProcess(SYNCHRONIZE, FALSE, status.dwProcessId);
66 
67  CloseServiceHandle( service );
68  CloseServiceHandle( scm );
69 
70  return ret;
71 }
72 
73 static
74 VOID
77 {
78  CloseHandle(hServiceProcess);
79  UnregisterWait(hThemeStopWaitObject);
80  UnregisterWait(hThemeServiceWaitObject);
81 
84 }
85 
86 static
87 VOID
90 {
91  /* The theme service died and we don't know if it could set its events properly */
92  ResetEvent(hStartEvent);
93  ResetEvent(hStopEvent);
94 
95  ThemeStopCallback(lpParameter, TimerOrWaitFired);
96 }
97 
98 static
99 VOID
100 CALLBACK
102 {
103  UnregisterWait(hThemeStartWaitObject);
104 
105  hServiceProcess = GetThemeServiceProcessHandle();
106 
107  RegisterWaitForSingleObject(&hThemeStopWaitObject, hStopEvent, ThemeStopCallback, NULL, INFINITE, WT_EXECUTEDEFAULT);
108  RegisterWaitForSingleObject(&hThemeServiceWaitObject, hServiceProcess, ThemeServiceDiedCallback, NULL, INFINITE, WT_EXECUTEDEFAULT);
109 
111 }
112 
113 BOOL
114 WINAPI
116 {
117  hStartEvent = CreateEventW(NULL, TRUE, FALSE, L"Global\\ThemeStartEvent");
118  hStopEvent = CreateEventW(NULL, TRUE, FALSE, L"Global\\ThemeStopEvent");
119 
120  RegisterWaitForSingleObject(&hThemeStartWaitObject, hStartEvent, ThemeStartCallback, NULL, INFINITE, WT_EXECUTEDEFAULT);
121 
122  return TRUE;
123 }
124 
125 DWORD
126 WINAPI
128 {
129  SC_HANDLE scm, service;
131  BOOL ret = FALSE;
132  DWORD start_time = GetTickCount();
133 
134  TRACE("\n");
135 
136  if (!(scm = OpenSCManagerW( NULL, NULL, 0 )))
137  {
138  ERR( "failed to open service manager\n" );
139  return FALSE;
140  }
141  if (!(service = OpenServiceW( scm, ServiceName, SERVICE_QUERY_STATUS )))
142  {
143  ERR( "failed to open themes service\n" );
144  CloseServiceHandle( scm );
145  return FALSE;
146  }
147 
148  while(TRUE)
149  {
150  DWORD dummy;
151 
153  (BYTE *)&status, sizeof(status), &dummy ))
154  break;
155  if (status.dwCurrentState == SERVICE_RUNNING)
156  {
157  ret = TRUE;
158  break;
159  }
160  if (status.dwCurrentState != SERVICE_START_PENDING)
161  {
162  break;
163  }
164 
165  if (GetTickCount() - start_time > dwTimeout)
166  {
167  break;
168  }
169  Sleep( 100 );
170  };
171 
172  CloseServiceHandle( service );
173  CloseServiceHandle( scm );
174  return ret;
175 }
176 
177 /* EOF */
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
BOOL WINAPI ThemeHooksInstall()
Definition: themehooks.c:664
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define CALLBACK
Definition: compat.h:27
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
#define SERVICE_START_PENDING
Definition: winsvc.h:22
static VOID CALLBACK ThemeStartCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
Definition: thmserver.c:101
BOOL WINAPI ThemeHooksRemove()
Definition: themehooks.c:706
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2829
HANDLE hThemeStopWaitObject
Definition: thmserver.c:28
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:597
BOOL WINAPI UnregisterWait(IN HANDLE WaitHandle)
Definition: synch.c:880
static WCHAR ServiceName[]
Definition: thmserver.c:26
HANDLE hServiceProcess
Definition: thmserver.c:29
WINE_DEFAULT_DEBUG_CHANNEL(shsvcs)
#define SERVICE_RUNNING
Definition: winsvc.h:24
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2102
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
static VOID CALLBACK ThemeStopCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
Definition: thmserver.c:76
static HANDLE GetThemeServiceProcessHandle(VOID)
Definition: thmserver.c:37
#define TRACE(s)
Definition: solgame.cpp:4
unsigned int BOOL
Definition: ntddk_ex.h:94
HANDLE hStopEvent
Definition: thmserver.c:29
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI ThemeWaitForServiceReady(DWORD dwTimeout)
Definition: thmserver.c:127
HANDLE hStartEvent
Definition: thmserver.c:29
int ret
static const WCHAR L[]
Definition: oid.c:1087
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2010
LPVOID lpParameter
Definition: kernel32.h:232
#define SYNCHRONIZE
Definition: nt_native.h:61
BOOL WINAPI ThemeWatchForStart(VOID)
Definition: thmserver.c:115
#define WINAPI
Definition: msvc.h:20
unsigned char BYTE
Definition: ntddk_ex.h:96
#define ERR(fmt,...)
Definition: debug.h:109
_In_ void _In_ PCCERT_CONTEXT _In_opt_ LPFILETIME _In_ DWORD _In_ DWORD dwTimeout
Definition: wincrypt.h:6079
HANDLE hThemeStartWaitObject
Definition: thmserver.c:28
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1257
unsigned char dummy
Definition: maze.c:118
BOOL WINAPI RegisterWaitForSingleObject(OUT PHANDLE phNewWaitObject, IN HANDLE hObject, IN WAITORTIMERCALLBACK Callback, IN PVOID Context, IN ULONG dwMilliseconds, IN ULONG dwFlags)
Definition: synch.c:796
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent(IN HANDLE hEvent)
Definition: synch.c:660
static VOID CALLBACK ThemeServiceDiedCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
Definition: thmserver.c:89
#define INFINITE
Definition: serial.h:102
static SERVICE_STATUS status
Definition: service.c:31
#define WT_EXECUTEDEFAULT
Definition: winnt_old.h:1071
HANDLE hThemeServiceWaitObject
Definition: thmserver.c:28