ReactOS  0.4.15-dev-5142-g967f5b9
desktop.cpp
Go to the documentation of this file.
1 /*
2  * ReactOS Explorer
3  *
4  * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "precomp.h"
22 
24 {
25 private:
29 
31  static DWORD WINAPI s_DesktopThreadProc(LPVOID lpParameter);
32 
33 public:
36 
37  HRESULT Initialize(ITrayWindow* pTray);
38  void Destroy();
39 };
40 
41 /*******************************************************************/
42 
44  m_Tray(NULL),
45  m_hInitEvent(NULL),
46  m_hThread(NULL)
47 {
48 }
49 
51 {
52  Destroy();
53 }
54 
56 {
57  HANDLE Handles[2];
58 
59  if (!pTray || m_Tray)
60  {
61  return E_FAIL;
62  }
63 
65  if (!m_hInitEvent)
66  {
67  return E_FAIL;
68  }
69 
70  m_Tray = pTray;
72 
73  if (!m_hThread)
74  {
77 
78  m_Tray = NULL;
79 
80  return E_FAIL;
81  }
82 
83  Handles[0] = m_hThread;
84  Handles[1] = m_hInitEvent;
85 
86  for (;;)
87  {
88  DWORD WaitResult = MsgWaitForMultipleObjects(_countof(Handles), Handles, FALSE, INFINITE, QS_ALLEVENTS);
89 
90  if (WaitResult == WAIT_OBJECT_0 + _countof(Handles))
91  {
93  }
94  else if (WaitResult != WAIT_FAILED && WaitResult != WAIT_OBJECT_0)
95  {
96  break;
97  }
98  else
99  {
101  m_hThread = NULL;
102 
104  m_hInitEvent = NULL;
105 
106  m_Tray = NULL;
107 
108  return E_FAIL;
109  }
110  }
111  return S_OK;
112 }
113 
115 {
116  if (m_hThread)
117  {
118  DWORD WaitResult = WaitForSingleObject(m_hThread, 0);
119  if (WaitResult == WAIT_TIMEOUT)
120  {
121  /* Send WM_QUIT message to the thread and wait for it to terminate */
124  }
125 
127  m_hThread = NULL;
128  }
129 
130  if (m_hInitEvent)
131  {
133  m_hInitEvent = NULL;
134  }
135 
136  m_Tray = NULL;
137 }
138 
140 {
142  HANDLE hDesktop;
143  HRESULT hRet;
144  DWORD dwResult = 1;
145 
147 
148  hRet = m_Tray->QueryInterface(IID_PPV_ARG(IShellDesktopTray, &pSdt));
149  if (!SUCCEEDED(hRet))
150  {
151  goto Cleanup;
152  }
153 
154  hDesktop = _SHCreateDesktop(pSdt);
155  if (!hDesktop)
156  {
157  goto Cleanup;
158  }
159 
160  if (!SetEvent(m_hInitEvent))
161  {
162  /* Failed to notify that we initialized successfully, kill ourselves
163  * to make the main thread wake up! */
164  goto Cleanup;
165  }
166 
167  _SHDesktopMessageLoop(hDesktop);
168  dwResult = 0;
169 
170 Cleanup:
171  OleUninitialize();
172  return dwResult;
173 }
174 
176 {
177  CDesktopThread* pDesktopThread = static_cast<CDesktopThread*>(lpParameter);
178  return pDesktopThread->DesktopThreadProc();
179 }
180 
181 /*******************************************************************/
182 
183 HANDLE
184 DesktopCreateWindow(IN OUT ITrayWindow *Tray)
185 {
186  CDesktopThread* pDesktopThread = new CDesktopThread();
187 
188  HRESULT hres = pDesktopThread->Initialize(Tray);
190  {
191  delete pDesktopThread;
192  return NULL;
193  }
194 
195  return pDesktopThread;
196 }
197 
198 VOID
200 {
201  CDesktopThread* pDesktopThread = reinterpret_cast<CDesktopThread*>(hDesktop);
202  delete pDesktopThread;
203 }
HRESULT Initialize(ITrayWindow *pTray)
Definition: desktop.cpp:55
#define IN
Definition: typedefs.h:39
#define CloseHandle
Definition: compat.h:598
HANDLE m_hInitEvent
Definition: desktop.cpp:27
HANDLE m_hThread
Definition: desktop.cpp:28
CComPtr< ITrayWindow > m_Tray
Definition: desktop.cpp:26
VOID TrayProcessMessages(IN OUT ITrayWindow *Tray)
#define WM_QUIT
Definition: winuser.h:1613
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
DWORD WINAPI GetThreadId(IN HANDLE Thread)
Definition: thread.c:913
#define IID_PPV_ARG(Itype, ppType)
#define E_FAIL
Definition: ddrawi.h:102
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:651
VOID DesktopDestroyShellWindow(IN HANDLE hDesktop)
Definition: desktop.cpp:199
DWORD DesktopThreadProc()
Definition: desktop.cpp:139
BOOL WINAPI _SHDesktopMessageLoop(HANDLE hDesktop)
Definition: rshell.cpp:81
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define FALSE
Definition: types.h:117
if SUCCEEDED(hr)
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
#define QS_ALLEVENTS
Definition: winuser.h:896
#define WAIT_OBJECT_0
Definition: winbase.h:403
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:79
#define _countof(array)
Definition: sndvol32.h:68
#define FAILED_UNEXPECTEDLY(hr)
Definition: shellutils.h:82
#define WINAPI
Definition: msvc.h:6
unsigned long DWORD
Definition: ntddk_ex.h:95
#define WAIT_FAILED
Definition: winbase.h:410
HANDLE WINAPI _SHCreateDesktop(IShellDesktopTray *ShellDesk)
Definition: rshell.cpp:52
#define WAIT_TIMEOUT
Definition: dderror.h:14
DWORD WINAPI MsgWaitForMultipleObjects(_In_ DWORD nCount, _In_reads_opt_(nCount) CONST HANDLE *pHandles, _In_ BOOL fWaitAll, _In_ DWORD dwMilliseconds, _In_ DWORD dwWakeMask)
static const WCHAR Cleanup[]
Definition: register.c:80
static DWORD WINAPI s_DesktopThreadProc(LPVOID lpParameter)
Definition: desktop.cpp:175
HRESULT WINAPI DECLSPEC_HOTPATCH OleInitialize(LPVOID reserved)
Definition: ole2.c:169
#define S_OK
Definition: intsafe.h:52
HANDLE DesktopCreateWindow(IN OUT ITrayWindow *Tray)
Definition: desktop.cpp:184
#define NULL
Definition: types.h:112
#define OUT
Definition: typedefs.h:40
#define INFINITE
Definition: serial.h:102
void WINAPI DECLSPEC_HOTPATCH OleUninitialize(void)
Definition: ole2.c:230
BOOL WINAPI PostThreadMessageW(_In_ DWORD, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
void Destroy()
Definition: desktop.cpp:114