Debian Codegraph @

Contrib/Banner/Banner.c

raw
1 #include <windows.h>
2 #include <nsis/pluginapi.h> // nsis plugin
3 #include "../../Source/exehead/resource.h"
4
5 // Unicode support added by Jim Park -- 08/02/2007
6
7 HINSTANCE hInstance;
8 HWND hwBanner;
9 HANDLE hThread;
10 BOOL bFailed;
11
12 TCHAR buf[1024];
13
14 INT_PTR CALLBACK BannerProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
15 {
16 if (uMsg == WM_INITDIALOG)
17 {
18 int iMainStringSet = 0;
19
20 popstring(buf);
21 while (lstrcmp(buf, _T("/set")) == 0)
22 {
23 unsigned int id;
24 popstring(buf);
25 id = myatou(buf);
26 popstring(buf);
27 SetDlgItemText(hwndDlg, id, buf);
28 popstring(buf);
29
30 if (id == IDC_STR)
31 iMainStringSet++;
32 }
33
34 SetWindowText(hwndDlg, buf);
35 if (!iMainStringSet)
36 SetDlgItemText(hwndDlg, IDC_STR, buf);
37
38 if (!*buf)
39 SetWindowLongPtr(hwndDlg, GWL_EXSTYLE, GetWindowLongPtr(hwndDlg, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
40 }
41 if (uMsg == WM_CLOSE)
42 {
43 DestroyWindow(hwndDlg);
44 }
45 return FALSE;
46 }
47
48 BOOL ProcessMessages()
49 {
50 BOOL processed = FALSE;
51 MSG msg;
52
53 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
54 {
55 DispatchMessage(&msg);
56 processed = TRUE;
57 }
58
59 return processed;
60 }
61
62 DWORD WINAPI BannerThread(LPVOID lpParameter)
63 {
64 HWND hwndParent = (HWND) lpParameter;
65 HWND lhwBanner;
66
67 // This right here is the mother of all evils when it comes to
68 // foreground windows. The dialog is created in another thread
69 // and there can only be one thread holding the right to set the
70 // foreground window. So long as this thread exists and has an
71 // active window, another thread from the same process can steal
72 // its thunder. But if the window and the thread are destroyed,
73 // the foreground rights pass on to another process. To avoid
74 // this situation that could cause the installer to show up on
75 // the background if Banner is used in .onInit, we don't let
76 // CreateDialog show the window and instead do this in the
77 // original thread. This is done by not specifying WS_VISIBLE
78 // for IDD_VERIFY.
79
80 lhwBanner = CreateDialog(
81 GetModuleHandle(0),
82 MAKEINTRESOURCE(IDD_VERIFY),
83 hwndParent,
84 BannerProc
85 );
86 if (!lhwBanner)
87 {
88 bFailed = TRUE;
89 return 0;
90 }
91
92 while (IsWindow(lhwBanner))
93 {
94 if (!ProcessMessages())
95 {
96 hwBanner = lhwBanner;
97 WaitMessage();
98 }
99 }
100
101 hwBanner = NULL;
102
103 return 0;
104 }
105
106 static UINT_PTR PluginCallback(enum NSPIM msg)
107 {
108 return 0;
109 }
110
111 void __declspec(dllexport) show(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
112 {
113 EXDLL_INIT();
114
115 extra->RegisterPluginCallback(hInstance, PluginCallback);
116
117 {
118 DWORD dwThreadId;
119 DWORD dwMainThreadId = GetCurrentThreadId();
120
121 hwBanner = NULL;
122
123 if (!IsWindowVisible(hwndParent))
124 hwndParent = 0;
125
126 bFailed = FALSE;
127
128 hThread = CreateThread(0, 0, BannerThread, (LPVOID) hwndParent, 0, &dwThreadId);
129
130 // wait for the window to initialize and for the stack operations to finish
131 while (hThread && !hwBanner && !bFailed)
132 {
133 ProcessMessages();
134 Sleep(10);
135 }
136
137 CloseHandle(hThread);
138
139 if (AttachThreadInput(dwMainThreadId, dwThreadId, TRUE))
140 {
141 // Activates and displays a window
142 ShowWindow(hwBanner, SW_SHOW);
143 AttachThreadInput(dwMainThreadId, dwThreadId, FALSE);
144 }
145 else
146 ShowWindow(hwBanner, SW_SHOW);
147 }
148 }
149
150 void __declspec(dllexport) getWindow(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop)
151 {
152 wsprintf(buf, _T("%u"), hwBanner);
153 pushstring(buf);
154 }
155
156 void __declspec(dllexport) destroy(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop)
157 {
158 if (!hwBanner)
159 return;
160
161 PostMessage(hwBanner, WM_CLOSE, 0, 0);
162
163 // Wait for the thread to finish
164 while (hwBanner)
165 {
166 ProcessMessages();
167 Sleep(25);
168 }
169 }
170
171 BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
172 {
173 hInstance = hInst;
174 if (hwBanner && ul_reason_for_call == DLL_PROCESS_DETACH)
175 {
176 destroy(0, 0, 0, 0);
177 }
178 return TRUE;
179 }