提議的解答 how to use IWriteEngine2

  • Thursday, March 29, 2012 8:22 AM
     
      Has Code

    I have the same problem like http://social.msdn.microsoft.com/Forums/en-NZ/windowsopticalplatform/thread/0274808e-45c3-4957-9f87-0bce419cdf45

    the code likes

    OnBurn()
    { 
     CDiscMaster discMaster;
     if(!discMaster.Initialize())
     {
      AfxMessageBox("初始化Master失败");
      return ;
     }
    
     CDiscRecorder *pDiscRecorder = NULL;
     for(i = 0; i < discMaster.GetTotalDevices(); i++)
     {
      pDiscRecorder = new CDiscRecorder;
      if(!pDiscRecorder->Initialize(discMaster.GetDeviceUniqueID(i)))
      {
       delete pDiscRecorder;
       pDiscRecorder = NULL;
       continue;
      }
      CDiscFormatData discFormat ;
      if(!discFormat.Initialize(pDiscRecorder,_T("BurnCDTest")))
      {
       delete pDiscRecorder ;
       pDiscRecorder = NULL;
       continue;
      }
    
      break;
     }
    
     if(pDiscRecorder == NULL)
     {
      AfxMessageBox("没有找到可刻录的光驱");
      return ;
     }
    
     if(!pDiscRecorder->AcquireExclusiveAccess(true,_T("BurnCD")))
     {
      AfxMessageBox(_T("无法获取光驱的独占权"));
      return ;
     }
    
     CFileObject fileObj(_T("F:\\vc60.rar"));
     IStream *pIStream = NULL;
     pIStream = fileObj.GetStream();
    
    	CWriteEngine writeEngine;
    	writeEngine.Initialize(pDiscRecorder->GetInterfaceEx(),2048); 
    
    	hr = writeEngine.GetInterface()->put_UseStreamingWrite12(VARIANT_FALSE);
    
    	writeEngine.Burn(pIStream,20); 
    
    
    	pDiscRecorder->ReleaseExclusiveAccess();
    }
    
    bool CWriteEngine::Initialize(IDiscRecorder2Ex *pDiscRecorder2Ex,ULONG bytePerSector)
    {
    	m_hResult = ::CoCreateInstance(__uuidof(MsftWriteEngine2), NULL, CLSCTX_INPROC_SERVER,
    		__uuidof(IWriteEngine2), (void**)&m_pWriteEngine);
    
    	if(FAILED(m_hResult))
    	{
    		return false;
    	}
    
    	m_hResult = m_pWriteEngine->put_Recorder(pDiscRecorder2Ex);
    
    	if(!SUCCEEDED(m_hResult))
    	{
    		return false;
    	}
    
    
    	ULONG ulMax;
    	m_hResult = pDiscRecorder2Ex->GetMaximumNonPageAlignedTransferSize(&ulMax);
    	if(ulMax < bytePerSector)
    	{
    		bytePerSector = ulMax;
    	}
    
    	if(bytePerSector < 1)
    	{
    		bytePerSector = 1;
    	}
    	m_hResult = m_pWriteEngine->put_BytesPerSector(bytePerSector);
    	if(!SUCCEEDED(m_hResult))
    	{
    		return false;
    	}
    
    	m_hResult = m_pWriteEngine->put_EndingSectorsPerSecond(-1);
    
    	m_hResult = m_pWriteEngine->put_StartingSectorsPerSecond(-1); 
    
    	m_hResult = m_pWriteEngine->put_UseStreamingWrite12(VARIANT_TRUE); 
    
    	if(FAILED(m_hResult))
    	{
    		return false;
    	}
    
    	return true;
    }
    
    
    bool CWriteEngine::Burn(IStream *pIstream,long NumofBlocks,bool blast)
    {
    //	IDiscFormat2RawCD
    
    	CWriteEngineEvent* eventSink = CWriteEngineEvent::CreateEventSink();
    	if(eventSink == NULL)
    	{
    		m_errorMsg = _T("Unable to create event sink");
    		return false;
    	}
    
    	if(!eventSink->ConnectWriteEngine(this))
    	{
    		m_errorMsg = _T("Unable to connect event sink with interface");
    		return false;
    	}
    
    //	if(!blast)
    	{
    		m_hResult = m_pWriteEngine->WriteSection(pIstream,m_lLastEndSector,NumofBlocks);
    	}
    // 	else
    // 	{
    // 		m_hResult = m_pWriteEngine->WriteSection(pIstream,1,1);
    // 	}
    	if(!SUCCEEDED(m_hResult))
    	{
    		if(m_hResult == E_POINTER)
    		{
    			m_errorMsg = _T("Pointer is not valid.");
    		}
    		else if(m_hResult == E_OUTOFMEMORY)
    		{
    			m_errorMsg = _T("Failed to allocate the required memory.");
    		}
    		else if(m_hResult == E_INVALIDARG)
    		{
    			m_errorMsg = _T("One or more arguments are not valid.");
    		}
    		else if(m_hResult == 0xC0AA0002)
    		{
    			m_errorMsg = _T("The request was canceled.");
    		}
    		else
    		{
    			m_errorMsg = _T("Unkonow error.");
    		}
    		return false;
    	}
    
    	m_lLastEndSector += NumofBlocks;
    
    	return true;
    }
    

    WriteSection failed with E_IMAPI_UNEXPECTED_RESPONSE_FROM_DEVICE when burning DVD+RW(which is first to burn). (DVD+R is ok)

    But strangely, it is successful that use IDiscFormat2Data to burn dvd+rw. More strangely, IWriteEngine can burn the dvd+rw which was burned through IDiscFormat2Data .

    According to MSDN, there are five steps that I can implement when using IWriteEngine2:

    1. Preparing the hardware by setting mode pages for the media.
    2. Querying the hardware to verify that the media is large enough.
    3. Initializing the write, for example, by formatting the media or setting OPC.
    4. Performing the actual WRITE commands.
    5. Finishing the write by stopping the formatting or closing the session or track.

    But in my code, only step 4 is cared.

    Does the problem happen because I doesnot implement step1 and 3? If so, how can I implement them?

    Can anyone help me?


All Replies

  • Monday, June 11, 2012 11:30 PM
     
     Proposed Answer

    Yes the problem you see is precisely due to the absence of steps 1 and 3. IDiscFormat2Data performs these steps on blank media, such that the next time you use the media steps 1 and 3 are not required.

    If you can't use IDiscFormat2Data (which if you can would simplify things), you need to learn the details of the MMC specification. That specification is available at http://www.t10.org/drafts.htm#MMC_Family However it requires to either be a member of T10 or to purchase it at the ANSI store. Mt Fuji specifications are focused on DVD-R/RW but may include what you need for DVD+R/RW and are downloadable for free from ftp://ftp.avc-pioneer.com/ These are the two specifications we used at Microsoft to develop our IMAPI code.

    Again, your life would be a lot simpler if you used IDiscFormat2Data to abstract yourself from the fairly complex task of setting the proper write parameters of the media.


    Senior Soft. Dev. Eng. | Microsoft IT | Microsoft Corporation

    • Proposed As Answer by David Burg Tuesday, June 12, 2012 9:44 PM
    •