关于操作剪切板的有关问题

关于操作剪切板的问题
在MFC中 下面操作剪切板的代码不成功 不晓得问题出现在哪,麻烦有知道的说下,多谢。

CString TotalStr = "123456";

char* Buffer;  
HGLOBAL ClipBuffer;
EmptyClipboard();
ClipBuffer = GlobalAlloc(GMEM_MOVEABLE, TotalStr.GetLength() + 1);
Buffer = (char*)GlobalLock(ClipBuffer);
memcpy(Buffer, TotalStr.GetBuffer(),  TotalStr.GetLength());
GlobalUnlock(ClipBuffer);
SetClipboardData(CF_TEXT, ClipBuffer);
CloseClipboard();
GlobalFree(ClipBuffer);

------解决方案--------------------
EmptyClipboard
The EmptyClipboard function empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the window that currently has the clipboard open. 

BOOL EmptyClipboard(VOID)
 
Parameters
This function has no parameters. 

Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. 

Remarks
Before calling EmptyClipboard, an application must open the clipboard by using the OpenClipboard function. If the application specifies a NULL window handle when opening the clipboard, EmptyClipboard succeeds but sets the clipboard owner to NULL. 

QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Requires version 1.0 or later.
  Header: Declared in winuser.h.
  Import Library: Use user32.lib.

See Also
Clipboard Overview, Clipboard Functions, OpenClipboard, SetClipboardData, WM_DESTROYCLIPBOARD