Click to See Complete Forum and Search --> : Clipboard program


Karen
April 19th, 1999, 03:58 PM
So I got a grip over the weekend and eliminated most of my problems, however, one remains. When I try to use the function GetClipboardData, it returns a handle to clipboard object and I am trying to convert that into an char array, but it just puts garbage in the array.
Here is the function - any suggestions would be appreciated.

void From_Clipboard(HWND TextEdit, HWND hwnd2){

char destination[20];

if(OpenClipboard(hwnd2)){
static HGLOBAL clipbuffer;
static char * buffer;
clipbuffer = GlobalAlloc(GMEM_MOVEABLE, strlen(destination)+1);
buffer = (char*)GlobalLock(clipbuffer);
clipbuffer = GetClipboardData(CF_TEXT);
lstrcpy(destination, LPCSTR(buffer));
GlobalUnlock(clipbuffer);
SetWindowText(TextEdit, destination);
}

else{
MessageBox(NULL, "Unable to read from clipboard!", "Error",
MB_OK | MB_ICONINFORMATION);
}

CloseClipboard();
}

Paul McKenzie
April 19th, 1999, 06:00 PM
This has not been tested:

if(OpenClipboard(hwnd2)){
static HGLOBAL clipbuffer;
static char * buffer;

// Get handle to clipboard data
clipbuffer = GetClipboardData(CF_TEXT);

// Now lock the handle and return pointer to lock
buffer = (char*)GlobalLock(clipbuffer);

// No need to allocate again, buffer points to the clipboard data.
//Why duplicate what the clipboard is supposed to do, namely store the string?

// What if text > 20 characters???
lstrcpy(destination, LPCSTR(buffer));

GlobalUnlock(clipbuffer);

// You forgot to close clipboard
CloseClipboard();

SetWindowText(TextEdit, destination);
}

Regards,

Paul McKenzie