xref: /freebsd/crypto/krb5/src/windows/leash/LeashAboutBox.cpp (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 //*****************************************************************************
2 // File:	LeashAboutBox.cpp
3 // By:		Arthur David Leather
4 // Created:	12/02/98
5 // Copyright:	@1998 Massachusetts Institute of Technology - All rights
6 //              reserved.
7 // Description:	CPP file for LeashAboutBox.h. Contains variables and functions
8 //		for the Leash About Box Dialog Box
9 //
10 // History:
11 //
12 // MM/DD/YY	Inits	Description of Change
13 // 12/02/98	ADL	Original
14 //*****************************************************************************
15 
16 
17 #include "stdafx.h"
18 #include "leash.h"
19 #include "LeashAboutBox.h"
20 #include "reminder.h"
21 #include "lglobals.h"
22 #include "psapi.h"
23 
24 #ifdef _DEBUG
25 #define new DEBUG_NEW
26 #undef THIS_FILE
27 static char THIS_FILE[] = __FILE__;
28 #endif
29 
30 /////////////////////////////////////////////////////////////////////////////
31 // CLeashAboutBox dialog
32 
33 
CLeashAboutBox(CWnd * pParent)34 CLeashAboutBox::CLeashAboutBox(CWnd* pParent /*=NULL*/)
35 	: CDialog(CLeashAboutBox::IDD, pParent)
36         , m_bListModules(FALSE)
37 {
38     m_missingFileError = FALSE;
39 
40     //{{AFX_DATA_INIT(CLeashAboutBox)
41     m_fileItem = _T("");
42     //}}AFX_DATA_INIT
43 }
44 
45 
DoDataExchange(CDataExchange * pDX)46 void CLeashAboutBox::DoDataExchange(CDataExchange* pDX)
47 {
48     CDialog::DoDataExchange(pDX);
49     //{{AFX_DATA_MAP(CLeashAboutBox)
50     DDX_Control(pDX, IDC_PROPERTIES, m_propertiesButton);
51     DDX_Control(pDX, IDC_LEASH_MODULES, m_radio_LeashDLLs);
52     DDX_Control(pDX, IDC_LEASH_MODULE_LB, m_LB_DLLsLoaded);
53     DDX_LBString(pDX, IDC_LEASH_MODULE_LB, m_fileItem);
54     //}}AFX_DATA_MAP
55 }
56 
57 
58 BEGIN_MESSAGE_MAP(CLeashAboutBox, CDialog)
59     //{{AFX_MSG_MAP(CLeashAboutBox)
60     ON_WM_HSCROLL()
61     ON_LBN_SELCHANGE(IDC_LEASH_MODULE_LB, OnSelchangeLeashModuleLb)
62     ON_BN_CLICKED(IDC_ALL_MODULES, OnAllModules)
63     ON_BN_CLICKED(IDC_LEASH_MODULES, OnLeashModules)
64     ON_LBN_DBLCLK(IDC_LEASH_MODULE_LB, OnDblclkLeashModuleLb)
65     ON_BN_CLICKED(IDC_PROPERTIES, OnProperties)
66     ON_LBN_SETFOCUS(IDC_LEASH_MODULE_LB, OnSetfocusLeashModuleLb)
67     ON_BN_CLICKED(IDC_NOT_LOADED_MODULES, OnNotLoadedModules)
68     //}}AFX_MSG_MAP
69 END_MESSAGE_MAP()
70 ;
71 /////////////////////////////////////////////////////////////////////////////
72 // CLeashAboutBox message handlers
73 
OnHScroll(UINT nSBCode,UINT nPos,CScrollBar * pScrollBar)74 void CLeashAboutBox::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
75 {
76     CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
77 }
78 
GetModules95(DWORD processID,BOOL allModules)79 BOOL CLeashAboutBox::GetModules95(DWORD processID, BOOL allModules)
80 {
81     char szModNames[1024];
82     MODULEENTRY32 me32 = {0};
83     HANDLE hProcessSnap = NULL;
84 
85     hProcessSnap = pCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processID);
86     if (hProcessSnap == (HANDLE)-1)
87         return FALSE;
88 
89     me32.dwSize = sizeof(MODULEENTRY32);
90     if (pModule32First(hProcessSnap, &me32))
91     {
92         do
93         {
94             lstrcpy(szModNames, me32.szExePath);
95             strupr(szModNames);
96 
97             if (!allModules)
98             {
99                 if (!strstr(szModNames, "SYSTEM"))
100                     m_LB_DLLsLoaded.AddString(me32.szExePath);
101             }
102             else
103                 m_LB_DLLsLoaded.AddString(me32.szExePath);
104         }
105         while (pModule32Next(hProcessSnap, &me32));
106     }
107 
108     return TRUE;
109 }
110 
GetModulesNT(DWORD processID,BOOL allModules)111 void CLeashAboutBox::GetModulesNT(DWORD processID, BOOL allModules)
112 {
113     char checkName[1024];
114     HMODULE hMods[1024];
115     HANDLE hProcess;
116     DWORD cbNeeded;
117     unsigned int i;
118 
119     // Get a list of all the modules in this process.
120     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
121                            FALSE, processID);
122 
123     if (pEnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
124     {
125         for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
126         {
127             char szModName[2048];
128 
129             // Get the full path to the module's file.
130             if (pGetModuleFileNameEx(hProcess, hMods[i], szModName,
131                                      sizeof(szModName)))
132             {
133                 lstrcpy(checkName, szModName);
134                 strupr(checkName);
135 
136                 if (!allModules)
137                 {
138                     if (!strstr(checkName, "SYSTEM32"))
139                         m_LB_DLLsLoaded.AddString(szModName);
140                 }
141                 else
142                     m_LB_DLLsLoaded.AddString(szModName);
143             }
144         }
145     }
146 
147     CloseHandle(hProcess);
148 }
149 
HighlightFirstItem()150 void CLeashAboutBox::HighlightFirstItem()
151 {
152     UINT numModules = m_LB_DLLsLoaded.GetCount();
153     CHAR numModulesBuffer [25];
154     _itoa(numModules, numModulesBuffer, 10);
155 
156     if (numModules)
157     {
158         m_LB_DLLsLoaded.SetCurSel(0);
159         m_propertiesButton.EnableWindow();
160     }
161     else
162         m_propertiesButton.EnableWindow(FALSE);
163 
164     GetDlgItem(IDC_STATIC_NO_OF_MODULES)->SetWindowText(numModulesBuffer);
165 }
166 
167 DWORD
SetVersionInfo(UINT id_version,UINT id_copyright)168 CLeashAboutBox::SetVersionInfo(
169     UINT id_version,
170     UINT id_copyright
171     )
172 {
173     TCHAR filename[1024];
174     DWORD dwVersionHandle;
175     LPVOID pVersionInfo = 0;
176     DWORD retval = 0;
177     LPDWORD pLangInfo = 0;
178     LPTSTR szVersion = 0;
179     LPTSTR szCopyright = 0;
180     UINT len = 0;
181     TCHAR sname_version[] = TEXT("FileVersion");
182     TCHAR sname_copyright[] = TEXT("LegalCopyright");
183     TCHAR szVerQ[(sizeof("\\StringFileInfo\\12345678\\") +
184                   max(sizeof(sname_version) / sizeof(TCHAR),
185                       sizeof(sname_copyright) / sizeof(TCHAR)))];
186     TCHAR * cp = szVerQ;
187 
188     if (!GetModuleFileName(NULL, filename, sizeof(filename)))
189         return GetLastError();
190 
191     DWORD size = GetFileVersionInfoSize(filename, &dwVersionHandle);
192 
193     if (!size)
194         return GetLastError();
195 
196     pVersionInfo = malloc(size);
197     if (!pVersionInfo)
198         return ERROR_NOT_ENOUGH_MEMORY;
199 
200     if (!GetFileVersionInfo(filename, dwVersionHandle, size, pVersionInfo))
201     {
202         retval = GetLastError();
203         goto cleanup;
204     }
205 
206     if (!VerQueryValue(pVersionInfo, TEXT("\\VarFileInfo\\Translation"),
207                        (LPVOID*)&pLangInfo, &len))
208     {
209         retval = GetLastError();
210         goto cleanup;
211     }
212 
213 
214     cp += wsprintf(szVerQ,
215                    TEXT("\\StringFileInfo\\%04x%04x\\"),
216                    LOWORD(*pLangInfo), HIWORD(*pLangInfo));
217 
218     lstrcpy(cp, sname_version);
219     if (!VerQueryValue(pVersionInfo, szVerQ, (LPVOID*)&szVersion, &len))
220     {
221         retval = GetLastError() || ERROR_NOT_ENOUGH_MEMORY;
222         goto cleanup;
223     }
224     TCHAR version[100];
225     _sntprintf(version, sizeof(version), TEXT("MIT Kerberos Version %s"), szVersion);
226     version[sizeof(version) - 1] = 0;
227     GetDlgItem(id_version)->SetWindowText(version);
228 
229     lstrcpy(cp, sname_copyright);
230     if (!VerQueryValue(pVersionInfo, szVerQ, (LPVOID*)&szCopyright, &len))
231     {
232         retval = GetLastError() || ERROR_NOT_ENOUGH_MEMORY;
233         goto cleanup;
234     }
235     GetDlgItem(id_copyright)->SetWindowText(szCopyright);
236 
237  cleanup:
238     if (pVersionInfo)
239         free(pVersionInfo);
240     return retval;
241 }
242 
OnInitDialog()243 BOOL CLeashAboutBox::OnInitDialog()
244 {
245     CDialog::OnInitDialog();
246 
247     // XXX - we need to add some sensible behavior on error.
248     // We need to get the version info and display it...
249     SetVersionInfo(IDC_ABOUT_VERSION, IDC_ABOUT_COPYRIGHT);
250 
251     if (!CLeashApp::m_hToolHelp32 && !CLeashApp::m_hPsapi)
252         m_missingFileError = TRUE;
253 
254     if (m_bListModules) {
255         m_radio_LeashDLLs.SetCheck(TRUE);
256         OnLeashModules();
257 
258         HighlightFirstItem();
259 
260         if (!CLeashApp::m_hPsapi)
261             GetDlgItem(IDC_PROPERTIES)->EnableWindow(FALSE);
262     } else {
263         m_radio_LeashDLLs.ShowWindow(SW_HIDE);
264         GetDlgItem(IDC_NOT_LOADED_MODULES)->ShowWindow(SW_HIDE);
265         GetDlgItem(IDC_ALL_MODULES)->ShowWindow(SW_HIDE);
266         GetDlgItem(IDC_PROPERTIES)->ShowWindow(SW_HIDE);
267         GetDlgItem(IDC_STATIC_MODULES_LOADED)->ShowWindow(SW_HIDE);
268         GetDlgItem(IDC_STATIC_NO_OF_MODULES)->ShowWindow(SW_HIDE);
269         m_LB_DLLsLoaded.ShowWindow(SW_HIDE);
270         // shrink window, move 'OK' button
271         const int hideDiff = 150;
272         RECT okRect;
273         CWnd* pOK = GetDlgItem(IDOK);
274         pOK->GetWindowRect(&okRect);
275         ScreenToClient(&okRect);
276         pOK->SetWindowPos(0, okRect.left, okRect.top - hideDiff,
277                           0, 0, SWP_NOZORDER | SWP_NOSIZE);
278         RECT dlgRect;
279         GetWindowRect( &dlgRect );
280 
281         SetWindowPos(0,0,0,
282                      dlgRect.right-dlgRect.left,
283                      dlgRect.bottom-dlgRect.top - hideDiff,
284                      SWP_NOZORDER|SWP_NOMOVE);
285     }
286     return TRUE;  // return TRUE unless you set the focus to a control
287     // EXCEPTION: OCX Property Pages should return FALSE
288 }
289 
OnSelchangeLeashModuleLb()290 void CLeashAboutBox::OnSelchangeLeashModuleLb()
291 {
292 }
293 
OnAllModules()294 void CLeashAboutBox::OnAllModules()
295 {
296     if (!CLeashApp::m_hToolHelp32 && !CLeashApp::m_hPsapi)
297         return; //error
298 
299     m_LB_DLLsLoaded.ResetContent();
300 
301     if (!CLeashApp::m_hPsapi)
302         GetModules95(GetCurrentProcessId());
303     //m_LB_DLLsLoaded.AddString("Doesn't work in Windows 95");
304     else
305         GetModulesNT(GetCurrentProcessId());
306 
307     HighlightFirstItem();
308 }
309 
OnLeashModules()310 void CLeashAboutBox::OnLeashModules()
311 {
312     if (!CLeashApp::m_hToolHelp32 && !CLeashApp::m_hPsapi)
313         return; // error
314 
315     m_LB_DLLsLoaded.ResetContent();
316 
317     if (!CLeashApp::m_hPsapi)
318         GetModules95(GetCurrentProcessId(), FALSE);
319     //m_LB_DLLsLoaded.AddString("Doesn't work in Windows 95");
320     else
321         GetModulesNT(GetCurrentProcessId(), FALSE);
322 
323     HighlightFirstItem();
324 }
325 
OnNotLoadedModules()326 void CLeashAboutBox::OnNotLoadedModules()
327 {
328     m_LB_DLLsLoaded.ResetContent();
329 
330     if (!CLeashApp::m_hKrb5DLL)
331         m_LB_DLLsLoaded.AddString(KERB5DLL);
332 
333     HighlightFirstItem();
334 }
335 
OnDblclkLeashModuleLb()336 void CLeashAboutBox::OnDblclkLeashModuleLb()
337 {
338     m_LB_DLLsLoaded.GetText(m_LB_DLLsLoaded.GetCurSel(), m_fileItem);
339 
340     SHELLEXECUTEINFO sei;
341     ZeroMemory(&sei,sizeof(sei));
342     sei.cbSize = sizeof(sei);
343     sei.lpFile = m_fileItem;
344     sei.lpVerb = "properties";
345     sei.fMask  = SEE_MASK_INVOKEIDLIST;
346 
347     if (!ShellExecuteEx(&sei))
348     {
349         MessageBox("Can't find selected file or Properties dialog", "Error",
350                    MB_OK);
351     }
352 }
353 
OnProperties()354 void CLeashAboutBox::OnProperties()
355 {
356     OnDblclkLeashModuleLb();
357 }
358 
OnSetfocusLeashModuleLb()359 void CLeashAboutBox::OnSetfocusLeashModuleLb()
360 {
361     if (m_LB_DLLsLoaded.GetCount())
362         m_propertiesButton.EnableWindow(TRUE);
363 }
364 
PreTranslateMessage(MSG * pMsg)365 BOOL CLeashAboutBox::PreTranslateMessage(MSG* pMsg)
366 {
367     if (m_missingFileError)
368     {
369         ::MessageBox(NULL, "OnInitDialog::We can't find file\"PSAPI.DLL\" "
370                      "or \"KERNEL32.DLL\"!!!\n"
371                      "About Box will not work properly.",
372                      "Error", MB_OK);
373 
374         m_missingFileError = FALSE;
375     }
376     return CDialog::PreTranslateMessage(pMsg);
377 }
378