NeroBurn.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // NeroBurn.cpp: implementation of the CNeroBurn class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. //这个类也是类之间相互引用的一个具体的成功的;例子
  5. #include "stdafx.h"
  6. #include "LYFZIPManage.h"
  7. #include "NeroDlg.h"
  8. #include "NeroBurn.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. extern CNeroDlg* pDlg;
  18. CNeroBurn::CNeroBurn()
  19. {
  20. NeroBurnOK=false;
  21. NeroWorkError=false;
  22. }
  23. CNeroBurn::~CNeroBurn()
  24. {
  25. NeroAPIFree();
  26. for(int i=0; i<m_photoMniiArray.GetSize (); i++)
  27. delete [](m_photoMniiArray.ElementAt (i));
  28. m_photoMniiArray.RemoveAll();
  29. }
  30. extern DWORD FindAppProcessID(CString path);
  31. BOOL CNeroBurn::NeroAPIInit()
  32. {
  33. DWORD id=FindAppProcessID("imapi.exe");
  34. if(id!=-1)
  35. {
  36. HANDLE ProcessHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id);
  37. if(ProcessHandle)TerminateProcess(ProcessHandle,0);
  38. }
  39. if(!NeroAPIGlueConnect (NULL))
  40. {
  41. AfxMessageBox("刻录驱动未安装, 请与系统管理员联系!", MB_ICONSTOP);
  42. return 0;
  43. }
  44. dwVersion = NeroGetAPIVersion();
  45. CString strVersion;
  46. strVersion.Format("NeroAPI version %d.%d.%d.%d",
  47. dwVersion / 1000,
  48. (dwVersion / 100) % 10,
  49. (dwVersion / 10) % 10,
  50. dwVersion % 10);
  51. //AppendString(strVersion);
  52. //AppendString("Filling NERO_SETTINGS structure");
  53. strcpy(pcNeroFilesPath, "11111");
  54. strcpy(pcVendor, "ahead");
  55. strcpy(pcSoftware, "Nero - Burning Rom");
  56. strcpy(pcLanguageFile, "Nero.txt");
  57. memset(&nsSettings, 0, sizeof(nsSettings));
  58. nsSettings.nstNeroFilesPath = pcNeroFilesPath;
  59. nsSettings.nstVendor = pcVendor;
  60. nsSettings.nstIdle.ncCallbackFunction =NULL; // p->IdleCallback;
  61. //NULL;
  62. nsSettings.nstIdle.ncUserData = NULL; // p;//
  63. nsSettings.nstSoftware = pcSoftware;
  64. nsSettings.nstUserDialog.ncCallbackFunction =pDlg->UserDialog;
  65. nsSettings.nstUserDialog.ncUserData =pDlg; // p;
  66. nsSettings.nstLanguageFile =NULL;//pcLanguageFile;
  67. memset(&npProgress, 0, sizeof(npProgress));
  68. npProgress.npAbortedCallback =NULL;// p->AbortedCallback;
  69. npProgress.npAddLogLineCallback =NULL;//p->AddLogLine;
  70. npProgress.npDisableAbortCallback =NULL;//NULL;
  71. npProgress.npProgressCallback =pDlg->ProgressCallback;
  72. npProgress.npSetPhaseCallback =NULL;//p->SetPhaseCallback;
  73. npProgress.npUserData =pDlg;
  74. pndiDeviceInfos = NULL;
  75. NEROAPI_INIT_ERROR initErr;
  76. initErr = NeroInit(&nsSettings, NULL);
  77. switch (initErr)
  78. {
  79. case NEROAPI_INIT_OK:
  80. //AppendString("Initialization of the NeroAPI successful.");
  81. break;
  82. case NEROAPI_INIT_INVALID_ARGS:
  83. {
  84. AfxMessageBox("无效的参数, 请与系统管理员联系!", MB_ICONSTOP);
  85. return 0;
  86. }
  87. break;
  88. case NEROAPI_INIT_INVALID_SERIAL_NUM:
  89. {
  90. AfxMessageBox("刻录驱动未正确安装, 请与系统管理员联系!", MB_ICONSTOP);
  91. return 0;
  92. }
  93. break;
  94. default:
  95. {
  96. AfxMessageBox("出现未知错误, 请与系统管理员联系!", MB_ICONSTOP);
  97. return 0;
  98. }
  99. //AppendString("An error occured. The type of error cannot bedetermined.");
  100. break;
  101. }
  102. pndiDeviceInfos = NeroGetAvailableDrivesEx (MEDIA_CD, NULL);
  103. int lgl=1;
  104. if (!pndiDeviceInfos) {
  105. //AppendString("NeroGetAvailableDrives() returned no available devices.");
  106. {
  107. AfxMessageBox("未找到可用的刻录设备, 请与系统管理员联系!", MB_ICONSTOP);
  108. return 0;
  109. }
  110. }
  111. else
  112. {
  113. if (pndiDeviceInfos->nsdisNumDevInfos > 0)
  114. {
  115. //AppendString("Found the following devices:");
  116. for (DWORD dDeviceCounter = 0; dDeviceCounter < pndiDeviceInfos->nsdisNumDevInfos; dDeviceCounter++)
  117. {
  118. //AppendString(pndiDeviceInfos->nsdisDevInfos[dDeviceCounter].nsdiDeviceName);
  119. //int i = m_cbxDevices.AddString(pndiDeviceInfos->nsdisDevInfos[dDeviceCounter].nsdiDeviceName);
  120. //m_cbxDevices.SetItemDataPtr(i, &pndiDeviceInfos->nsdisDevInfos[dDeviceCounter]);
  121. }
  122. //m_cbxDevices.SelectString(-1, pndiDeviceInfos->nsdisDevInfos[0].nsdiDeviceName);
  123. }
  124. else
  125. {
  126. //AppendString("The number of available devices is 0.");
  127. }
  128. }
  129. return 1;
  130. }
  131. void CNeroBurn::NeroAPIFree()
  132. {
  133. if (pndiDeviceInfos)
  134. {
  135. NeroFreeMem(pndiDeviceInfos);
  136. }
  137. NeroClearErrors();
  138. NeroDone();
  139. NeroAPIGlueDone();
  140. return;
  141. }
  142. void GetChildArray(CString str, CStringArray *pArray, CStringArray &array)
  143. {
  144. CString dirname=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  145. CString temp;
  146. for(int i=0; i<pArray->GetSize (); i++)
  147. {
  148. temp=pArray->ElementAt (i);
  149. temp=temp.Left (temp.ReverseFind ('\\'));
  150. if(temp==str)
  151. array.Add (pArray->ElementAt (i));
  152. }
  153. }
  154. void GetChildPhotoArray(CString str, CStringArray *pArray, CStringArray &array)
  155. {
  156. // AfxMessageBox(str);
  157. CString temp;
  158. for(int i=0; i<pArray->GetSize (); i++)
  159. {
  160. temp=pArray->ElementAt (i);
  161. temp=temp.Left (temp.ReverseFind ('\\'));
  162. if(temp==str)
  163. {
  164. array.Add (pArray->ElementAt (i));
  165. /// AfxMessageBox(pArray->ElementAt (i));
  166. }
  167. }
  168. }
  169. void CNeroBurn::NeroAPIBurn()
  170. {
  171. for(int i=0; i<m_photoMniiArray.GetSize (); i++)
  172. delete [](m_photoMniiArray.ElementAt (i));
  173. m_photoMniiArray.RemoveAll();
  174. CString str;
  175. for(i=0;i<m_DirArray.GetSize ();i++)
  176. {
  177. NERO_ISO_ITEM *pMniiFile=new NERO_ISO_ITEM;
  178. m_photoMniiArray.Add(pMniiFile);
  179. m_nameArray.Add(m_DirArray.ElementAt(i));
  180. }
  181. for(i=0;i<m_DirArray.GetSize ();i++)
  182. {
  183. NERO_ISO_ITEM *pMniiFile=m_photoMniiArray.ElementAt (i);
  184. str=m_DirRealNameArray.ElementAt (i);
  185. strcpy(pMniiFile->fileName,str.Right(str.GetLength()-str.ReverseFind('\\')-1));
  186. strcpy(pMniiFile->sourceFilePath,"c:\\");
  187. pMniiFile->isDirectory=true;
  188. pMniiFile->isReference=false;
  189. if(i==m_DirArray.GetSize ()-1)
  190. {
  191. pMniiFile->nextItem=NULL;
  192. }
  193. else
  194. {
  195. pMniiFile->nextItem=m_photoMniiArray.ElementAt(i+1);
  196. }
  197. //////////////////////////
  198. pMniiFile->subDirFirstItem=NULL;
  199. //////////////////////////
  200. }
  201. for(i=0;i<m_DirArray.GetSize ();i++)
  202. {
  203. JoinDir(m_DirArray.ElementAt(i));
  204. }
  205. NERO_WRITE_CD writeCD;
  206. memset(&writeCD,0,sizeof(writeCD));
  207. writeCD.nwcdpCDStamp=NULL;
  208. writeCD.nwcdArtist=NULL;
  209. writeCD.nwcdTitle=NULL;
  210. writeCD.nwcdCDExtra=FALSE;
  211. writeCD.nwcdNumTracks=0;
  212. //int i = m_cbxDevices.GetCurSel();
  213. //AfxMessageBox("直接使用pndiDeviceInfos->nsdiDevInfos[0]",1,1);
  214. // pndiDeviceInfos = NeroGetAvailableDrivesEx (MEDIA_CD, NULL);
  215. NERO_SCSI_DEVICE_INFO* nsdiDevice =
  216. (NERO_SCSI_DEVICE_INFO*)&pndiDeviceInfos->nsdisDevInfos[0];
  217. //m_cbxDevices.GetItemDataPtr(i);
  218. ndhDeviceHandle = NeroOpenDevice(nsdiDevice);
  219. if (!ndhDeviceHandle)
  220. {
  221. AfxMessageBox("刻录设备不能打开, 请与系统管理员联系!", MB_ICONSTOP);
  222. NeroWorkError=true;
  223. //AppendString("Device could not be opened: "+(CString)nsdiDevice->nsdiDeviceName);
  224. }
  225. else
  226. {
  227. CString title=g_title+"("+m_name;
  228. title+=")客照";
  229. writeCD.nwcdIsoTrack = NeroCreateIsoTrackEx(m_photoMniiArray.ElementAt(0),title,NCITEF_USE_JOLIET);
  230. int iRes;
  231. if(m_bDvd)
  232. iRes= NeroBurn(ndhDeviceHandle, NERO_ISO_AUDIO_MEDIA, &writeCD,
  233. NBF_WRITE|NBF_CLOSE_SESSION, 0, &npProgress);
  234. else
  235. iRes= NeroBurn(ndhDeviceHandle, NERO_ISO_AUDIO_CD, &writeCD,
  236. NBF_WRITE|NBF_CLOSE_SESSION, 0, &npProgress);
  237. NeroFreeIsoTrack(writeCD.nwcdIsoTrack);
  238. NeroCloseDevice(ndhDeviceHandle);
  239. //m_btnAbort.EnableWindow(false);
  240. //m_mCancel.EnableWindow(true);
  241. //mOK.EnableWindow(true);
  242. //m_pgsProgress.SetPos(0);
  243. //m_cbxDevices.EnableWindow(true);
  244. //m_btnBrowse.EnableWindow(true);
  245. //m_btnBurn.EnableWindow(true);
  246. // mbAborted = false;
  247. char* Log = NeroGetErrorLog();
  248. //AppendString(Log);
  249. switch(iRes)
  250. {
  251. case NEROAPI_BURN_OK:
  252. AfxMessageBox("刻录成功!", MB_ICONINFORMATION);
  253. //p->GetDlgItem(IDC_SURETEXT)->SetWindowText("光盘已经刻录好了,不要再按我了嘛!!");
  254. NeroBurnOK=true;
  255. break;
  256. case NEROAPI_BURN_UNKNOWN_CD_FORMAT:
  257. AfxMessageBox("刻录失败:无效刻录盘格式!", MB_ICONSTOP);
  258. NeroWorkError=true;
  259. //AppendString ("BurnCD() : unknown CD format");
  260. break;
  261. case NEROAPI_BURN_INVALID_DRIVE:
  262. //AppendString ("BurnCD() : invalid drive");
  263. AfxMessageBox("刻录失败:驱动器无效!", MB_ICONSTOP);
  264. NeroWorkError=true;
  265. break;
  266. case NEROAPI_BURN_FAILED:
  267. AfxMessageBox("刻录失败!", MB_ICONSTOP);
  268. NeroWorkError=true;
  269. //AppendString ("BurnCD() : burn failed");
  270. break;
  271. case NEROAPI_BURN_FUNCTION_NOT_ALLOWED:
  272. //AppendString ("BurnCD() : function not allowed");
  273. case NEROAPI_BURN_DRIVE_NOT_ALLOWED:
  274. //AppendString ("BurnCD() : drive not allowed");
  275. AfxMessageBox("刻录失败:刻录驱动未安装正确!", MB_ICONSTOP);
  276. NeroWorkError=true;
  277. break;
  278. default:
  279. AfxMessageBox("刻录失败:未知错误!", MB_ICONSTOP);
  280. NeroWorkError=true;
  281. //AppendString ("BurnCD() : unknown error");
  282. break;
  283. }
  284. }
  285. }
  286. void CNeroBurn::JoinDir(CString dirname)
  287. {
  288. CString str;
  289. if(GetPosFromName(dirname)==-1)
  290. {//把根目录加进去
  291. NERO_ISO_ITEM *pMniiFile=new NERO_ISO_ITEM;
  292. m_photoMniiArray.Add(pMniiFile);
  293. m_nameArray.Add(dirname);
  294. str=dirname;
  295. strcpy(pMniiFile->fileName,str.Right(str.GetLength()-str.ReverseFind('\\')-1));
  296. strcpy(pMniiFile->sourceFilePath,"c:\\");
  297. pMniiFile->isDirectory=true;
  298. pMniiFile->isReference=false;
  299. pMniiFile->nextItem=NULL;
  300. }
  301. CStringArray childdirarray;
  302. CStringArray childphotoarray;
  303. GetChildArray(dirname, m_pDir, childdirarray);
  304. GetChildPhotoArray(dirname, &(m_pPhotoArray[GetDirPos(dirname)]), childphotoarray);
  305. int dircount=childdirarray.GetSize ();
  306. int photocount=childphotoarray.GetSize ();
  307. int oldcount=m_photoMniiArray.GetSize();
  308. if( dircount==0 && photocount==0 )
  309. {
  310. NERO_ISO_ITEM *pMniiFile=m_photoMniiArray.ElementAt(GetPosFromName(dirname));
  311. pMniiFile->subDirFirstItem=NULL;return;
  312. }
  313. for(int nn=0; nn<dircount+photocount; nn++)
  314. {
  315. NERO_ISO_ITEM *pMniiFile=new NERO_ISO_ITEM;
  316. m_photoMniiArray.Add(pMniiFile);
  317. m_nameArray.Add("");
  318. }
  319. int realpos=0;
  320. for( nn=0; nn<childdirarray.GetSize (); nn++)
  321. {
  322. str=childdirarray.ElementAt (nn);
  323. NERO_ISO_ITEM *pMniiFile=m_photoMniiArray.ElementAt(realpos+oldcount);
  324. m_nameArray.SetAt(realpos+oldcount, str);
  325. strcpy(pMniiFile->fileName,str.Right(str.GetLength()-str.ReverseFind('\\')-1));
  326. strcpy(pMniiFile->sourceFilePath,"c:\\");
  327. pMniiFile->isDirectory=true;
  328. pMniiFile->isReference=false;
  329. if(nn==dircount+photocount-1)
  330. {
  331. pMniiFile->nextItem=NULL;
  332. }
  333. else
  334. {
  335. pMniiFile->nextItem=m_photoMniiArray.ElementAt(realpos+oldcount+1);
  336. }
  337. realpos++;
  338. }
  339. for(int j=0; j<childphotoarray.GetSize(); j++)
  340. {
  341. CString path=childphotoarray.ElementAt(j);
  342. NERO_ISO_ITEM *pMniiFile=m_photoMniiArray.ElementAt(realpos+oldcount);
  343. m_nameArray.SetAt(realpos+oldcount, path);
  344. strcpy(pMniiFile->fileName,path.Right(path.GetLength()-path.ReverseFind('\\')-1));
  345. strcpy(pMniiFile->sourceFilePath,path);
  346. pMniiFile->isDirectory=false;
  347. pMniiFile->isReference=false;
  348. if(j==photocount-1)
  349. pMniiFile->nextItem=NULL;
  350. else
  351. pMniiFile->nextItem=m_photoMniiArray.ElementAt(realpos+oldcount+1);
  352. realpos++;
  353. }
  354. NERO_ISO_ITEM *pMniiFile=m_photoMniiArray.ElementAt(GetPosFromName(dirname));
  355. pMniiFile->subDirFirstItem=m_photoMniiArray.ElementAt(oldcount);
  356. for( nn=0; nn<childdirarray.GetSize (); nn++)
  357. {
  358. JoinDir(childdirarray.ElementAt (nn));
  359. }
  360. }
  361. int CNeroBurn::GetDirPos(CString dir)
  362. {
  363. for(int i=0; i<m_pDir->GetSize(); i++)
  364. {
  365. if(dir==m_pDir->ElementAt(i))
  366. return i;
  367. }
  368. return -1;
  369. }
  370. //imapi.exe
  371. int CNeroBurn::GetPosFromName(CString dirname)
  372. {
  373. for(int i=0; i<m_nameArray.GetSize(); i++)
  374. {
  375. if(dirname==m_nameArray.ElementAt(i))
  376. return i;
  377. }
  378. return -1;
  379. }