Leaked source code of windows server 2003
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.

400 lines
12 KiB

  1. // wiatest.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "MainFrm.h"
  6. #include "ChildFrm.h"
  7. #include "wiatestDoc.h"
  8. #include "wiatestView.h"
  9. #include "WiaeditpropTable.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. LONG WIACONSTANT_VALUE_FROMINDEX(int index)
  16. {
  17. return g_EditPropTable[index].lVal;
  18. }
  19. TCHAR *WIACONSTANT_TSTR_FROMINDEX(int index)
  20. {
  21. return g_EditPropTable[index].pszValName;
  22. }
  23. INT FindEndIndexInTable(TCHAR *pszPropertyName)
  24. {
  25. int index = FindStartIndexInTable(pszPropertyName);
  26. if(index >=0){
  27. while((g_EditPropTable[index].pszPropertyName != NULL) && (lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0)){
  28. index++;
  29. }
  30. }
  31. return (index - 1);
  32. }
  33. INT FindStartIndexInTable(TCHAR *pszPropertyName)
  34. {
  35. int index = 0;
  36. BOOL bFound = FALSE;
  37. while((g_EditPropTable[index].pszPropertyName != NULL) && (bFound == FALSE) ){
  38. // check for property name
  39. if(lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0){
  40. // we found property name
  41. bFound = TRUE;
  42. } else {
  43. index++;
  44. }
  45. }
  46. if(!bFound){
  47. index = -1;
  48. }
  49. return index;
  50. }
  51. BOOL WIACONSTANT2TSTR(TCHAR *pszPropertyName, LONG lValue, TCHAR *pszValName)
  52. {
  53. BOOL bFound = FALSE;
  54. if(pszValName){
  55. int index = 0;
  56. while((g_EditPropTable[index].pszPropertyName != NULL) && (bFound == FALSE) ){
  57. // check for property name
  58. if(lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0){
  59. // we found property name
  60. if(g_EditPropTable[index].lVal == lValue){
  61. lstrcpy(pszValName,g_EditPropTable[index].pszValName);
  62. bFound = TRUE;
  63. }
  64. }
  65. index++;
  66. }
  67. }
  68. return bFound;
  69. }
  70. BOOL TSTR2WIACONSTANT(TCHAR *pszPropertyName, TCHAR *pszValName, LONG *plVal)
  71. {
  72. BOOL bFound = FALSE;
  73. if(pszValName){
  74. int index = 0;
  75. while((g_EditPropTable[index].pszPropertyName != NULL) && (bFound == FALSE)){
  76. // check for property name
  77. if(lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0){
  78. // we found property name
  79. if(lstrcmpi(g_EditPropTable[index].pszValName,pszValName) == 0){
  80. *plVal = g_EditPropTable[index].lVal;
  81. bFound = TRUE;
  82. }
  83. }
  84. index++;
  85. }
  86. }
  87. return bFound;
  88. }
  89. void RC2TSTR(UINT uResourceID, TCHAR *szString, LONG size)
  90. {
  91. memset(szString,0,size);
  92. INT iNumTCHARSWritten = 0;
  93. HINSTANCE hInstance = NULL;
  94. hInstance = AfxGetInstanceHandle();
  95. if(!hInstance){
  96. MessageBox(NULL,TEXT("Could not get WIATEST's HINSTANCE for string loading."),TEXT("WIATEST Error"),MB_ICONERROR);
  97. return;
  98. }
  99. iNumTCHARSWritten = LoadString(hInstance,uResourceID,szString,(size / (sizeof(TCHAR))));
  100. }
  101. void StatusMessageBox(HWND hWnd, UINT uResourceID)
  102. {
  103. TCHAR szResourceString[MAX_PATH];
  104. memset(szResourceString,0,sizeof(szResourceString));
  105. RC2TSTR(uResourceID,szResourceString,sizeof(szResourceString));
  106. StatusMessageBox(hWnd,szResourceString);
  107. }
  108. void StatusMessageBox(HWND hWnd, LPTSTR szStatusText)
  109. {
  110. TCHAR Title[MAX_PATH];
  111. memset(Title,0,sizeof(Title));
  112. // load status dialog title
  113. RC2TSTR(IDS_WIASTATUS_DIALOGTITLE,Title,sizeof(Title));
  114. MessageBox(hWnd,szStatusText,Title, MB_ICONINFORMATION);
  115. }
  116. void StatusMessageBox(UINT uResourceID)
  117. {
  118. TCHAR szResourceString[MAX_PATH];
  119. memset(szResourceString,0,sizeof(szResourceString));
  120. RC2TSTR(uResourceID,szResourceString,sizeof(szResourceString));
  121. StatusMessageBox(szResourceString);
  122. }
  123. void StatusMessageBox(LPTSTR szStatusText)
  124. {
  125. TCHAR Title[MAX_PATH];
  126. memset(Title,0,sizeof(Title));
  127. // load status dialog title
  128. RC2TSTR(IDS_WIASTATUS_DIALOGTITLE,Title,sizeof(Title));
  129. MessageBox(NULL,szStatusText,Title, MB_ICONINFORMATION);
  130. }
  131. void ErrorMessageBox(UINT uResourceID, HRESULT hrError)
  132. {
  133. TCHAR szResourceString[MAX_PATH];
  134. memset(szResourceString,0,sizeof(szResourceString));
  135. RC2TSTR(uResourceID,szResourceString,sizeof(szResourceString));
  136. ErrorMessageBox(szResourceString,hrError);
  137. }
  138. void ErrorMessageBox(LPTSTR szErrorText, HRESULT hrError)
  139. {
  140. ULONG ulLen = MAX_PATH;
  141. TCHAR MsgBuf[MAX_PATH];
  142. TCHAR *pAllocMsgBuf = NULL;
  143. TCHAR Title[MAX_PATH];
  144. memset(Title,0,sizeof(Title));
  145. memset(MsgBuf,0,sizeof(MsgBuf));
  146. // load error dialog title
  147. RC2TSTR(IDS_WIAERROR_DIALOGTITLE,Title,sizeof(Title));
  148. // attempt to handle WIA custom errors first
  149. switch (hrError) {
  150. case WIA_ERROR_GENERAL_ERROR:
  151. RC2TSTR(IDS_WIAERROR_GENERAL,MsgBuf,sizeof(MsgBuf));
  152. break;
  153. case WIA_ERROR_PAPER_JAM:
  154. RC2TSTR(IDS_WIAERROR_PAPERJAM ,MsgBuf,sizeof(MsgBuf));
  155. break;
  156. case WIA_ERROR_PAPER_EMPTY:
  157. RC2TSTR(IDS_WIAERROR_PAPEREMPTY ,MsgBuf,sizeof(MsgBuf));
  158. break;
  159. case WIA_ERROR_PAPER_PROBLEM:
  160. RC2TSTR(IDS_WIAERROR_PAPERPROBLEM ,MsgBuf,sizeof(MsgBuf));
  161. break;
  162. case WIA_ERROR_OFFLINE:
  163. RC2TSTR(IDS_WIAERROR_DEVICEOFFLINE ,MsgBuf,sizeof(MsgBuf));
  164. break;
  165. case WIA_ERROR_BUSY:
  166. RC2TSTR(IDS_WIAERROR_DEVICEBUSY,MsgBuf,sizeof(MsgBuf));
  167. break;
  168. case WIA_ERROR_WARMING_UP:
  169. RC2TSTR(IDS_WIAERROR_WARMINGUP,MsgBuf,sizeof(MsgBuf));
  170. break;
  171. case WIA_ERROR_USER_INTERVENTION:
  172. RC2TSTR(IDS_WIAERROR_USERINTERVENTION,MsgBuf,sizeof(MsgBuf));
  173. break;
  174. case WIA_ERROR_ITEM_DELETED:
  175. RC2TSTR(IDS_WIAERROR_ITEMDELETED,MsgBuf,sizeof(MsgBuf));
  176. break;
  177. case WIA_ERROR_DEVICE_COMMUNICATION:
  178. RC2TSTR(IDS_WIAERROR_DEVICECOMMUNICATION,MsgBuf,sizeof(MsgBuf));
  179. break;
  180. case WIA_ERROR_INVALID_COMMAND:
  181. RC2TSTR(IDS_WIAERROR_INVALIDCOMMAND,MsgBuf,sizeof(MsgBuf));
  182. break;
  183. case S_OK:
  184. if(szErrorText)
  185. lstrcpy(MsgBuf,szErrorText);
  186. break;
  187. default:
  188. ulLen = 0;
  189. ulLen = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  190. NULL, hrError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  191. (LPTSTR)&pAllocMsgBuf, 0, NULL);
  192. break;
  193. }
  194. if (ulLen <= 0) {
  195. // just use the HRESULT as a formatted string
  196. TSPRINTF(MsgBuf,TEXT("HRESULT = 0x%08X"),hrError);
  197. } else {
  198. if(pAllocMsgBuf){
  199. // trim right (remove \r\n from formatted string)
  200. pAllocMsgBuf[ulLen - (2 * sizeof(TCHAR))] = 0; // reterminate the string
  201. // copy string into message buffer
  202. lstrcpy(MsgBuf,pAllocMsgBuf);
  203. // FormatMessage allocated a buffer to display
  204. LocalFree(pAllocMsgBuf);
  205. }
  206. }
  207. if(S_OK != hrError){
  208. TCHAR szFinalText[MAX_PATH];
  209. memset(szFinalText,0,sizeof(szFinalText));
  210. #ifndef UNICODE
  211. TSPRINTF(szFinalText,TEXT("%s\n(%s)"),szErrorText,MsgBuf);
  212. #else
  213. TSPRINTF(szFinalText,TEXT("%ws\n(%ws)"),szErrorText,MsgBuf);
  214. #endif
  215. MessageBox(NULL,szFinalText,Title,MB_ICONERROR);
  216. } else {
  217. MessageBox(NULL,szErrorText,Title,MB_ICONWARNING);
  218. }
  219. }
  220. /////////////////////////////////////////////////////////////////////////////
  221. // CWiatestApp
  222. BEGIN_MESSAGE_MAP(CWiatestApp, CWinApp)
  223. //{{AFX_MSG_MAP(CWiatestApp)
  224. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  225. // NOTE - the ClassWizard will add and remove mapping macros here.
  226. // DO NOT EDIT what you see in these blocks of generated code!
  227. //}}AFX_MSG_MAP
  228. // Standard file based document commands
  229. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  230. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  231. // Standard print setup command
  232. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  233. END_MESSAGE_MAP()
  234. /////////////////////////////////////////////////////////////////////////////
  235. // CWiatestApp construction
  236. CWiatestApp::CWiatestApp()
  237. {
  238. // TODO: add construction code here,
  239. // Place all significant initialization in InitInstance
  240. }
  241. /////////////////////////////////////////////////////////////////////////////
  242. // The one and only CWiatestApp object
  243. CWiatestApp theApp;
  244. /////////////////////////////////////////////////////////////////////////////
  245. // CWiatestApp initialization
  246. BOOL CWiatestApp::InitInstance()
  247. {
  248. // initialize COM
  249. CoInitialize(NULL);
  250. AfxEnableControlContainer();
  251. // Standard initialization
  252. // If you are not using these features and wish to reduce the size
  253. // of your final executable, you should remove from the following
  254. // the specific initialization routines you do not need.
  255. #ifdef _AFXDLL
  256. Enable3dControls(); // Call this when using MFC in a shared DLL
  257. #else
  258. Enable3dControlsStatic(); // Call this when linking to MFC statically
  259. #endif
  260. SetRegistryKey(_T("Microsoft"));
  261. LoadStdProfileSettings(); // Load standard INI file options (including MRU)
  262. // Register the application's document templates. Document templates
  263. // serve as the connection between documents, frame windows and views.
  264. CMultiDocTemplate* pDocTemplate;
  265. pDocTemplate = new CMultiDocTemplate(
  266. IDR_WIATESTYPE,
  267. RUNTIME_CLASS(CWiatestDoc),
  268. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  269. RUNTIME_CLASS(CWiatestView));
  270. AddDocTemplate(pDocTemplate);
  271. // create main MDI Frame window
  272. CMainFrame* pMainFrame = new CMainFrame;
  273. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  274. return FALSE;
  275. m_pMainWnd = pMainFrame;
  276. // Parse command line for standard shell commands, DDE, file open
  277. CCommandLineInfo cmdInfo;
  278. ParseCommandLine(cmdInfo);
  279. #ifdef _OPEN_NEW_DEVICE_ON_STARTUP
  280. // Dispatch commands specified on the command line
  281. if (!ProcessShellCommand(cmdInfo))
  282. return FALSE;
  283. #endif
  284. // The main window has been initialized, so show and update it.
  285. pMainFrame->ShowWindow(m_nCmdShow);
  286. pMainFrame->UpdateWindow();
  287. return TRUE;
  288. }
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CAboutDlg dialog used for App About
  291. class CAboutDlg : public CDialog
  292. {
  293. public:
  294. CAboutDlg();
  295. // Dialog Data
  296. //{{AFX_DATA(CAboutDlg)
  297. enum { IDD = IDD_ABOUTBOX };
  298. //}}AFX_DATA
  299. // ClassWizard generated virtual function overrides
  300. //{{AFX_VIRTUAL(CAboutDlg)
  301. protected:
  302. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  303. //}}AFX_VIRTUAL
  304. // Implementation
  305. protected:
  306. //{{AFX_MSG(CAboutDlg)
  307. // No message handlers
  308. //}}AFX_MSG
  309. DECLARE_MESSAGE_MAP()
  310. };
  311. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  312. {
  313. //{{AFX_DATA_INIT(CAboutDlg)
  314. //}}AFX_DATA_INIT
  315. }
  316. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  317. {
  318. CDialog::DoDataExchange(pDX);
  319. //{{AFX_DATA_MAP(CAboutDlg)
  320. //}}AFX_DATA_MAP
  321. }
  322. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  323. //{{AFX_MSG_MAP(CAboutDlg)
  324. // No message handlers
  325. //}}AFX_MSG_MAP
  326. END_MESSAGE_MAP()
  327. // App command to run the dialog
  328. void CWiatestApp::OnAppAbout()
  329. {
  330. CAboutDlg aboutDlg;
  331. aboutDlg.DoModal();
  332. }
  333. /////////////////////////////////////////////////////////////////////////////
  334. // CWiatestApp message handlers
  335. int CWiatestApp::ExitInstance()
  336. {
  337. // uninitialize COM
  338. CoUninitialize();
  339. return CWinApp::ExitInstance();
  340. }