Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
14 KiB

  1. #include <windows.h>
  2. #include <uicommon.h>
  3. #include <commctrl.h>
  4. #include <shellext.h>
  5. #include <simcrack.h>
  6. #include <shlwapi.h>
  7. #include "errdlg.h"
  8. #include "rawerror.h"
  9. #include "resource.h"
  10. extern HINSTANCE g_hInstance;
  11. //
  12. // String constants
  13. //
  14. #define INI_FILE_NAME TEXT("forceerr.ini")
  15. #define GENERAL_SECTION TEXT("ForceError")
  16. #define PROGRAMS_SECTION TEXT("Programs")
  17. #define LAST_PROGRAM TEXT("LastProgram")
  18. CErrorMessageDialog::CErrorMessageDialog( HWND hWnd )
  19. : m_hWnd(hWnd),
  20. m_strIniFileName(INI_FILE_NAME)
  21. {
  22. //
  23. // Create an absolute pathname for the default INI file,
  24. // so it points to the same directory as the EXE
  25. //
  26. TCHAR szCurrFile[MAX_PATH];
  27. if (GetModuleFileName( NULL, szCurrFile, ARRAYSIZE(szCurrFile)))
  28. {
  29. if (PathRemoveFileSpec( szCurrFile ))
  30. {
  31. m_strIniFileName = CSimpleString(szCurrFile);
  32. if (!m_strIniFileName.MatchLastCharacter(TEXT('\\')))
  33. {
  34. m_strIniFileName += TEXT("\\");
  35. }
  36. m_strIniFileName += CSimpleString(INI_FILE_NAME);
  37. }
  38. }
  39. //
  40. // If the default INI file location has been overridden in the registry, use it instead
  41. //
  42. CSimpleString strIniFile = CSimpleReg( HKEY_FORCEERROR, REGSTR_FORCEERR_KEY ).Query( INI_FILE_NAME, TEXT("") );
  43. if (strIniFile.Length())
  44. {
  45. m_strIniFileName = strIniFile;
  46. }
  47. }
  48. CErrorMessageDialog::~CErrorMessageDialog()
  49. {
  50. }
  51. void CErrorMessageDialog::SelectError( HRESULT hrSelect )
  52. {
  53. LRESULT nSel = 0;
  54. for (LRESULT i=0;i<SendDlgItemMessage( m_hWnd, IDC_ERROR_VALUE, CB_GETCOUNT, 0, 0 );i++)
  55. {
  56. HRESULT hr = static_cast<HRESULT>(SendDlgItemMessage( m_hWnd, IDC_ERROR_VALUE, CB_GETITEMDATA, i, 0 ));
  57. if (hrSelect == hr)
  58. {
  59. nSel = i;
  60. break;
  61. }
  62. }
  63. SendDlgItemMessage( m_hWnd, IDC_ERROR_VALUE, CB_SETCURSEL, nSel, 0 );
  64. }
  65. void CErrorMessageDialog::SelectErrorPoint( int nErrorPoint )
  66. {
  67. for (LRESULT i=0;i<SendDlgItemMessage( m_hWnd, IDC_ERROR_POINT, CB_GETCOUNT, 0, 0 );i++)
  68. {
  69. int nCurrErrorPoint = GetComboBoxItemData( GetDlgItem( m_hWnd, IDC_ERROR_POINT ), i );
  70. if (nErrorPoint == nCurrErrorPoint)
  71. {
  72. SendDlgItemMessage( m_hWnd, IDC_ERROR_POINT, CB_SETCURSEL, i, 0 );
  73. break;
  74. }
  75. }
  76. }
  77. LRESULT CErrorMessageDialog::GetComboBoxItemData( HWND hWnd, LRESULT nIndex, LRESULT nDefault )
  78. {
  79. LRESULT lResult = SendMessage( hWnd, CB_GETITEMDATA, nIndex, 0 );
  80. if (CB_ERR == lResult)
  81. {
  82. lResult = nDefault;
  83. }
  84. return lResult;
  85. }
  86. CSimpleString CErrorMessageDialog::GetComboBoxString( HWND hWnd, LRESULT nIndex )
  87. {
  88. CSimpleString strResult;
  89. LRESULT nTextLen = SendMessage( hWnd, CB_GETLBTEXTLEN, nIndex, 0 );
  90. if (nTextLen)
  91. {
  92. LPTSTR pszText = new TCHAR[nTextLen+1];
  93. if (pszText)
  94. {
  95. if (SendMessage( hWnd, CB_GETLBTEXT, nIndex, reinterpret_cast<LPARAM>(pszText)))
  96. {
  97. strResult = pszText;
  98. }
  99. delete[] pszText;
  100. }
  101. }
  102. return strResult;
  103. }
  104. CSimpleString CErrorMessageDialog::GetCurrentlySelectedComboBoxString( HWND hWnd )
  105. {
  106. CSimpleString strResult;
  107. LRESULT nIndex = SendMessage( hWnd, CB_GETCURSEL, 0, 0 );
  108. if (CB_ERR != nIndex)
  109. {
  110. strResult = GetComboBoxString( hWnd, nIndex );
  111. }
  112. return strResult;
  113. }
  114. LRESULT CErrorMessageDialog::GetCurrentComboBoxSelection( HWND hWnd )
  115. {
  116. return SendMessage( hWnd, CB_GETCURSEL, 0, 0 );
  117. }
  118. LRESULT CErrorMessageDialog::GetCurrentComboBoxSelectionData( HWND hWnd, LRESULT nDefault )
  119. {
  120. LRESULT lResult = nDefault;
  121. LRESULT nCurIndex = GetCurrentComboBoxSelection( hWnd );
  122. if (CB_ERR != nCurIndex)
  123. {
  124. lResult = GetComboBoxItemData( hWnd, nCurIndex, nDefault );
  125. }
  126. return lResult;
  127. }
  128. CSimpleString CErrorMessageDialog::GetIniString( LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault )
  129. {
  130. CSimpleString strResult(pszDefault);
  131. TCHAR szString[1024];
  132. if (GetPrivateProfileString( pszSection, pszKey, pszDefault, szString, ARRAYSIZE(szString), m_strIniFileName ))
  133. {
  134. strResult = szString;
  135. }
  136. return strResult;
  137. }
  138. UINT CErrorMessageDialog::GetIniInt( LPCTSTR pszSection, LPCTSTR pszKey, UINT nDefault )
  139. {
  140. return GetPrivateProfileInt( pszSection, pszKey, nDefault, m_strIniFileName );
  141. }
  142. void CErrorMessageDialog::PopulateProgramComboBox()
  143. {
  144. WIA_PUSH_FUNCTION((TEXT("PopulateProgramComboBox")));
  145. SendDlgItemMessage( m_hWnd, IDC_ERROR_PROGRAMS, CB_RESETCONTENT, 0, 0 );
  146. const int c_nSize = 24000;
  147. LPTSTR pszSections = new TCHAR[c_nSize];
  148. if (pszSections)
  149. {
  150. if (GetPrivateProfileString( PROGRAMS_SECTION, NULL, TEXT(""), pszSections, c_nSize, m_strIniFileName ))
  151. {
  152. for (LPTSTR pszCurr=pszSections;pszCurr && *pszCurr;pszCurr += lstrlen(pszCurr)+1 )
  153. {
  154. TCHAR szAppName[MAX_PATH] = {0};
  155. if (GetPrivateProfileString( PROGRAMS_SECTION, pszCurr, TEXT(""), szAppName, ARRAYSIZE(szAppName), m_strIniFileName ))
  156. {
  157. if (lstrlen(szAppName))
  158. {
  159. SendDlgItemMessage( m_hWnd, IDC_ERROR_PROGRAMS, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(szAppName) );
  160. }
  161. }
  162. }
  163. }
  164. delete[] pszSections;
  165. }
  166. LRESULT nSelectedItem = 0;
  167. CSimpleString strLastSelectedProgram = GetIniString( GENERAL_SECTION, LAST_PROGRAM );
  168. if (strLastSelectedProgram.Length())
  169. {
  170. nSelectedItem = SendDlgItemMessage( m_hWnd, IDC_ERROR_PROGRAMS, CB_FINDSTRINGEXACT, -1, reinterpret_cast<LPARAM>(strLastSelectedProgram.String()));
  171. if (nSelectedItem < 0)
  172. {
  173. nSelectedItem = 0;
  174. }
  175. }
  176. WiaUiUtil::ModifyComboBoxDropWidth( GetDlgItem( m_hWnd, IDC_ERROR_PROGRAMS ) );
  177. SendDlgItemMessage( m_hWnd, IDC_ERROR_PROGRAMS, CB_SETCURSEL, nSelectedItem, 0 );
  178. }
  179. CSimpleString CErrorMessageDialog::GetCurrentlySelectedProgram()
  180. {
  181. return GetCurrentlySelectedComboBoxString( GetDlgItem( m_hWnd, IDC_ERROR_PROGRAMS ) );
  182. }
  183. void CErrorMessageDialog::PopulateErrorPointComboBox()
  184. {
  185. WIA_PUSH_FUNCTION((TEXT("PopulateProgramComboBox")));
  186. SendDlgItemMessage( m_hWnd, IDC_ERROR_POINT, CB_RESETCONTENT, 0, 0 );
  187. if (CB_ERR != SendDlgItemMessage( m_hWnd, IDC_ERROR_POINT, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(CSimpleString( IDS_NO_ERROR, g_hInstance ).String() ) ) )
  188. {
  189. CSimpleString strCurrentProgram = GetCurrentlySelectedProgram();
  190. if (strCurrentProgram.Length())
  191. {
  192. const int c_nSize = 24000;
  193. LPTSTR pszErrors = new TCHAR[c_nSize];
  194. if (pszErrors)
  195. {
  196. if (GetPrivateProfileString( strCurrentProgram, NULL, TEXT(""), pszErrors, c_nSize, m_strIniFileName ))
  197. {
  198. for (LPTSTR pszCurr=pszErrors;pszCurr && *pszCurr;pszCurr += lstrlen(pszCurr)+1 )
  199. {
  200. if (lstrlen(pszCurr))
  201. {
  202. UINT nFlag = GetIniInt( strCurrentProgram, pszCurr);
  203. if (nFlag)
  204. {
  205. LRESULT nIndex = SendDlgItemMessage( m_hWnd, IDC_ERROR_POINT, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszCurr) );
  206. if (CB_ERR != nIndex)
  207. {
  208. SendDlgItemMessage( m_hWnd, IDC_ERROR_POINT, CB_SETITEMDATA, nIndex, nFlag );
  209. }
  210. }
  211. }
  212. }
  213. }
  214. delete[] pszErrors;
  215. }
  216. }
  217. SelectErrorPoint( CWiaDebugClient::GetForceFailurePoint(strCurrentProgram) );
  218. }
  219. WiaUiUtil::ModifyComboBoxDropWidth( GetDlgItem( m_hWnd, IDC_ERROR_POINT ) );
  220. }
  221. void CErrorMessageDialog::PopulateErrorsComboBox()
  222. {
  223. SendDlgItemMessage( m_hWnd, IDC_ERROR_VALUE, CB_RESETCONTENT, 0, 0 );
  224. for (int i=0;i<g_ErrorMessageCount;i++)
  225. {
  226. LRESULT nIndex = SendMessage( GetDlgItem( m_hWnd, IDC_ERROR_VALUE ), CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(g_ErrorMessages[i].pszName));
  227. if (nIndex != CB_ERR)
  228. {
  229. SendMessage( GetDlgItem( m_hWnd, IDC_ERROR_VALUE ), CB_SETITEMDATA, nIndex, g_ErrorMessages[i].hr );
  230. }
  231. }
  232. WiaUiUtil::ModifyComboBoxDropWidth( GetDlgItem( m_hWnd, IDC_ERROR_VALUE ) );
  233. }
  234. void CErrorMessageDialog::InitializeAllFields()
  235. {
  236. PopulateErrorsComboBox();
  237. HandleErrorSelectionChange();
  238. PopulateProgramComboBox();
  239. HandleProgramsSelectionChange();
  240. }
  241. void CErrorMessageDialog::HandleErrorSelectionChange()
  242. {
  243. CSimpleString strErrorDescription;
  244. LRESULT nCurIndex = SendDlgItemMessage( m_hWnd, IDC_ERROR_VALUE, CB_GETCURSEL, 0, 0 );
  245. if (CB_ERR != nCurIndex)
  246. {
  247. HRESULT hr = static_cast<HRESULT>(SendDlgItemMessage( m_hWnd, IDC_ERROR_VALUE, CB_GETITEMDATA, nCurIndex, 0 ));
  248. strErrorDescription = WiaUiUtil::GetErrorTextFromHResult( hr );
  249. }
  250. if (!strErrorDescription.Length())
  251. {
  252. m_bErrorStringProvided = false;
  253. strErrorDescription.LoadString( IDS_NO_SYSTEM_ERROR_MESSAGE, g_hInstance );
  254. }
  255. else
  256. {
  257. m_bErrorStringProvided = true;
  258. }
  259. strErrorDescription.SetWindowText( GetDlgItem( m_hWnd, IDC_ERROR_DESCRIPTION ) );
  260. }
  261. void CErrorMessageDialog::HandleProgramsSelectionChange()
  262. {
  263. PopulateErrorPointComboBox();
  264. HandlePointSelectionChange();
  265. SelectError( CWiaDebugClient::GetForceFailureValue(GetCurrentlySelectedProgram() ) );
  266. HandleErrorSelectionChange();
  267. }
  268. void CErrorMessageDialog::HandlePointSelectionChange()
  269. {
  270. BOOL bEnable = (0 != GetCurrentComboBoxSelectionData( GetDlgItem( m_hWnd, IDC_ERROR_POINT ) ) );
  271. EnableWindow( GetDlgItem( m_hWnd, IDC_ERROR_VALUE ), bEnable );
  272. EnableWindow( GetDlgItem( m_hWnd, IDC_ERROR_VALUE_PROMPT ), bEnable );
  273. }
  274. void CErrorMessageDialog::OnSetError( WPARAM, LPARAM )
  275. {
  276. CSimpleString strCurrentProgram = GetCurrentlySelectedProgram();
  277. if (strCurrentProgram.Length())
  278. {
  279. LRESULT nErrorPoint = GetCurrentComboBoxSelectionData( GetDlgItem( m_hWnd, IDC_ERROR_POINT ) );
  280. CWiaDebugClient::SetForceFailurePoint(strCurrentProgram,nErrorPoint);
  281. if (nErrorPoint)
  282. {
  283. LRESULT nErrorValue = GetCurrentComboBoxSelectionData( GetDlgItem( m_hWnd, IDC_ERROR_VALUE ) );
  284. CWiaDebugClient::SetForceFailureValue(strCurrentProgram,nErrorValue);
  285. }
  286. }
  287. }
  288. void CErrorMessageDialog::OnCancel( WPARAM, LPARAM )
  289. {
  290. CSimpleString strCurrentProgram = GetCurrentlySelectedProgram();
  291. WritePrivateProfileString( GENERAL_SECTION, LAST_PROGRAM, strCurrentProgram, m_strIniFileName );
  292. EndDialog( m_hWnd, IDCANCEL );
  293. }
  294. void CErrorMessageDialog::OnErrorsSelChange( WPARAM, LPARAM )
  295. {
  296. HandleErrorSelectionChange();
  297. }
  298. void CErrorMessageDialog::OnPointSelChange( WPARAM, LPARAM )
  299. {
  300. HandlePointSelectionChange();
  301. }
  302. void CErrorMessageDialog::OnProgramsSelChange( WPARAM, LPARAM )
  303. {
  304. HandleProgramsSelectionChange();
  305. }
  306. void CErrorMessageDialog::OnClearAll( WPARAM, LPARAM )
  307. {
  308. for (LRESULT i=0;i<SendDlgItemMessage( m_hWnd, IDC_ERROR_PROGRAMS, CB_GETCOUNT, 0, 0 );i++)
  309. {
  310. CSimpleString strCurrProgram = GetComboBoxString( GetDlgItem( m_hWnd, IDC_ERROR_PROGRAMS ), i );
  311. if (strCurrProgram.Length())
  312. {
  313. CWiaDebugClient::SetForceFailurePoint(strCurrProgram,0);
  314. CWiaDebugClient::SetForceFailureValue(strCurrProgram,0);
  315. }
  316. }
  317. InitializeAllFields();
  318. }
  319. void CErrorMessageDialog::OnRefresh( WPARAM, LPARAM )
  320. {
  321. InitializeAllFields();
  322. }
  323. LRESULT CErrorMessageDialog::OnInitDialog( WPARAM, LPARAM )
  324. {
  325. SendMessage( m_hWnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(LoadImage(g_hInstance,MAKEINTRESOURCE(IDI_FORCEERR), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR) ));
  326. SendMessage( m_hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadImage(g_hInstance,MAKEINTRESOURCE(IDI_FORCEERR), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR) ));
  327. InitializeAllFields();
  328. return TRUE;
  329. }
  330. LRESULT CErrorMessageDialog::OnDestroy( WPARAM, LPARAM )
  331. {
  332. return 0;
  333. }
  334. LRESULT CErrorMessageDialog::OnCtlColorStatic( WPARAM wParam, LPARAM lParam )
  335. {
  336. LRESULT lRes = DefWindowProc( m_hWnd, WM_CTLCOLORSTATIC, wParam, lParam );
  337. if (reinterpret_cast<HWND>(lParam) == GetDlgItem( m_hWnd, IDC_ERROR_DESCRIPTION ))
  338. {
  339. if (!m_bErrorStringProvided)
  340. {
  341. SetTextColor( reinterpret_cast<HDC>(wParam), RGB(255,0,0) );
  342. }
  343. }
  344. return lRes;
  345. }
  346. LRESULT CErrorMessageDialog::OnCommand( WPARAM wParam, LPARAM lParam )
  347. {
  348. SC_BEGIN_COMMAND_HANDLERS()
  349. {
  350. SC_HANDLE_COMMAND(IDCANCEL,OnCancel);
  351. SC_HANDLE_COMMAND(IDC_SET_ERROR,OnSetError);
  352. SC_HANDLE_COMMAND(IDC_REFRESH,OnRefresh);
  353. SC_HANDLE_COMMAND(IDC_CLEAR_ALL,OnClearAll);
  354. SC_HANDLE_COMMAND_NOTIFY(CBN_SELCHANGE,IDC_ERROR_POINT,OnPointSelChange);
  355. SC_HANDLE_COMMAND_NOTIFY(CBN_SELCHANGE,IDC_ERROR_VALUE,OnErrorsSelChange);
  356. SC_HANDLE_COMMAND_NOTIFY(CBN_SELCHANGE,IDC_ERROR_PROGRAMS,OnProgramsSelChange);
  357. }
  358. SC_END_COMMAND_HANDLERS();
  359. }
  360. INT_PTR __stdcall CErrorMessageDialog::DialogProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  361. {
  362. SC_BEGIN_DIALOG_MESSAGE_HANDLERS(CErrorMessageDialog)
  363. {
  364. SC_HANDLE_DIALOG_MESSAGE( WM_INITDIALOG, OnInitDialog );
  365. SC_HANDLE_DIALOG_MESSAGE( WM_CTLCOLORSTATIC, OnCtlColorStatic );
  366. SC_HANDLE_DIALOG_MESSAGE( WM_DESTROY, OnDestroy );
  367. SC_HANDLE_DIALOG_MESSAGE( WM_COMMAND, OnCommand );
  368. }
  369. SC_END_DIALOG_MESSAGE_HANDLERS();
  370. }