我从MSDN里面拿的代码,稍微改了一下写了这样一段,目的是把str_tmp里的东西复制进剪贴板,但是现在只复制第一个字符,比如下面这个就只有a而不是预想的abcd,求解,多谢各位了~if (!OpenClipboard()) {
MessageBox(_T("Open Clipboard Failure."));
return;
}
EmptyClipboard();
str_tmp=_T("abcd"); LPTSTR chr_tmp = str_tmp.GetBuffer();
int cch = _tcslen(chr_tmp); HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, 
        (cch + 1) * sizeof(TCHAR)); 
    if (hglbCopy == NULL) 
    { 
        CloseClipboard(); 
        return;// FALSE; 
    }     // Lock the handle and copy the text to the buffer.     LPTSTR lptstrCopy;
lptstrCopy =(LPTSTR) GlobalLock(hglbCopy); 
    _tcscpy_s(lptstrCopy, cch+1, chr_tmp);
    GlobalUnlock(hglbCopy);     // Place the handle on the clipboard.     SetClipboardData(CF_TEXT, hglbCopy); 
CloseClipboard();

解决方案 »

  1.   

    GetBuffer()用之后可不能忘记ReleaseBuffer()
      

  2.   

    if (!OpenClipboard()) {
            MessageBox(_T("Open Clipboard Failure."));
            return;
        }
        EmptyClipboard();
        str_tmp=_T("abcd");    HGLOBAL hglbCopy = GlobalAlloc(GHND | GMEM_SHARE, (str_tmp.GetLength() + 1) * sizeof(TCHAR)); 
        if (hglbCopy == NULL) 
        { 
            CloseClipboard(); 
            return;// FALSE; 
        }     // Lock the handle and copy the text to the buffer.     LPTSTR lptstrCopy;
        lptstrCopy =(LPTSTR) GlobalLock(hglbCopy); 
        _tcscpy_s(lptstrCopy, cch+1, chr_tmp);
        GlobalUnlock(hglbCopy);     // Place the handle on the clipboard.     SetClipboardData(CF_TEXT, hglbCopy); 
        CloseClipboard();
      

  3.   

    if (!OpenClipboard()) {
            MessageBox(_T("Open Clipboard Failure."));
            return;
        }
        EmptyClipboard();
        str_tmp=_T("abcd");    HGLOBAL hglbCopy = GlobalAlloc(GHND | GMEM_SHARE, (str_tmp.GetLength() + 1) * sizeof(TCHAR)); 
        if (hglbCopy == NULL) 
        { 
            CloseClipboard(); 
            return;// FALSE; 
        }     // Lock the handle and copy the text to the buffer.     LPTSTR lptstrCopy = (LPTSTR) GlobalLock(hglbCopy); 
        _tcscpy(lptstrCopy, str_tmp);
        GlobalUnlock(hglbCopy);     // Place the handle on the clipboard.     SetClipboardData(CF_TEXT, hglbCopy); 
        CloseClipboard();
      

  4.   

    如果把由字母和数字组成的Unicode字符串当作LPSTR来看,就只有一个字符。
      

  5.   

    SetClipboardData(CF_UNICODETEXT, hglbCopy); 
      

  6.   


    LPTSTR chr_tmp = str_tmp.GetBuffer(); //没有指定缓冲区的大小,在VC6下无法编译。代码已经修改,并不是将LZ的代码COPY一遍。
      

  7.   

     str_tmp=_T("abcd");    LPTSTR chr_tmp = str_tmp.GetBuffer();
        int cch = _tcslen(chr_tmp);
    把这个改为
    char[100]=_T("abcd");
    int cch = _tcslen(chr_tmp);
     LPTSTR chr_tmp = str_tmp.GetBuffer(); 然后在项目属性里把unicode编码改为多字节的。应该就可以了