做项目的时候需要做一录屏功能,做出来之后,发现自己的代码,在有些机器上会失败。
查看代码发现是PrepareToEncode在那些机器上会调用失败,用GetLastError发现错误码为10049,很不明白。代码如下:
HRESULT hr = S_OK;
TCHAR szMsg[256] = {0};
// center the dialog on the screen
CenterWindow();

// set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE);

// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);

UIAddChildWindowContainer(m_hWnd); hr = m_spEncoder.CoCreateInstance(CLSID_WMEncoder, NULL, CLSCTX_INPROC_SERVER);

TCHAR szPath[_MAX_PATH] = {0};
GetModuleFileName(NULL, szPath, _countof(szPath));
LPTSTR lpEnd = _tcsrchr(szPath, _T('\\'));
lstrcpy(lpEnd, _T("\\test.wme"));
if( m_spEncoder ) {
hr = m_spEncoder->Load(CComBSTR(szPath));
_sntprintf(szMsg, _countof(szMsg), _T("m_spEncoder->Load hr=0x%x\n"), hr);
OutputDebugString(szMsg);
if( SUCCEEDED( hr ) ) {
CComPtr<IWMEncFile> spFile;
hr = m_spEncoder->get_File(&spFile);
_sntprintf(szMsg, _countof(szMsg), _T("m_spEncoder->get_File hr=0x%x\n"), hr);
OutputDebugString(szMsg);
if( spFile ) {
CComBSTR bstrFile;
hr = spFile->get_LocalFileName(&bstrFile);
_sntprintf(szMsg, _countof(szMsg), _T("LocalFileName(%ls) hr=0x%x\n"), bstrFile, hr);
OutputDebugString(szMsg);
CFileDialog dlg(FALSE, _T("wmv"), _T("test.wmv"), OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("windows media video file(*.wmv)\0*.wmv\0"), m_hWnd);
dlg.DoModal();
_sntprintf(szMsg, _countof(szMsg), _T("FileName(%s)\n"), dlg.m_szFileName);
OutputDebugString(szMsg);
hr = spFile->put_LocalFileName(CComBSTR(dlg.m_szFileName));
_sntprintf(szMsg, _countof(szMsg), _T("put_LocalFileName hr=0x%x\n"), hr);
OutputDebugString(szMsg);
}
hr = m_spEncoder->PrepareToEncode(VARIANT_TRUE);
DWORD lasterror = GetLastError();
_sntprintf(szMsg, _countof(szMsg), _T("m_spEncoder->PrepareToEncode hr=0x%x\n"), hr);
OutputDebugString(szMsg);
}
if( SUCCEEDED( hr ) ) {
hr = m_spEncoder->Start();
_sntprintf(szMsg, _countof(szMsg), _T("m_spEncoder->Start hr=0x%x\n"), hr);
OutputDebugString(szMsg);
}希望大牛指点!