I would write it like this:
program ProbeClipboard; {$APPTYPE CONSOLE} uses SysUtils, Windows, ClipBrd; var CF_GPX: Cardinal; ClipboardData: Windows.HGLOBAL; Ptr: Pointer; Size: DWORD; begin CF_GPX := RegisterClipboardFormat('GPX'); Clipboard.Open; try if Clipboard.HasFormat(CF_GPX) then begin Writeln('GPX format available in clipboard'); ClipboardData := Clipboard.GetAsHandle(CF_GPX); if ClipboardData=0 then RaiseLastOSError; Ptr := Windows.GlobalLock(ClipboardData); if Ptr=nil then RaiseLastOSError; try Size := Windows.GlobalSize(ClipboardData); //Ptr now points to a memory block of Size bytes //containing the clipboard data finally Windows.GlobalUnlock(ClipboardData); end; end; finally Clipboard.Close; end; end.
Note that I have moved the clipboard Open command, which blocks the clipboard outside the test for the CF_GPX format. That is, to avoid the race condition that exists in your code. In your code, the clipboard can be changed between a call to HasFormat and a call to OpenClipboard .
I also used the Clipboard class. This class has everything you need, and you do not need to use the original Win32 clipboard API.
I even checked the error!
source share