Clipboard - GlobalAlloc fails

Previous topic - Next topic

Synthetic

Basically I'm just trying to read regular text (CF_TEXT) from the Windows clipboard but as soon as I pass the handle to GlobalLock, it crashes. No errors are being thrown when compiling and this code should work. Any ideas?  =D Sample code below.

Code (glbasic) Select

INLINE

DECLARE(IsClipboardFormatAvailable,"user32.dll",(int),int);
DECLARE(GetClipboardData,"user32.dll",(int),int);
DECLARE(OpenClipboard,"user32.dll",(void*),int);
DECLARE(CloseClipboard,"user32.dll",(void),int);
DECLARE(GlobalLock,"kernal32.dll",(int),int);
DECLARE(GlobalUnlock,"kernal32.dll",(int),int);

ENDINLINE

FUNCTION get_clipboard:
INLINE
HANDLE hData;
if(IsClipboardFormatAvailable(1) && OpenClipboard(GLBASIC_HWND()))
{
hData = GetClipboardData(1);
if(hData != NULL)
{
char *clipboard_data = (char*) GlobalLock(hData);
GlobalUnlock(hData);
}
CloseClipboard();
}
ENDINLINE
ENDFUNCTION
"Impossible is a word people use to make themselves feel better when they quit."

My AMXMODX plugins for Day of Defeat 1.3 can be found here.

Kitty Hello

#1
Thank you for reminding me. I wrote a cross platform Clipboard library once:
http://www.glbasic.com/showroom.php?game=clipboard

Edit:
BTW, here's how I did it for Win32:
Code (glbasic) Select

::EmptyClipboard();
// ::SetClipboardData(CF_TEXT, (void*)text_Str.c_str());


int cch = LEN(text_Str);
void* hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, cch+1);
        if (hglbCopy)
        {
        // Lock the handle and copy the text to the buffer.
        char* lptstrCopy = (char*)GlobalLock(hglbCopy);

        for(int n=0; n<=cch; ++n)
        {
        lptstrCopy[n] = text_Str[n];
        }
        lptstrCopy[cch] = '\0';
        GlobalUnlock(hglbCopy);
        SetClipboardData(CF_TEXT, hglbCopy);
        }
::CloseClipboard();


Synthetic

Oh excellent, I was also already looking into this for Linux as well. Looks like you just saved me some time. XD
"Impossible is a word people use to make themselves feel better when they quit."

My AMXMODX plugins for Day of Defeat 1.3 can be found here.