关于OLE剪贴板的小问题

tpProgramer 2002-04-30 10:52:22
我使用OLE剪贴板剪贴一段CString文字,设置格式CF_TEXT,但是在代码中调用 COleDataObject对象的 GetGlobalData方法时出现了非法访问,其中代码如下:

在执行拷贝的按钮单击响应事件中:
COleDataSource * pSource = SaveText(); //SavtText为自己写的程序,在下面
if(pSource)
{
pSource->SetClipboard();
}

在执行粘贴的按钮单击响应事件中:
COleDataObject dataObject;
VERIFY(dataObject.AttachClipboard); //从剪贴板得到
DoPasteText(&dataObject); //DoPasteText为自己写的程序,在下面

两个自己写的函数如下:SaveText , DoPasteText

//从m_cSource编辑框控件得到字符串,从字符串的内容创建数据来源对象。
//构造一个COldDataSource对象,然后,将其字符串内容拷贝到全局内存中。
//最后,调用CacheGlobalData来存储在数据来源对象中的HGLOBAL句柄。
COleDataSource * COLEPasteDlg::SaveText()
{
CString strSource;
m_cSource.GetWindowText(strSource);
if(!strSource.IsEmpty()) //if the source string is not empty.
{
COleDataSource * pSource = new COleDataSource();
int nTextSize = strSource.GetLength()-1;
HGLOBAL hText = ::GlobalAlloc(GMEM_SHARE,nTextSize);
LPSTR pText =(LPSTR)::GlobalLock(hText);
ASSERT(pText);
strcpy(pText ,strSource);
::GlobalUnlock(hText);
/*
STGMEDIUM stg;
FORMATETC fmt;
SETFORMATETC(fmt, CF_TEXT, DVASPECT_CONTENT,NULL,TYMED_HGLOBAL,1);
stg.hGlobal = hText;
pSource->CacheData(CF_TEXT,&stg,&fmt);
*/
FORMATETC fmt;
SETFORMATETC(fmt, CF_TEXT, DVASPECT_CONTENT,NULL,TYMED_HGLOBAL,1);
pSource->CacheGlobalData(CF_TEXT, hText,&fmt);
return pSource;
}
return NULL;
}

//从数据对象中填充 strTarget,然后填充到编辑框控件m_cTarget中
BOOL COLEPasteDlg::DoPasteText(COleDataObject *pDataObject)
{
FORMATETC fmt;
SETFORMATETC(fmt, CF_TEXT, DVASPECT_CONTENT,NULL,TYMED_HGLOBAL,1);
//--------------------------------------------Error Occur---------
//下面这一行出现了错误,我跟踪发现是对象的GetData函数出现了错误
//(GetGlobalData在内部实现时调用了GetData函数)
HGLOBAL hText = pDataObject->GetGlobalData(CF_TEXT,&fmt);
//--------------------------------------------Error End---------
ASSERT(hText != NULL);
CString strTarget= (LPSTR)::GlobalLock(hText);
m_cTarget.SetWindowText(strTarget);
::GlobalUnlock(hText);

return TRUE;
}
...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tpProgramer 2002-04-30
  • 打赏
  • 举报
回复
yes! very Good!

if I change the code:

int nTextSize = strSource.GetLength() + 1;
//not GetLength() or GetLength()-1!!

everything is Ok!

thanks for masterz's help !
tpProgramer 2002-04-30
  • 打赏
  • 举报
回复
thanks for masterz' first help, but I want use the OLE Clipboard,because later I'll use it to copy and cut custom format.

I'll test the code following your advice.




masterz 2002-04-30
  • 打赏
  • 举报
回复
int nTextSize = strSource.GetLength()-1;// this is not correct. your buffter can not hold the whole string because its length is less than the string length.
masterz 2002-04-30
  • 打赏
  • 举报
回复
How to place text on the clipboard
CString source;
//put your text in source
if(OpenClipboard())
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}
How to get text off of the clipboard
char * buffer;
if(OpenClipboard())
{
buffer = (char*)GetClipboardData(CF_TEXT);
//do something with buffer here
//before it goes out of scope
}
CloseClipboard();

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧