Re: SetClipBoard returns Junk characters!
"Anu" <anu.mishra@gmail.com> ha scritto nel messaggio
news:86095524-a89a-448b-8fa9-637c3115bb99@a1g2000hsb.googlegroups.com...
I am trying this code to copy clipboard, but when I do paste, i get
junk characters only (like chinese) in non unicode build of my app.
But in unicode build of my app, it works fine. How can I make it work
in Non unicode? CF_TEXT format works, but I am expecting non english
characters as URL, so I have to use CF_UNICODE format.
These are two routines I wrote to copy Unicode text to clipboard, using
Unicode format (CF_UNICODETEXT) and ANSI format (CF_TEXT).
The code is commented, so you can follow it.
<code>
//-------------------------------------------------------------------
// Copies Unicode text to the clipboard.
// Returns FALSE on error, TRUE on success.
//-------------------------------------------------------------------
BOOL CTestClipboardCopyDlg::CopyTextAsUnicode( const wchar_t * text )
{
ASSERT( text != NULL );
// Open the clipboard
if ( !OpenClipboard() )
return FALSE;
// Empty clipboard, so we can copy data to it
if ( ! EmptyClipboard() )
{
CloseClipboard();
return FALSE;
}
// Allocate global memory object for text.
// This will be used to copy text in to the clipboard.
SIZE_T bytesToCopy = (wcslen( text ) + 1) * sizeof(wchar_t);
HGLOBAL hCopy = GlobalAlloc( GMEM_MOVEABLE, bytesToCopy );
if ( hCopy == NULL )
{
CloseClipboard();
return FALSE;
}
// Lock memory, and copy text to it
wchar_t * destination = (wchar_t *) GlobalLock( hCopy );
if ( destination == NULL )
{
GlobalUnlock( hCopy );
CloseClipboard();
return FALSE;
}
memcpy( destination, text, bytesToCopy );
// Unlock memory block
GlobalUnlock( hCopy );
// Copy text to the clipboard, in Unicode format
if ( SetClipboardData( CF_UNICODETEXT, hCopy ) == NULL )
{
CloseClipboard();
return FALSE;
}
// Release the clipboard
CloseClipboard();
// All right
return TRUE;
}
//-------------------------------------------------------------------
// Copies Unicode text to the clipboard, using ANSI/MBCS.
// Returns FALSE on error, TRUE on success.
//-------------------------------------------------------------------
BOOL CTestClipboardCopyDlg::CopyTextAsAnsi( const wchar_t * text )
{
ASSERT( text != NULL );
// Convert text from Unicode to ANSI/MBCS
CW2A ansiText( text );
// Open the clipboard
if ( !OpenClipboard() )
return FALSE;
// Empty clipboard, so we can copy data to it
if ( ! EmptyClipboard() )
{
CloseClipboard();
return FALSE;
}
// Allocate global memory object for text.
// This will be used to copy text in to the clipboard.
SIZE_T bytesToCopy = (strlen( ansiText ) + 1) * sizeof(char);
HGLOBAL hCopy = GlobalAlloc( GMEM_MOVEABLE, bytesToCopy );
if ( hCopy == NULL )
{
CloseClipboard();
return FALSE;
}
// Lock memory, and copy text to it
char * destination = (char *) GlobalLock( hCopy );
if ( destination == NULL )
{
GlobalUnlock( hCopy );
CloseClipboard();
return FALSE;
}
memcpy( destination, ansiText, bytesToCopy );
// Unlock memory block
GlobalUnlock( hCopy );
// Copy text to the clipboard, in ANSI format
if ( SetClipboardData( CF_TEXT, hCopy ) == NULL )
{
CloseClipboard();
return FALSE;
}
// Release the clipboard
CloseClipboard();
// All right
return TRUE;
}
</code>
HTH,
Giovanni