老师,您好!
在Q友项目中,客户端发送图片时会引发异常中断,引发异常中断的代码已加粗!
这个问题要怎么解决呢?
// 选择图片 按钮事件
void CSendDlg::OnSendImg()
{
CFileDialog flDlg(TRUE, "", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "图片文件|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;||", this);
if(flDlg.DoModal() == IDOK) {
CString csFile = flDlg.GetPathName(); //获取图片文件名
IRichEditOle *lpRichOle = m_SendEdit.GetIRichEditOle();
if(lpRichOle != NULL) {
InsertImage(lpRichOle, csFile);
lpRichOle->Release();
lpRichOle = NULL;
}
}
// 防止发送窗口中的对象被选中
m_ShowEdit.SetSel(-1, 0);
m_ShowEdit.Invalidate();
}
// 向指定的消息框中插入图片
BOOL CSendDlg::InsertImage(IRichEditOle *lpRichEditOle, CString &csFileName)
{
IStorage *lpStorage = NULL; // 存储接口
IOleObject *lpOleObject = NULL; // OLE object
LPLOCKBYTES lpLockBytes = NULL;
IOleClientSite *lpOleClientSite = NULL;
GIFLib::ICGifPtr lpAnimator;
CLSID clsid;
REOBJECT reobject;
HRESULT hr;
if(lpRichEditOle == NULL) {
return FALSE;
}
hr = ::CoInitialize(NULL); //初始化COM库
if(FAILED(hr)) {
_com_issue_error(hr);
}
hr = lpAnimator.CreateInstance(GIFLib::CLSID_CGif);
//在此引发中断//////////////////////////////////////////////////////////////////////////////////////
if(FAILED(hr)) {
_com_issue_error(hr);
}
////////////////////////////////////////////////////////////////////////////////////////////
lpRichEditOle->GetClientSite(&lpOleClientSite);
try {
// 获取OLE对象接口
hr = lpAnimator->QueryInterface(IID_IOleObject, (void **)&lpOleObject);
if(FAILED(hr)) {
AfxMessageBox("Error QueryInterface");
}
hr = lpOleObject->GetUserClassID(&clsid);
if(FAILED(hr)) {
AfxMessageBox("Error GetUserClassID");
}
// 防止出现错误提示
lpOleObject->SetClientSite(NULL);
lpOleObject->SetClientSite(lpOleClientSite);
hr = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes); // 创建LOCKBYTE对象
if(FAILED(hr)) {
AfxThrowOleException(hr);
}
ASSERT(lpLockBytes != NULL);
hr = ::StgCreateDocfileOnILockBytes(lpLockBytes,
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE,
0, &lpStorage);
if(FAILED(hr)) {
VERIFY(lpLockBytes->Release() == 0);
lpLockBytes = NULL;
AfxThrowOleException(hr);
}
ZeroMemory(&reobject, sizeof(REOBJECT)); // 初始化一对象
reobject.cbStruct = sizeof(REOBJECT);
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_BLANK;
reobject.poleobj = lpOleObject;
reobject.polesite = lpOleClientSite;
reobject.pstg = lpStorage;
hr = lpRichEditOle->InsertObject(&reobject);
hr = lpAnimator->LoadFromFile(csFileName.AllocSysString());
if(FAILED(hr)) {
AfxThrowOleException(hr);
}
RedrawWindow(); // 刷新窗体
lpOleClientSite->SaveObject();
OleSetContainedObject(lpOleObject, TRUE);
}
catch(CException *e) {
e->Delete();
}
lpAnimator->Release();
lpStorage->Release();
return TRUE;
}