winhttp: Added missing LeaveCriticalSection (Coverity).
[wine] / programs / oleview / oleview.c
1 /*
2  * OleView (oleview.c)
3  *
4  * Copyright 2006 Piotr Caban
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 St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "main.h"
22 #include "wine/unicode.h"
23
24 GLOBALS globals;
25 static const WCHAR wszRegEdit[] = { '\\','r','e','g','e','d','i','t','.','e','x','e','\0' };
26 static WCHAR wszFormat[] = { '<','o','b','j','e','c','t','\n',' ',' ',' ',
27     'c','l','a','s','s','i','d','=','\"','c','l','s','i','d',':','%','s','\"','\n',
28     '>','\n','<','/','o','b','j','e','c','t','>','\0' };
29
30 static INT_PTR CALLBACK SysConfProc(HWND hDlgWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
31 {
32     HKEY hKey;
33     WCHAR buffer[MAX_LOAD_STRING];
34     DWORD bufSize;
35
36     WCHAR wszReg[] = { 'S','o','f','t','w','a','r','e','\\',
37         'M','i','c','r','o','s','o','f','t','\\','O','L','E','\\','\0' };
38     WCHAR wszEnableDCOM[] = { 'E','n','a','b','l','e','D','C','O','M','\0' };
39     WCHAR wszEnableRemote[] = { 'E','n','a','b','l','e',
40         'R','e','m','o','t','e','C','o','n','n','e','c','t','\0' };
41     WCHAR wszYes[] = { 'Y', '\0' };
42     WCHAR wszNo[] = { 'N', '\0' };
43
44     switch(uMsg)
45     {
46         case WM_INITDIALOG:
47             if(RegOpenKeyW(HKEY_LOCAL_MACHINE, wszReg, &hKey) != ERROR_SUCCESS)
48                 RegCreateKeyW(HKEY_LOCAL_MACHINE, wszReg, &hKey);
49
50             bufSize = sizeof(buffer);
51             if(RegGetValueW(hKey, NULL, wszEnableDCOM, RRF_RT_REG_SZ,
52                         NULL, buffer, &bufSize) != ERROR_SUCCESS)
53             {
54                 bufSize = sizeof(wszYes);
55                 RegSetValueExW(hKey, wszEnableDCOM, 0, REG_SZ, (BYTE*)wszYes, bufSize);
56             }
57
58             CheckDlgButton(hDlgWnd, IDC_ENABLEDCOM,
59                     buffer[0]=='Y' ? BST_CHECKED : BST_UNCHECKED);
60
61             bufSize = sizeof(buffer);
62             if(RegGetValueW(hKey, NULL, wszEnableRemote, RRF_RT_REG_SZ,
63                         NULL, buffer, &bufSize) != ERROR_SUCCESS)
64             {
65                 bufSize = sizeof(wszYes);
66                 RegSetValueExW(hKey, wszEnableRemote, 0, REG_SZ, (BYTE*)wszYes, bufSize);
67             }
68
69             CheckDlgButton(hDlgWnd, IDC_ENABLEREMOTE,
70                     buffer[0]=='Y' ? BST_CHECKED : BST_UNCHECKED);
71             
72             RegCloseKey(hKey);
73             return TRUE;
74         case WM_COMMAND:
75             switch(LOWORD(wParam)) {
76             case IDOK:
77                 bufSize = sizeof(wszYes);
78
79                 RegOpenKeyW(HKEY_LOCAL_MACHINE, wszReg, &hKey);
80
81                 RegSetValueExW(hKey, wszEnableDCOM, 0, REG_SZ,
82                         IsDlgButtonChecked(hDlgWnd, IDC_ENABLEDCOM) == BST_CHECKED ?
83                         (BYTE*)wszYes : (BYTE*)wszNo, bufSize);
84
85                 RegSetValueExW(hKey, wszEnableRemote, 0, REG_SZ,
86                         IsDlgButtonChecked(hDlgWnd, IDC_ENABLEREMOTE) == BST_CHECKED ?
87                         (BYTE*)wszYes : (BYTE*)wszNo, bufSize);
88
89                 RegCloseKey(hKey);
90
91                 EndDialog(hDlgWnd, IDOK);
92                 return TRUE;
93             case IDCANCEL:
94                 EndDialog(hDlgWnd, IDCANCEL);
95                 return TRUE;
96             }
97     }
98
99     return FALSE;
100 }
101
102 static INT_PTR CALLBACK CreateInstOnProc(HWND hDlgWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
103 {
104     HWND hEdit;
105
106     switch(uMsg)
107     {
108         case WM_COMMAND:
109             switch(LOWORD(wParam)) {
110             case IDOK:
111                 memset(globals.wszMachineName, 0, sizeof(WCHAR[MAX_LOAD_STRING]));
112                 hEdit = GetDlgItem(hDlgWnd, IDC_MACHINE);
113
114                 if (GetWindowTextLengthW(hEdit)>0)
115                     GetWindowTextW(hEdit, globals.wszMachineName, MAX_LOAD_STRING);
116
117                 EndDialog(hDlgWnd, IDOK);
118                 return TRUE;
119             case IDCANCEL:
120                 EndDialog(hDlgWnd, IDCANCEL);
121                 return TRUE;
122             }
123     }
124
125     return FALSE;
126 }
127
128 static void InitOpenFileName(HWND hWnd, OPENFILENAMEW *pofn, WCHAR *wszFilter,
129         WCHAR *wszTitle, WCHAR *wszFileName)
130 {
131     memset(pofn, 0, sizeof(OPENFILENAMEW));
132     pofn->lStructSize = sizeof(OPENFILENAMEW);
133     pofn->hwndOwner = hWnd;
134     pofn->hInstance = globals.hMainInst;
135
136     pofn->lpstrTitle = wszTitle;
137     pofn->lpstrFilter = wszFilter;
138     pofn->nFilterIndex = 0;
139     pofn->lpstrFile = wszFileName;
140     pofn->nMaxFile = MAX_LOAD_STRING;
141     pofn->Flags = OFN_HIDEREADONLY | OFN_ENABLESIZING;
142 }
143
144 static void CopyClsid(HTREEITEM item)
145 {
146     TVITEMW tvi;
147
148     memset(&tvi, 0, sizeof(TVITEMW));
149     tvi.hItem = item;
150     tvi.cchTextMax = MAX_LOAD_STRING;
151     SendMessageW(globals.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
152
153     if(OpenClipboard(globals.hMainWnd) && EmptyClipboard() && tvi.lParam)
154     {
155         HANDLE hClipData = GlobalAlloc(GHND, sizeof(WCHAR[MAX_LOAD_STRING]));
156         LPVOID pLoc = GlobalLock(hClipData);
157
158         lstrcpyW(pLoc, ((ITEM_INFO *)tvi.lParam)->clsid);
159         GlobalUnlock(hClipData);
160         hClipData = SetClipboardData(CF_UNICODETEXT, hClipData);
161         CloseClipboard();
162     }
163 }
164
165 static void CopyHTMLTag(HTREEITEM item)
166 {
167     TVITEMW tvi;
168
169     memset(&tvi, 0, sizeof(TVITEMW));
170     tvi.hItem = item;
171     tvi.cchTextMax = MAX_LOAD_STRING;
172     SendMessageW(globals.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
173
174     if(OpenClipboard(globals.hMainWnd) && EmptyClipboard() && tvi.lParam)
175     {
176         HANDLE hClipData = GlobalAlloc(GHND, sizeof(WCHAR[MAX_LOAD_STRING]));
177         LPVOID pLoc = GlobalLock(hClipData);
178         int clsidLen = lstrlenW(((ITEM_INFO *)tvi.lParam)->clsid)-1;
179
180         ((ITEM_INFO *)tvi.lParam)->clsid[clsidLen] = '\0';
181         wsprintfW(pLoc, wszFormat, ((ITEM_INFO *)tvi.lParam)->clsid+1);
182         ((ITEM_INFO *)tvi.lParam)->clsid[clsidLen] = '}';
183
184         GlobalUnlock(hClipData);
185         hClipData = SetClipboardData(CF_UNICODETEXT, hClipData);
186         CloseClipboard();
187     }
188 }
189
190 static void ResizeChild(void)
191 {
192     RECT client, stat, tool;
193
194     MoveWindow(globals.hStatusBar, 0, 0, 0, 0, TRUE);
195     MoveWindow(globals.hToolBar, 0, 0, 0, 0, TRUE);
196
197     if(IsWindowVisible(globals.hStatusBar))
198         GetClientRect(globals.hStatusBar, &stat);
199     else stat.bottom = 0;
200
201     if(IsWindowVisible(globals.hToolBar))
202     {
203         GetClientRect(globals.hToolBar, &tool);
204         tool.bottom += 2;
205     }
206     else tool.bottom = 0;
207
208     GetClientRect(globals.hMainWnd, &client);
209     MoveWindow(globals.hPaneWnd, 0, tool.bottom,
210             client.right, client.bottom-tool.bottom-stat.bottom, TRUE);
211 }
212
213 void RefreshMenu(HTREEITEM item)
214 {
215     TVITEMW tvi;
216     HTREEITEM parent;
217     HMENU hMenu = GetMenu(globals.hMainWnd);
218
219     memset(&tvi, 0, sizeof(TVITEMW));
220     tvi.hItem = item;
221     SendMessageW(globals.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
222
223     parent = (HTREEITEM)SendMessageW(globals.hTree, TVM_GETNEXTITEM,
224             TVGN_PARENT, (LPARAM)item);
225
226     SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_CREATEINST, FALSE);
227     SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_RELEASEINST, FALSE);
228     SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_VIEW, FALSE);
229
230     if(tvi.lParam && ((ITEM_INFO *)tvi.lParam)->cFlag&SHOWALL)
231     {
232         EnableMenuItem(hMenu, IDM_COPYCLSID, MF_ENABLED);
233         EnableMenuItem(hMenu, IDM_HTMLTAG, MF_ENABLED);
234         EnableMenuItem(hMenu, IDM_VIEW, MF_GRAYED);
235
236         if(!((ITEM_INFO *)tvi.lParam)->loaded)
237         {
238             EnableMenuItem(hMenu, IDM_CREATEINST, MF_ENABLED);
239             EnableMenuItem(hMenu, IDM_CREATEINSTON, MF_ENABLED);
240             EnableMenuItem(hMenu, IDM_RELEASEINST, MF_GRAYED);
241             SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_CREATEINST, TRUE);
242         }
243         else 
244         {
245             EnableMenuItem(hMenu, IDM_CREATEINST, MF_GRAYED);
246             EnableMenuItem(hMenu, IDM_CREATEINSTON, MF_GRAYED);
247             EnableMenuItem(hMenu, IDM_RELEASEINST, MF_ENABLED);
248             SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_RELEASEINST, TRUE);
249         }
250     }
251     else if(tvi.lParam && 
252             (((ITEM_INFO *)tvi.lParam)->cFlag&INTERFACE || parent==tree.hTL))
253     {
254         EnableMenuItem(hMenu, IDM_TYPEINFO, MF_GRAYED);
255         EnableMenuItem(hMenu, IDM_CREATEINST, MF_GRAYED);
256         EnableMenuItem(hMenu, IDM_CREATEINSTON, MF_GRAYED);
257         EnableMenuItem(hMenu, IDM_RELEASEINST, MF_GRAYED);
258         EnableMenuItem(hMenu, IDM_COPYCLSID, MF_ENABLED);
259         EnableMenuItem(hMenu, IDM_HTMLTAG, MF_GRAYED);
260         EnableMenuItem(hMenu, IDM_VIEW, MF_ENABLED);
261         SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_VIEW, TRUE);
262     }
263     else
264     {
265         EnableMenuItem(hMenu, IDM_TYPEINFO, MF_GRAYED);
266         EnableMenuItem(hMenu, IDM_CREATEINST, MF_GRAYED);
267         EnableMenuItem(hMenu, IDM_CREATEINSTON, MF_GRAYED);
268         EnableMenuItem(hMenu, IDM_RELEASEINST, MF_GRAYED);
269         EnableMenuItem(hMenu, IDM_COPYCLSID, MF_GRAYED);
270         EnableMenuItem(hMenu, IDM_HTMLTAG, MF_GRAYED);
271         EnableMenuItem(hMenu, IDM_VIEW, MF_GRAYED);
272     }
273
274     if(parent==tree.hAID || parent==tree.hGBCC)
275         EnableMenuItem(hMenu, IDM_COPYCLSID, MF_ENABLED);
276 }
277
278 static int MenuCommand(WPARAM wParam, HWND hWnd)
279 {
280     BOOL vis;
281     HTREEITEM hSelect;
282     WCHAR wszAbout[MAX_LOAD_STRING];
283     WCHAR wszAboutVer[MAX_LOAD_STRING];
284
285     switch(wParam)
286     {
287         case IDM_ABOUT:
288             LoadStringW(globals.hMainInst, IDS_ABOUT, wszAbout,
289                     sizeof(wszAbout)/sizeof(wszAbout[0]));
290             LoadStringW(globals.hMainInst, IDS_ABOUTVER, wszAboutVer,
291                     sizeof(wszAboutVer)/sizeof(wszAboutVer[0]));
292             ShellAboutW(hWnd, wszAbout, wszAboutVer, NULL);
293             break;
294         case IDM_COPYCLSID:
295             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
296                     TVM_GETNEXTITEM, TVGN_CARET, 0);
297             CopyClsid(hSelect);
298             break;
299         case IDM_HTMLTAG:
300             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
301                     TVM_GETNEXTITEM, TVGN_CARET, 0);
302             CopyHTMLTag(hSelect);
303             break;
304         case IDM_CREATEINST:
305             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
306                     TVM_GETNEXTITEM, TVGN_CARET, 0);
307             CreateInst(hSelect, NULL);
308             SendMessageW(globals.hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hSelect);
309             break;
310         case IDM_CREATEINSTON:
311             if(DialogBoxW(0, MAKEINTRESOURCEW(DLG_CREATEINSTON),
312                         hWnd, CreateInstOnProc) == IDCANCEL) break;
313             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
314                     TVM_GETNEXTITEM, TVGN_CARET, 0);
315             CreateInst(hSelect, globals.wszMachineName);
316             SendMessageW(globals.hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hSelect);
317             break;
318         case IDM_RELEASEINST:
319             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
320                     TVM_GETNEXTITEM, TVGN_CARET, 0);
321             ReleaseInst(hSelect);
322             RefreshMenu(hSelect);
323             RefreshDetails(hSelect);
324             break;
325         case IDM_EXPERT:
326             globals.bExpert = !globals.bExpert;
327             CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
328                     globals.bExpert ? MF_CHECKED : MF_UNCHECKED);
329             EmptyTree();
330             if(globals.bExpert) AddTreeEx();
331             else AddTree();
332             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
333                     TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT);
334             SendMessageW(globals.hTree, TVM_SELECTITEM, 0, (LPARAM)hSelect);
335             RefreshMenu(hSelect);
336             break;
337         case IDM_FLAG_INSERV:
338             vis = globals.dwClsCtx&CLSCTX_INPROC_SERVER;
339             globals.dwClsCtx = globals.dwClsCtx&(~CLSCTX_INPROC_SERVER);
340             globals.dwClsCtx = globals.dwClsCtx|((~vis)&CLSCTX_INPROC_SERVER);
341             if(!globals.dwClsCtx) globals.dwClsCtx = vis;
342             else CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
343                     vis ? MF_UNCHECKED : MF_CHECKED);
344             break;
345         case IDM_FLAG_INHANDL:
346             vis = globals.dwClsCtx&CLSCTX_INPROC_HANDLER;
347             globals.dwClsCtx = globals.dwClsCtx&(~CLSCTX_INPROC_HANDLER);
348             globals.dwClsCtx = globals.dwClsCtx|((~vis)&CLSCTX_INPROC_HANDLER);
349             if(!globals.dwClsCtx) globals.dwClsCtx = vis;
350             else CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
351                     vis ? MF_UNCHECKED : MF_CHECKED);
352             break;
353         case IDM_FLAG_LOCSERV:
354             vis = globals.dwClsCtx&CLSCTX_LOCAL_SERVER;
355             globals.dwClsCtx = globals.dwClsCtx&(~CLSCTX_LOCAL_SERVER);
356             globals.dwClsCtx = globals.dwClsCtx|((~vis)&CLSCTX_LOCAL_SERVER);
357             if(!globals.dwClsCtx) globals.dwClsCtx = vis;
358             else CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
359                     vis ? MF_UNCHECKED : MF_CHECKED);
360             break;
361         case IDM_FLAG_REMSERV:
362             vis = globals.dwClsCtx&CLSCTX_REMOTE_SERVER;
363             globals.dwClsCtx = globals.dwClsCtx&(~CLSCTX_REMOTE_SERVER);
364             globals.dwClsCtx = globals.dwClsCtx|((~vis)&CLSCTX_REMOTE_SERVER);
365             if(!globals.dwClsCtx) globals.dwClsCtx = vis;
366             else CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
367                     vis ? MF_UNCHECKED : MF_CHECKED);
368             break;
369         case IDM_REFRESH:
370             EmptyTree();
371             if(globals.bExpert) AddTreeEx();
372             else AddTree();
373             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
374                     TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT);
375             SendMessageW(globals.hTree, TVM_SELECTITEM, 0, (LPARAM)hSelect);
376             RefreshMenu(hSelect);
377             break;
378         case IDM_REGEDIT:
379         {
380             STARTUPINFOW si;
381             PROCESS_INFORMATION pi;
382             WCHAR app[MAX_PATH];
383
384             GetWindowsDirectoryW( app, MAX_PATH - sizeof(wszRegEdit)/sizeof(WCHAR) );
385             lstrcatW( app, wszRegEdit );
386             memset(&si, 0, sizeof(si));
387             si.cb = sizeof(si);
388             if (CreateProcessW(app, app, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
389             {
390                 CloseHandle(pi.hProcess);
391                 CloseHandle(pi.hThread);
392             }
393             break;
394         }
395         case IDM_STATUSBAR:
396             vis = IsWindowVisible(globals.hStatusBar);
397             ShowWindow(globals.hStatusBar, vis ? SW_HIDE : SW_SHOW);
398             CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
399                     vis ? MF_UNCHECKED : MF_CHECKED);
400             ResizeChild();
401             break;
402         case IDM_SYSCONF:
403             DialogBoxW(0, MAKEINTRESOURCEW(DLG_SYSCONF), hWnd, SysConfProc);
404             break;
405         case IDM_TOOLBAR:
406             vis = IsWindowVisible(globals.hToolBar);
407             ShowWindow(globals.hToolBar, vis ? SW_HIDE : SW_SHOW);
408             CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
409                     vis ? MF_UNCHECKED : MF_CHECKED);
410             ResizeChild();
411             break;
412         case IDM_TYPELIB:
413             {
414             static const WCHAR filterW[] = {'%','s','%','c','*','.','t','l','b',';','*','.','o','l','b',';','*','.','d','l','l',';','*','.','o','c','x',';','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0};
415             OPENFILENAMEW ofn;
416             static WCHAR wszTitle[MAX_LOAD_STRING];
417             static WCHAR wszName[MAX_LOAD_STRING];
418             WCHAR filter_typelib[MAX_LOAD_STRING], filter_all[MAX_LOAD_STRING], filter[MAX_PATH];
419
420             LoadStringW(globals.hMainInst, IDS_OPEN, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0]));
421             LoadStringW(globals.hMainInst, IDS_OPEN_FILTER_TYPELIB, filter_typelib, sizeof(filter_typelib)/sizeof(WCHAR));
422             LoadStringW(globals.hMainInst, IDS_OPEN_FILTER_ALL, filter_all, sizeof(filter_all)/sizeof(WCHAR));
423             snprintfW( filter, MAX_PATH, filterW, filter_typelib, 0, 0, filter_all, 0, 0 );
424             InitOpenFileName(hWnd, &ofn, filter, wszTitle, wszName);
425             if(GetOpenFileNameW(&ofn)) CreateTypeLibWindow(globals.hMainInst, wszName);
426             break;
427             }
428         case IDM_VIEW:
429             hSelect = (HTREEITEM)SendMessageW(globals.hTree,
430                     TVM_GETNEXTITEM, TVGN_CARET, 0);
431             if(IsInterface(hSelect)) InterfaceViewer(hSelect);
432             else CreateTypeLibWindow(globals.hMainInst, NULL);
433             break;
434         case IDM_EXIT:
435             DestroyWindow(hWnd);
436             break;
437     }
438     return 0;
439 }
440
441 static void UpdateStatusBar(int itemID)
442 {
443     WCHAR info[MAX_LOAD_STRING];
444
445     if(!LoadStringW(globals.hMainInst, itemID, info, sizeof(info)/sizeof(info[0])))
446         LoadStringW(globals.hMainInst, IDS_READY, info, sizeof(info)/sizeof(info[0]));
447
448     SendMessageW(globals.hStatusBar, SB_SETTEXTW, 0, (LPARAM)info);
449 }
450
451 static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg,
452         WPARAM wParam, LPARAM lParam)
453 {
454     switch(uMsg)
455     {
456         case WM_CREATE:
457             OleInitialize(NULL);
458             PaneRegisterClassW();
459             TypeLibRegisterClassW();
460             if(!CreatePanedWindow(hWnd, &globals.hPaneWnd, globals.hMainInst))
461                 PostQuitMessage(0);
462             SetLeft(globals.hPaneWnd, CreateTreeWindow(globals.hMainInst));
463             SetRight(globals.hPaneWnd, CreateDetailsWindow(globals.hMainInst));
464             SetFocus(globals.hTree);
465             break;
466         case WM_COMMAND:
467             MenuCommand(LOWORD(wParam), hWnd);
468             break;
469         case WM_DESTROY:
470             OleUninitialize();
471             EmptyTree();
472             PostQuitMessage(0);
473             break;
474         case WM_MENUSELECT:
475             UpdateStatusBar(LOWORD(wParam));
476             break;
477         case WM_SETFOCUS:
478             SetFocus(globals.hTree);
479             break;
480         case WM_SIZE:
481             if(wParam == SIZE_MINIMIZED) break;
482             ResizeChild();
483             break;
484         default:
485             return DefWindowProcW(hWnd, uMsg, wParam, lParam);
486     }
487     return 0;
488 }
489
490 static BOOL InitApplication(HINSTANCE hInst)
491 {
492     WNDCLASSW wc;
493     WCHAR wszAppName[MAX_LOAD_STRING];
494
495     LoadStringW(hInst, IDS_APPNAME, wszAppName, sizeof(wszAppName)/sizeof(wszAppName[0]));
496
497     memset(&wc, 0, sizeof(WNDCLASSW));
498     wc.lpfnWndProc = WndProc;
499     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
500     wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MENU);
501     wc.lpszClassName = wszAppName;
502
503     if(!RegisterClassW(&wc))
504         return FALSE;
505
506     return TRUE;
507 }
508
509 static BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
510 {
511     HWND hWnd;
512     WCHAR wszAppName[MAX_LOAD_STRING];
513     WCHAR wszTitle[MAX_LOAD_STRING];
514     TBBUTTON tB[] = {
515         {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
516         {0, IDM_BIND, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
517         {1, IDM_TYPELIB, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
518         {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
519         {2, IDM_REGEDIT, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
520         {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
521         {3, IDM_CREATEINST, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
522         {4, IDM_RELEASEINST, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0},
523         {0, 0, 0, BTNS_SEP, {0, 0}, 0, 0},
524         {5, IDM_VIEW, TBSTATE_ENABLED, BTNS_BUTTON, {0, 0}, 0, 0}
525     };
526
527     LoadStringW(hInst, IDS_APPNAME, wszAppName, sizeof(wszAppName)/sizeof(wszAppName[0]));
528     LoadStringW(hInst, IDS_APPTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0]));
529
530     hWnd = CreateWindowW(wszAppName, wszTitle, WS_OVERLAPPEDWINDOW,
531             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);
532     if(!hWnd) return FALSE;
533
534     globals.hStatusBar = CreateStatusWindowW(WS_VISIBLE|WS_CHILD,
535             wszTitle, hWnd, 0);
536
537     globals.hToolBar = CreateToolbarEx(hWnd, WS_CHILD|WS_VISIBLE, 0, 1, hInst,
538             IDB_TOOLBAR, tB, 10, 16, 16, 16, 16, sizeof(TBBUTTON));
539     SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_CREATEINST, FALSE);
540     SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_RELEASEINST, FALSE);
541     SendMessageW(globals.hToolBar, TB_ENABLEBUTTON, IDM_VIEW, FALSE);
542
543     globals.hMainWnd = hWnd;
544     globals.hMainInst = hInst;
545     globals.bExpert = TRUE;
546     globals.dwClsCtx = CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER;
547
548     ShowWindow(hWnd, nCmdShow);
549     UpdateWindow(hWnd);
550
551     return TRUE;
552 }
553
554 int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
555 {
556     MSG msg;
557     HANDLE hAccelTable;
558    
559     if(!hPrevInst)
560     {
561         if(!InitApplication(hInst))
562             return FALSE;
563     }
564
565     if(!InitInstance(hInst, nCmdShow))
566         return FALSE;
567
568     hAccelTable = LoadAcceleratorsW(hInst, MAKEINTRESOURCEW(IDA_OLEVIEW));
569
570     while(GetMessageW(&msg, NULL, 0, 0))
571     {
572         if(TranslateAcceleratorW(globals.hMainWnd, hAccelTable, &msg)) continue;
573
574         TranslateMessage(&msg);
575         DispatchMessageW(&msg);
576     }
577
578     return msg.wParam;
579 }