source: trunk/Utilities/RotView/MainDialog.h @ 937

Last change on this file since 937 was 494, checked in by roman, 9 years ago

VS2013 upgrade and Windows 8 fix

File size: 11.1 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2012-2013
3// Created by Roman Ryltsov roman@alax.info
4
5#pragma once
6
7#include <atlsecurity.h>
8#include "AboutDialog.h"
9
10static BOOL IsAdministrator()
11{
12        bool bIsMember = FALSE;
13        return CAccessToken().CheckTokenMembership(Sids::Admins(), &bIsMember) && bIsMember;
14}
15
16////////////////////////////////////////////////////////////
17// CMainDialog
18
19class CMainDialog : 
20        public CDialogImpl<CMainDialog>,
21        public CDialogResize<CMainDialog>
22{
23public:
24        enum { IDD = IDD_MAIN };
25
26BEGIN_MSG_MAP_EX(CMainDialog)
27        //CHAIN_MSG_MAP(CDialogImpl<CMainDialog>)
28        CHAIN_MSG_MAP(CDialogResize<CMainDialog>)
29        MSG_WM_INITDIALOG(OnInitDialog)
30        MSG_WM_DESTROY(OnDestroy)
31        MSG_LVN_GETDISPINFO(IDC_MAIN_ENTRY, OnEntryListViewGetDispInfo)
32        MSG_LVN_GETINFOTIP(IDC_MAIN_ENTRY, OnEntryListViewGetInfoTip)
33        MSG_LVN_ITEMCHANGED(IDC_MAIN_ENTRY, OnEntryListViewItemChanged)
34        MSG_LVN_DBLCLK(IDC_MAIN_ENTRY, OnEntryListViewDblClk)
35        MSG_WM_SYSCOMMAND(OnSysCommand)
36        COMMAND_ID_HANDLER_EX(IDCANCEL, OnCommand)
37        COMMAND_ID_HANDLER_EX(IDC_MAIN_REFRESH, OnRefreshButtonClicked)
38        COMMAND_ID_HANDLER_EX(IDC_MAIN_PROBE, OnProbeButtonClicked)
39        REFLECT_NOTIFICATIONS()
40END_MSG_MAP()
41
42BEGIN_DLGRESIZE_MAP(CMainDialog)
43        DLGRESIZE_CONTROL(IDC_MAIN_ENTRY, DLSZ_SIZE_X | DLSZ_SIZE_Y)
44        DLGRESIZE_CONTROL(IDC_MAIN_REFRESH, DLSZ_MOVE_Y)
45        DLGRESIZE_CONTROL(IDC_MAIN_PROBE, DLSZ_MOVE_Y)
46        DLGRESIZE_CONTROL(IDCANCEL, DLSZ_MOVE_X | DLSZ_MOVE_Y)
47END_DLGRESIZE_MAP()
48
49public:
50
51        ////////////////////////////////////////////////////////
52        // CEntry
53
54        class CEntry
55        {
56        public:
57                CComPtr<IMoniker> m_pMoniker;
58                CStringW m_sDisplayName;
59                ULONGLONG m_nLastChangeTime;
60                CString m_sClassDescription;
61                CString m_sApplicationDescription;
62                CPath m_sPath;
63                HRESULT m_nGetObjectResult;
64
65        public:
66        // CEntry
67                CEntry() :
68                        m_nLastChangeTime(0),
69                        m_nGetObjectResult(S_FALSE)
70                {
71                }
72                CString GetGetObjectResultString() const
73                {
74                        CString sString;
75                        switch(m_nGetObjectResult)
76                        {
77                        case S_OK:
78                                sString = _T("Succeeded");
79                                break;
80                        case S_FALSE:
81                                //sString = ...
82                                break;
83                        default:
84                                sString = AtlFormatSystemMessage(m_nGetObjectResult).TrimRight(_T("\t\n\r ."));
85                                if(sString.IsEmpty())
86                                        sString = AtlFormatString(_T("Error 0x%08x"), m_nGetObjectResult);
87                                else
88                                        sString = AtlFormatString(_T("%s (0x%08x)"), sString, m_nGetObjectResult);
89                        }
90                        return sString;
91                }
92        };
93
94private:
95        CRoListViewT<CEntry, CRoListControlDataTraitsT> m_EntryListView;
96        CButton m_ProbeButton;
97        CComPtr<IRunningObjectTable> m_pRunningObjectTable;
98
99public:
100// CMainDialog
101        CMainDialog()
102        {
103        }
104        VOID UpdateEntryListView()
105        {
106                CRoArrayT<CEntry> EntryArray;
107                {
108                        CComPtr<IRunningObjectTable> pRunningObjectTable;
109                        __C(GetRunningObjectTable(0, &pRunningObjectTable));
110                        CComPtr<IEnumMoniker> pEnumMoniker;
111                        __C(pRunningObjectTable->EnumRunning(&pEnumMoniker));
112                        CComPtr<IMalloc> pMalloc;
113                        __C(CoGetMalloc(1, &pMalloc));
114                        CComPtr<IMoniker> pMoniker;
115                        while(pEnumMoniker->Next(1, &pMoniker, NULL) == S_OK)
116                        {
117                                _ATLTRY
118                                {
119                                        CEntry Entry;
120                                        Entry.m_pMoniker = pMoniker;
121                                        CComPtr<IBindCtx> pBindCtx;
122                                        __C(CreateBindCtx(0, &pBindCtx));
123                                        LPOLESTR pszDisplayName = NULL;
124                                        __C(pMoniker->GetDisplayName(pBindCtx, NULL, &pszDisplayName));
125                                        Entry.m_sDisplayName = pszDisplayName;
126                                        _Z4(atlTraceGeneral, 4, _T("Entry.m_sDisplayName %ls\n"), Entry.m_sDisplayName);
127                                        pMalloc->Free(pszDisplayName);
128                                        _ATLTRY
129                                        {
130                                                __C(pRunningObjectTable->GetTimeOfLastChange(pMoniker, &reinterpret_cast<FILETIME&>(Entry.m_nLastChangeTime)));
131                                                CLSID ClassIdentifier;
132                                                if(!Entry.m_sDisplayName.IsEmpty() && Entry.m_sDisplayName[0] == L'!')
133                                                        if(_PersistHelper::ClassIdentifierFromString((LPCWSTR) Entry.m_sDisplayName + 1, ClassIdentifier))
134                                                        {
135                                                                CString sClassIdentifierString(_PersistHelper::StringFromIdentifier(ClassIdentifier));
136                                                                Entry.m_sClassDescription = _RegKeyHelper::QueryStringValue(HKEY_CLASSES_ROOT, AtlFormatString(_T("CLSID\\%s"), sClassIdentifierString));
137                                                                #pragma region Application Identifier
138                                                                CString sApplicationIdentifierString = _RegKeyHelper::QueryStringValue(HKEY_CLASSES_ROOT, AtlFormatString(_T("CLSID\\%s"), sClassIdentifierString), _T("AppId"));
139                                                                CLSID ApplicationClassIdentifier;
140                                                                if(_PersistHelper::ClassIdentifierFromString(CT2CW(sApplicationIdentifierString), ApplicationClassIdentifier))
141                                                                        Entry.m_sApplicationDescription = _RegKeyHelper::QueryStringValue(HKEY_CLASSES_ROOT, AtlFormatString(_T("AppID\\%ls"), _PersistHelper::StringFromIdentifier(ApplicationClassIdentifier)));
142                                                                #pragma endregion
143                                                                #pragma region Path
144                                                                Entry.m_sPath = (LPCTSTR) _RegKeyHelper::QueryStringValue(HKEY_CLASSES_ROOT, AtlFormatString(_T("CLSID\\%s\\InprocServer32"), sClassIdentifierString));
145                                                                if(!_tcslen(Entry.m_sPath))
146                                                                        Entry.m_sPath = (LPCTSTR) _RegKeyHelper::QueryStringValue(HKEY_CLASSES_ROOT, AtlFormatString(_T("CLSID\\%s\\LocalServer32"), sClassIdentifierString));
147                                                                #pragma endregion
148                                                        }
149                                        }
150                                        _ATLCATCHALL()
151                                        {
152                                                _Z_EXCEPTION();
153                                        }
154                                        _W(EntryArray.Add(Entry) >= 0);
155                                }
156                                _ATLCATCHALL()
157                                {
158                                        _Z_EXCEPTION();
159                                }
160                                pMoniker.Release();
161                        }
162                        m_pRunningObjectTable = pRunningObjectTable;
163                }
164                m_EntryListView.DeleteAllItems();
165                for(SIZE_T nIndex = 0; nIndex < EntryArray.GetCount(); nIndex++)
166                {
167                        const INT nItem = m_EntryListView.InsertItem((INT) nIndex, EntryArray[nIndex]);
168                        _A(nItem >= 0);
169                }
170                UpdateControls();
171        }
172        VOID UpdateControls()
173        {
174                BOOL bCanProbe = FALSE;
175                if(m_pRunningObjectTable)
176                        for(INT nItem = m_EntryListView.GetNextItem(-1, LVNI_SELECTED); nItem >= 0 && !bCanProbe; nItem = m_EntryListView.GetNextItem(nItem, LVNI_SELECTED))
177                        {
178                                CEntry& Entry = m_EntryListView.GetItemData(nItem);
179                                if(Entry.m_pMoniker)
180                                        bCanProbe = TRUE;
181                        }
182                m_ProbeButton.EnableWindow(bCanProbe);
183        }
184
185// Window Message Handelrs
186        LRESULT OnInitDialog(HWND, LPARAM)
187        {
188                CWaitCursor WaitCursor;
189                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)), TRUE);
190                SetIcon(AtlLoadIconImage(IDI_MODULE, LR_DEFAULTCOLOR, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)), FALSE);
191                CMenuHandle Menu = GetSystemMenu(FALSE);
192                _W(Menu.AppendMenu(MF_SEPARATOR));
193                _W(Menu.AppendMenu(MF_STRING, ID_APP_ABOUT, _T("&About...")));
194                CAboutDialog::UpdateCaption(*this);
195                m_EntryListView.Initialize(GetDlgItem(IDC_MAIN_ENTRY));
196                //m_EntryListView.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
197                m_ProbeButton = GetDlgItem(IDC_MAIN_PROBE);
198                DlgResize_Init(TRUE);
199                #pragma region Default Size
200                CRect Position;
201                _W(GetWindowRect(Position));
202                Position.right += Position.Width() >> 1;
203                Position.bottom += Position.Height() >> 1;
204                _W(MoveWindow(Position));
205                #pragma endregion
206                _W(CenterWindow());
207                UpdateEntryListView();
208                return TRUE;
209        }
210        LRESULT OnDestroy() throw()
211        {
212                m_pRunningObjectTable.Release();
213                return 0;
214        }
215        LRESULT OnEntryListViewGetDispInfo(NMLVDISPINFO* pHeader)
216        {
217                if(pHeader->item.mask & LVIF_TEXT)
218                {
219                        const CEntry& Entry = m_EntryListView.DataFromParameter(pHeader->item.lParam);
220                        INT nSubItem = pHeader->item.iSubItem;
221                        CString& sTextBuffer = m_EntryListView.GetTextBufferString(TRUE);
222                        switch(nSubItem)
223                        {
224                        case 1: // Class Description
225                                sTextBuffer = Entry.m_sClassDescription;
226                                break;
227                        case 2: // Probe Result
228                                sTextBuffer = Entry.GetGetObjectResultString();
229                                break;
230                        case 3: // Change Time
231                                if(Entry.m_nLastChangeTime)
232                                {
233                                        FILETIME LocalTime;
234                                        _W(FileTimeToLocalFileTime(&reinterpret_cast<const FILETIME&>(Entry.m_nLastChangeTime), &LocalTime));
235                                        sTextBuffer = _StringHelper::FormatDateTime(LocalTime);
236                                }
237                                break;
238                        default: // Display Name
239                                sTextBuffer = CString(Entry.m_sDisplayName);
240                        }
241                        sTextBuffer.TrimRight(_T("\t\n\r ."));
242                        pHeader->item.pszText = m_EntryListView.GetTextBuffer();
243                }
244                //if(pHeader->item.mask & LVIF_IMAGE)
245                //      pHeader->item.iImage = 0;
246                return 0;
247        }
248        LRESULT OnEntryListViewGetInfoTip(NMLVGETINFOTIP* pHeader)
249        {
250                const CEntry& Entry = m_EntryListView.GetItemData(pHeader->iItem);
251                CString& sTextBuffer = m_EntryListView.GetTextBufferString(TRUE);
252                sTextBuffer.AppendFormat(_T("Display Name: %s\r\n"), Entry.m_sDisplayName);
253                if(!Entry.m_sClassDescription.IsEmpty())
254                        sTextBuffer.AppendFormat(_T("Class Description: %s\r\n"), Entry.m_sClassDescription);
255                if(!Entry.m_sApplicationDescription.IsEmpty())
256                        sTextBuffer.AppendFormat(_T("Application Description: %s\r\n"), Entry.m_sApplicationDescription);
257                if(_tcslen(Entry.m_sPath))
258                        sTextBuffer.AppendFormat(_T("Path: %s\r\n"), Entry.m_sPath);
259                if(Entry.m_nLastChangeTime)
260                {
261                        FILETIME LocalTime;
262                        _W(FileTimeToLocalFileTime(&reinterpret_cast<const FILETIME&>(Entry.m_nLastChangeTime), &LocalTime));
263                        CString sTime = _StringHelper::FormatDateTime(LocalTime);
264                        //sTime.AppendFormat(_T(" (%d seconds ago)"), (ULONG) ((CMsAccurateFileTime::GetTime() - Entry.m_nLastChangeTime) / (1000 * 10000i64)));
265                        sTextBuffer.AppendFormat(_T("Last Change Time: %s\r\n"), sTime);
266                }
267                if(Entry.m_nGetObjectResult != S_FALSE)
268                        sTextBuffer.AppendFormat(_T("Probe Result: %s\r\n"), Entry.GetGetObjectResultString());
269                sTextBuffer.TrimRight(_T("\t\n\r ."));
270                _tcsncpy_s(pHeader->pszText, pHeader->cchTextMax, m_EntryListView.GetTextBuffer(), _TRUNCATE);
271                #pragma region Clipboard
272                if(GetKeyState(VK_CONTROL) < 0 && GetKeyState(VK_SHIFT) < 0)
273                {
274                        SetClipboardText(m_hWnd, sTextBuffer);
275                        MessageBeep(MB_OK);
276                }
277                #pragma endregion
278                return 0;
279        }
280        LRESULT OnEntryListViewItemChanged(NMLISTVIEW* pHeader)
281        {
282                UpdateControls();
283                return 0;
284        }
285        LRESULT OnEntryListViewDblClk(NMITEMACTIVATE*)
286        {
287                m_ProbeButton.Click();
288                return 0;
289        }
290        LRESULT OnSysCommand(UINT nCommand, CPoint)
291        {
292                switch(nCommand)
293                {
294                case ID_APP_ABOUT:
295                        {
296                                CAboutDialog Dialog;
297                                Dialog.DoModal(m_hWnd);
298                        }
299                        break;
300                default:
301                        SetMsgHandled(FALSE);
302                }
303                return 0;
304        }
305        LRESULT OnCommand(UINT, INT nIdentifier, HWND)
306        {
307                _W(EndDialog(nIdentifier));
308                return 0;
309        }
310        LRESULT OnRefreshButtonClicked(UINT, INT, HWND)
311        {
312                CWaitCursor WaitCursor;
313                UpdateEntryListView();
314                return 0;
315        }
316        LRESULT OnProbeButtonClicked(UINT, INT, HWND)
317        {
318                CWaitCursor WaitCursor;
319                if(m_pRunningObjectTable)
320                {
321                        BOOL bUpdated = FALSE;
322                        for(INT nItem = m_EntryListView.GetNextItem(-1, LVNI_SELECTED); nItem >= 0; nItem = m_EntryListView.GetNextItem(nItem, LVNI_SELECTED))
323                        {
324                                CEntry& Entry = m_EntryListView.GetItemData(nItem);
325                                _ATLTRY
326                                {
327                                        if(Entry.m_pMoniker)
328                                        {
329                                                CComPtr<IUnknown> pUnknown;
330                                                Entry.m_nGetObjectResult = m_pRunningObjectTable->GetObject(Entry.m_pMoniker, &pUnknown);
331                                                bUpdated = TRUE;
332                                        }
333                                }
334                                _ATLCATCH(Exception)
335                                {
336                                        _Z_ATLEXCEPTION(Exception);
337                                        Entry.m_nGetObjectResult = (HRESULT) Exception;
338                                        bUpdated = TRUE;
339                                }
340                        }
341                        if(bUpdated)
342                                m_EntryListView.RedrawItems(0, m_EntryListView.GetItemCount() - 1);
343                }
344                return 0;
345        }
346};
Note: See TracBrowser for help on using the repository browser.