-
Notifications
You must be signed in to change notification settings - Fork 2
/
Desk.cpp
121 lines (96 loc) · 2.58 KB
/
Desk.cpp
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
/*
* Windows Explorer
*/
#include "precomp.h"
class CDesktopThread
{
HANDLE m_hEvent;
CComPtr<ITrayWindow> m_Tray;
DWORD DesktopThreadProc()
{
CComPtr<IShellDesktopTray> pSdt;
HANDLE hDesktop;
HRESULT hRet;
OleInitialize(NULL);
hRet = m_Tray->QueryInterface(IID_PPV_ARG(IShellDesktopTray, &pSdt));
if (!SUCCEEDED(hRet))
return 1;
hDesktop = _SHCreateDesktop(pSdt);
if (hDesktop == NULL)
return 1;
if (!SetEvent(m_hEvent))
{
/* Failed to notify that we initialized successfully, kill ourselves
to make the main thread wake up! */
return 1;
}
_SHDesktopMessageLoop(hDesktop);
/* FIXME: Properly rundown the main thread! */
ExitProcess(0);
return 0;
}
static DWORD CALLBACK s_DesktopThreadProc(IN OUT LPVOID lpParameter)
{
return reinterpret_cast<CDesktopThread*>(lpParameter)->DesktopThreadProc();
}
public:
CDesktopThread() :
m_hEvent(NULL),
m_Tray(NULL)
{
}
HRESULT Initialize(IN OUT ITrayWindow *pTray)
{
HANDLE hThread;
HANDLE Handles[2];
m_Tray = pTray;
m_hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
if (!m_hEvent)
return E_FAIL;
hThread = CreateThread(NULL, 0, s_DesktopThreadProc, (PVOID)this, 0, NULL);
if (!hThread)
{
CloseHandle(m_hEvent);
return E_FAIL;
}
Handles[0] = hThread;
Handles[1] = m_hEvent;
for (;;)
{
DWORD WaitResult = MsgWaitForMultipleObjects(_countof(Handles), Handles, FALSE, INFINITE, QS_ALLEVENTS);
if (WaitResult == WAIT_OBJECT_0 + _countof(Handles))
{
TrayProcessMessages(m_Tray);
}
else if (WaitResult != WAIT_FAILED && WaitResult != WAIT_OBJECT_0)
{
break;
}
}
CloseHandle(hThread);
CloseHandle(m_hEvent);
return S_OK;
}
void Destroy()
{
return;
}
};
HANDLE
DesktopCreateWindow(IN OUT ITrayWindow *Tray)
{
CDesktopThread* pDesktopThread = new CDesktopThread();
HRESULT hres = pDesktopThread->Initialize(Tray);
if (FAILED_UNEXPECTEDLY(hres))
{
delete pDesktopThread;
return NULL;
}
return pDesktopThread;
}
VOID
DesktopDestroyShellWindow(IN HANDLE hDesktop)
{
CDesktopThread* pDesktopThread = reinterpret_cast<CDesktopThread*>(hDesktop);
pDesktopThread->Destroy();
}