by John Munro >> Wed, 19 Dec 2007 16:41:22 GMT
I want to use a custom clipboard format so that application specific data can be copied to the clipboard from one instance of our application and pasted into another instance.
The data that's copied will make no sense to any other application, so using a custom format (rather than just Text) will allow other applications to ignore it.
The only clipboard support in Jade seems to be app.copyStringToClipboard etc. so in order to use a custom format I've had to use external functions.
The registerClipboardFormat, openClipboard etc. external functions all seem to work, but I'm having problems with the fact that to send the data to the clipboard I have to use globalAlloc, copyMemory etc. and I'm very weak with those.
My test method is getting an error 3 (Caught an unexpected C++ exception) at the stage of copying the data to the globally allocated buffer.
The test method is on a button click event because the documentation I read said that you need a real hwnd for openClipboard to work properly.
Can anyone help?
One odd thing is that RootSchema's _globalAlloc has the parameters as size and flags, but MSDN has them the other way around.
Also this is in 6.1 which is why I'm using Integer instead of MemoryAddress.
btnCopy_click(btn: Button input) updating;
constants
GMEM_MOVEABLE : Integer = 2;
vars
iClipFormat, iHandle, iPtr, iSize : Integer;
bClose : Boolean;
binData : Binary;
begin
iClipFormat := call registerClipboardFormat("CustomFormatTest");
if iClipFormat = 0 then
write "could not register clipboard format";
return;
endif;
if not call openClipboard(hwnd) then
write "could not open clipboard";
return;
endif;
bClose := true;
if not call emptyClipboard then
write "could not empty clipboard";
return;
endif;
binData := "<test>".Binary;
iSize := binData.length;
iHandle := call _globalAlloc(GMEM_MOVEABLE, iSize);
if iHandle = 0 then
write "could not allocate buffer";
return;
endif;
iPtr := call _globalLock(iHandle);
if iPtr = 0 then
write "could not lock handle";
return;
endif;
call copyMemory(iPtr, binData.bufferAddress, iSize);
call _globalUnlock(iHandle); // ensure this always happens
if call setClipboardData(iClipFormat, iHandle) = 0 then
write "could not set clipboard data";
return;
endif;
epilog
if bClose then
if not call closeClipboard then
write "could not close clipboard";
endif;
endif;
end;