Author Topic: Clipboard - GlobalAlloc fails  (Read 1427 times)

Offline Synthetic

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
    • ClanBam
Clipboard - GlobalAlloc fails
« on: 2009-Apr-24 »
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
« Last Edit: 2009-Apr-24 by Kitty Hello »
"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.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10739
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
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();
« Last Edit: 2009-Apr-24 by Kitty Hello »

Offline Synthetic

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
    • ClanBam
Re: Clipboard - GlobalAlloc fails
« Reply #2 on: 2009-Apr-24 »
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.