FPDUMPWDlg.cpp
上传用户:wandash
上传日期:2007-01-07
资源大小:79k
文件大小:11k
源码类别:

SCSI/ASPI

开发平台:

Visual C++

  1. // FPDUMPWDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FPDUMPW.h"
  5. #include "FPDUMPWDlg.h"
  6. #include "aspiw32.h"
  7. #include "DialogSettings.h"
  8. #include <io.h>
  9. #include <direct.h>
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. CString strFileName;
  16. BYTE HostAdapterID = 0;
  17. BYTE TargetID = 3;
  18. BYTE LUN = 0;
  19. // 后台线程
  20. UINT DumpWatcher( LPVOID pParam )
  21. {
  22. IsWatcherRunning = TRUE;
  23. bCancelWatcher = FALSE;
  24. CWnd *pDialog;
  25. CButton *pbtnSetting;
  26. CButton *pbtnClose;
  27. pDialog = AfxGetMainWnd();
  28. pbtnSetting = (CButton*)(pDialog->GetDlgItem(IDC_DUMP_SETTINGS));
  29. pbtnSetting->EnableWindow(FALSE);
  30. pbtnClose = (CButton*)(pDialog->GetDlgItem(IDC_DUMP_CLOSE));
  31. pbtnClose->EnableWindow(FALSE);
  32. // ::MessageBox(NULL,strFileName,"MESSAGE",MB_OK);
  33. // 1. Wait until tape is ready...
  34. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"等待磁带机就绪...");
  35. for (;;)
  36. {
  37. if (bCancelWatcher) goto ExitLoop;
  38. if (IO_ALL_TEST_UNIT_READY(HostAdapterID,TargetID,LUN)==SS_COMP) break;
  39. }
  40. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"磁带机已经就绪");
  41. // 2. Load the tape
  42. if (bCancelWatcher) goto ExitLoop;
  43. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"正在装载磁带...");
  44. if (IO_TAPE_LOAD(HostAdapterID,TargetID,LUN,0,1,1)!=SS_COMP)
  45. {
  46. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"装载磁带出错");
  47. goto ExitLoop;
  48. }
  49. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"装载磁带完成");
  50. // 3. read files
  51. if (bCancelWatcher) goto ExitLoop;
  52. {
  53. CString strFileNameEx;
  54. char szTag[12];
  55. int nStatus = 1; // 1 头,2 身体,3 尾
  56. int n = 1;
  57. int block = 0;
  58. FILE *fp = NULL;
  59. for (;;)
  60. {
  61. BYTE buf[2048];
  62. int nBlockLen;
  63. BOOLEAN bFileMark;
  64. BOOLEAN bILI;
  65. BOOLEAN bEOM;
  66. BOOLEAN bValid;
  67. BYTE bSenseKey;
  68. for (;;)
  69. {
  70. if (bCancelWatcher) goto ExitLoop;
  71. if (IO_ALL_TEST_UNIT_READY(HostAdapterID,TargetID,LUN)==SS_COMP) break;
  72. }
  73. if (bCancelWatcher) goto ExitLoop;
  74. IO_TAPE_READ(HostAdapterID,TargetID,LUN,2048,&(buf[0]),&nBlockLen,&bFileMark,&bEOM,&bILI,&bValid,&bSenseKey);
  75. if (bValid==0)
  76. {
  77. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"读磁带的过程中发生了无法预料的错误");
  78. fclose(fp);
  79. fp = NULL;
  80. goto ExitLoop;
  81. }
  82. if (nBlockLen>0)
  83. {
  84. if (nBlockLen<=2048)
  85. {
  86. if (fp==NULL)
  87. {
  88. switch(nStatus)
  89. {
  90. case 1:
  91. sprintf(szTag,".H%2.2d",n);
  92. break;
  93. case 2:
  94. sprintf(szTag,".D%2.2d",n);
  95. break;
  96. case 3:
  97. sprintf(szTag,".E%2.2d",n);
  98. break;
  99. }
  100. strFileNameEx = strFileName+ CString(szTag);
  101. pDialog->SetDlgItemText(IDC_DUMPSTATUS,CString("正在创建文件")+strFileNameEx);
  102. fp = fopen(strFileNameEx,"wb");
  103. if (fp==NULL)
  104. {
  105. pDialog->SetDlgItemText(IDC_DUMPSTATUS,CString("无法创建文件")+strFileNameEx);
  106. goto ExitLoop2;
  107. }
  108. }
  109. fwrite(&(buf[0]),1,nBlockLen,fp);
  110. } else
  111. {
  112. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"磁带数据缓冲区太小");
  113. fclose(fp);
  114. fp = NULL;
  115. goto ExitLoop;
  116. }
  117. }
  118. if (bSenseKey==8)
  119. {
  120. if (fp!=NULL)
  121. {
  122. fclose(fp);
  123. fp = NULL;
  124. }
  125. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"读磁带顺利结束");
  126. goto ExitLoop2;
  127. }
  128. if ((bFileMark)&&(fp!=NULL))
  129. {
  130. nStatus = (nStatus % 3) + 1;
  131. fclose(fp);
  132. fp = NULL;
  133. }
  134. }
  135. }
  136. ExitLoop:
  137. pDialog->SetDlgItemText(IDC_DUMPSTATUS,"用户中断了读磁带");
  138. ExitLoop2:
  139. pbtnSetting = (CButton*)(pDialog->GetDlgItem(IDC_DUMP_SETTINGS));
  140. pbtnSetting->EnableWindow(TRUE);
  141. pbtnClose = (CButton*)(pDialog->GetDlgItem(IDC_DUMP_CLOSE));
  142. pbtnClose->EnableWindow(TRUE);
  143. IsWatcherRunning = FALSE;
  144. return 0;
  145. }
  146. // 信号灯
  147. BOOLEAN  bCancelWatcher;
  148. CWinThread *pDumpWatcher;
  149. BOOLEAN  IsWatcherRunning = FALSE;
  150. /////////////////////////////////////////////////////////////////////////////
  151. // CAboutDlg dialog used for App About
  152. class CAboutDlg : public CDialog
  153. {
  154. public:
  155. CAboutDlg();
  156. // Dialog Data
  157. //{{AFX_DATA(CAboutDlg)
  158. enum { IDD = IDD_ABOUTBOX };
  159. //}}AFX_DATA
  160. // ClassWizard generated virtual function overrides
  161. //{{AFX_VIRTUAL(CAboutDlg)
  162. protected:
  163. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  164. //}}AFX_VIRTUAL
  165. // Implementation
  166. protected:
  167. //{{AFX_MSG(CAboutDlg)
  168. //}}AFX_MSG
  169. DECLARE_MESSAGE_MAP()
  170. };
  171. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  172. {
  173. //{{AFX_DATA_INIT(CAboutDlg)
  174. //}}AFX_DATA_INIT
  175. }
  176. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  177. {
  178. CDialog::DoDataExchange(pDX);
  179. //{{AFX_DATA_MAP(CAboutDlg)
  180. //}}AFX_DATA_MAP
  181. }
  182. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  183. //{{AFX_MSG_MAP(CAboutDlg)
  184. // No message handlers
  185. //}}AFX_MSG_MAP
  186. END_MESSAGE_MAP()
  187. /////////////////////////////////////////////////////////////////////////////
  188. // CFPDUMPWDlg dialog
  189. CFPDUMPWDlg::CFPDUMPWDlg(CWnd* pParent /*=NULL*/)
  190. : CDialog(CFPDUMPWDlg::IDD, pParent)
  191. {
  192. //{{AFX_DATA_INIT(CFPDUMPWDlg)
  193. // NOTE: the ClassWizard will add member initialization here
  194. //}}AFX_DATA_INIT
  195. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  196. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  197. }
  198. void CFPDUMPWDlg::DoDataExchange(CDataExchange* pDX)
  199. {
  200. CDialog::DoDataExchange(pDX);
  201. //{{AFX_DATA_MAP(CFPDUMPWDlg)
  202. // NOTE: the ClassWizard will add DDX and DDV calls here
  203. //}}AFX_DATA_MAP
  204. }
  205. BEGIN_MESSAGE_MAP(CFPDUMPWDlg, CDialog)
  206. //{{AFX_MSG_MAP(CFPDUMPWDlg)
  207. ON_WM_SYSCOMMAND()
  208. ON_WM_PAINT()
  209. ON_WM_QUERYDRAGICON()
  210. ON_BN_CLICKED(IDC_DUMP_START, OnDumpStart)
  211. ON_BN_CLICKED(IDC_DUMP_CANCEL, OnDumpCancel)
  212. ON_WM_CLOSE()
  213. ON_BN_CLICKED(IDC_DUMP_SETTINGS, OnDumpSettings)
  214. ON_BN_CLICKED(IDC_DUMP_CLOSE, OnDumpClose)
  215. //}}AFX_MSG_MAP
  216. END_MESSAGE_MAP()
  217. /////////////////////////////////////////////////////////////////////////////
  218. // CFPDUMPWDlg message handlers
  219. BOOL CFPDUMPWDlg::OnInitDialog()
  220. {
  221. CDialog::OnInitDialog();
  222. // Add "About..." menu item to system menu.
  223. // IDM_ABOUTBOX must be in the system command range.
  224. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  225. ASSERT(IDM_ABOUTBOX < 0xF000);
  226. CMenu* pSysMenu = GetSystemMenu(FALSE);
  227. if (pSysMenu != NULL)
  228. {
  229. CString strAboutMenu;
  230. strAboutMenu.LoadString(IDS_ABOUTBOX);
  231. if (!strAboutMenu.IsEmpty())
  232. {
  233. pSysMenu->AppendMenu(MF_SEPARATOR);
  234. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  235. }
  236. }
  237. // Set the icon for this dialog.  The framework does this automatically
  238. //  when the application's main window is not a dialog
  239. SetIcon(m_hIcon, TRUE); // Set big icon
  240. SetIcon(m_hIcon, FALSE); // Set small icon
  241. // TODO: Add extra initialization here
  242. pDumpWatcher = NULL;
  243. if (!LoadASPIManager())
  244. {
  245. MessageBox("无法载入ASPI管理模块,程序无法继续","严重错误",MB_ICONERROR);
  246. EndDialog(FALSE);
  247. }
  248. return TRUE;  // return TRUE  unless you set the focus to a control
  249. }
  250. void CFPDUMPWDlg::OnSysCommand(UINT nID, LPARAM lParam)
  251. {
  252. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  253. {
  254. CAboutDlg dlgAbout;
  255. dlgAbout.DoModal();
  256. }
  257. else
  258. {
  259. CDialog::OnSysCommand(nID, lParam);
  260. }
  261. }
  262. // If you add a minimize button to your dialog, you will need the code below
  263. //  to draw the icon.  For MFC applications using the document/view model,
  264. //  this is automatically done for you by the framework.
  265. void CFPDUMPWDlg::OnPaint() 
  266. {
  267. if (IsIconic())
  268. {
  269. CPaintDC dc(this); // device context for painting
  270. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  271. // Center icon in client rectangle
  272. int cxIcon = GetSystemMetrics(SM_CXICON);
  273. int cyIcon = GetSystemMetrics(SM_CYICON);
  274. CRect rect;
  275. GetClientRect(&rect);
  276. int x = (rect.Width() - cxIcon + 1) / 2;
  277. int y = (rect.Height() - cyIcon + 1) / 2;
  278. // Draw the icon
  279. dc.DrawIcon(x, y, m_hIcon);
  280. }
  281. else
  282. {
  283. CDialog::OnPaint();
  284. }
  285. }
  286. // The system calls this to obtain the cursor to display while the user drags
  287. //  the minimized window.
  288. HCURSOR CFPDUMPWDlg::OnQueryDragIcon()
  289. {
  290. return (HCURSOR) m_hIcon;
  291. }
  292. void CFPDUMPWDlg::OnDumpStart() 
  293. {
  294. // TODO: Add your control notification handler code here
  295. CString strDir;
  296. CString strName;
  297. GetDlgItemText(IDC_EDIT1,strDir);
  298. GetDlgItemText(IDC_EDIT2,strName);
  299. strDir.TrimLeft();
  300. strDir.TrimRight();
  301. strName.TrimLeft();
  302. strName.TrimRight();
  303. if ((strDir.GetLength()!=0)&&(strDir.Right(1)!=CString("\")))
  304. {
  305. strDir += CString("\");
  306. }
  307. strFileName = strDir + strName;
  308. if (strFileName.GetLength()==0)
  309. {
  310. MessageBox("子目录名和存盘名不能全部为空","参数错误",MB_ICONERROR);
  311. return ;
  312. }
  313. if (strDir.GetLength()>0)
  314. {
  315. CString strSearch = strDir + "*.*";
  316. struct _finddata_t fileinfo;
  317. CString strPath = strDir.Left(strDir.GetLength()-1);
  318. long handle;
  319. // 判断子目录是否存在
  320. handle = _findfirst(strSearch,&fileinfo);
  321. if (handle==-1)
  322. {
  323. int nRes = MessageBox("子目录不存在,是否要创建?","参数错误",MB_ICONERROR|MB_YESNO);
  324. if (nRes==IDYES)
  325. {
  326. if (-1==_mkdir(strPath))
  327. {
  328. MessageBox("无法创建子目录?","参数错误",MB_ICONERROR|MB_OK);
  329. }
  330. }
  331. } else
  332. {
  333. _findclose(handle);
  334. }
  335. }
  336. // 判断文件是否存在
  337. FILE *fp;
  338. CString strFirstFileName;
  339. strFirstFileName = strFileName + ".D01";
  340. fp = fopen((LPCSTR)strFirstFileName,"rb");
  341. if (fp!=NULL) 
  342. {
  343. fclose(fp);
  344. if (IDNO==MessageBox("文件已存在,要覆盖吗?","文件名重复",MB_YESNO))
  345. return ;
  346. }
  347. pDumpWatcher = AfxBeginThread(DumpWatcher,NULL);
  348. }
  349. void CFPDUMPWDlg::OnDumpCancel() 
  350. {
  351. // TODO: Add your control notification handler code here
  352. if (!IsWatcherRunning)
  353. return;
  354. bCancelWatcher = TRUE;
  355. // WaitForSingleObject(pDumpWatcher->m_hThread,INFINITE);
  356. // pDumpWatcher=NULL;
  357. }
  358. void CFPDUMPWDlg::OnClose() 
  359. {
  360. // TODO: Add your message handler code here and/or call default
  361. CDialog::OnClose();
  362. }
  363. void CFPDUMPWDlg::OnDumpSettings() 
  364. {
  365. // TODO: Add your control notification handler code here
  366. CDialogSettings myDialogSettings;
  367. myDialogSettings.m_HostAdapterID = HostAdapterID;
  368. myDialogSettings.m_TargetID  = TargetID;
  369. myDialogSettings.m_LUN  = LUN;
  370. if (IDOK==myDialogSettings.DoModal())
  371. {
  372. HostAdapterID = myDialogSettings.m_HostAdapterID;
  373. TargetID      = myDialogSettings.m_TargetID;
  374. LUN   = myDialogSettings.m_LUN;
  375. }
  376. }
  377. void CFPDUMPWDlg::OnDumpClose() 
  378. {
  379. // TODO: Add your control notification handler code here
  380. EndDialog(0);
  381. }
  382. void CFPDUMPWDlg::OnOK() 
  383. {
  384. // TODO: Add extra validation here
  385. return;
  386. // CDialog::OnOK();
  387. }
  388. void CFPDUMPWDlg::OnCancel() 
  389. {
  390. // TODO: Add extra cleanup here
  391. if (IsWatcherRunning)
  392. return;
  393. CDialog::OnCancel();
  394. }