cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
guy_kroizman
Level 2

Putting text in clipboard

Hi
I am trying (unsuccessfully) to copy some text to the clipboard.
has anyone succeeded in doing so?

This is as far as I got:

// prototypes for the clipboard actions
prototype copyTextToClipboard(string );
prototype NUMBER User32.CloseClipboard();
prototype NUMBER User32.OpenClipboard(BYVAL NUMBER);
prototype NUMBER User32.SetClipboardData(NUMBER, NUMBER);
prototype STRING Kernel32.GlobalLock ( NUMBER );
prototype NUMBER Kernel32.GlobalUnlock(HWND);
prototype NUMBER Kernel32.GlobalAlloc ( NUMBER, NUMBER );



#define GMEM_MOVEABLE 0x0002
#define GMEM_ZEROINIT 0x0040

#define GHND (GMEM_MOVEABLE | GMEM_ZEROINIT)
#define CF_UNICODETEXT 13

// use win32 api to copy the given string to the clipboard.
function copyTextToClipboard(str)
NUMBER hClipboardData;
STRING pLockedStr;
string test;

begin

if (User32.OpenClipboard(NULL) != 0) then

hClipboardData = Kernel32.GlobalAlloc(GHND, StrLength(str)+2); // added +2 for the null that AFAIK might be UTF-16

//Lock Clipboard, getting pointer to actual data.
pLockedStr = Kernel32.GlobalLock(hClipboardData);

CopyBytes(pLockedStr,0, str, 0, StrLength(str));

Kernel32.GlobalUnlock(hClipboardData);

User32.SetClipboardData(CF_UNICODETEXT, hClipboardData);

User32.CloseClipboard();
AskText("Paste here to check if paste to clipboard works...", "", test);

endif;


end;



From my logs I see that pLockedStr gets the proper value, but somehow when I press the ctrl+v I get nothing. (previous content of clipboard got deleted.)
Labels (1)
0 Kudos
(1) Reply
SMadden
Level 6

try to make pLockedString a pointer and use lstrcopy from kernel32 to copy the data in.

prototype POINTER Kernel32.lstrcpy( POINTER, POINTER );

function...
POINTER pLockedStr, pstr;
begin
...
//CopyBytes(pLockedStr,0, str, 0, StrLength(str));
pstr = &str;
lstrcpy( pLockedString , pstr );

Kernel32.GlobalUnlock(hClipboardData);
User32.SetClipboardData(CF_TEXT, hClipboardData);
User32.CloseClipboard();
0 Kudos