I have tried several ways to copy a textbuffer to the clipboard but all of them give me a very strange result. The first time I want to copy a text it works just fine but the second time however, it fails. The third time everything works again and the forth time it fails again. Then it repeats it self like that and I have no idea of why. I have attached an example. SetClipboardData returns 0 when it fails.


I'm using this procedure to copy a textbuffer:




Copy2CB proc pszStr:DWORD

LOCAL dwStrLen:DWORD
LOCAL hMemm:DWORD
LOCAL pMemm:DWORD

invoke lstrlen,pszStr
mov dwStrLen,eax
inc dwStrLen
invoke GlobalAlloc,GMEM_MOVEABLE,dwStrLen
mov hMemm,eax
invoke GlobalLock,hMemm
mov pMemm,eax
invoke lstrcpy,pMemm,pszStr
invoke GlobalUnlock,hMemm
invoke OpenClipboard,hWnd
invoke EmptyClipboard
invoke SetClipboardData,CF_TEXT,hMemm
invoke CloseClipboard
invoke GlobalFree,hMemm

ret
Copy2CB endp


What's wrong with it? Is it a bug in windows XP?
Posted on 2003-02-28 15:45:50 by Delight
Remove GlobalFree.
Posted on 2003-02-28 16:25:20 by The Svin
Works fine on my Windows 95c

To find out about the error, you could insert this code after SetClipBoard:



test eax, eax
jnz GoOn
invoke GetLastError
invoke FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS, NULL, eax, 1024, ADDR szBuff, 200, NULL
invoke MessageBox, NULL, ADDR szBuff, NULL, MB_ICONERROR or MB_OK

GoOn:


Don't forget to define szBuff db 200 DUP(0)
Posted on 2003-03-01 01:44:00 by aweX
Thank you The Svin, it works now, but I cannot understand why :confused: It seems very unlogical to me that I shouldn't free the handle after using it. I thought that would cause a memory loss every time the function is called.

aweX, thank you very much for the code snippet. It returned an error message that said something like "the reference (handle) is not valid".
Posted on 2003-03-01 14:36:24 by Delight
Because the memory handle is now in System usage after you pass it to SetClipBoardData.
It is global handle if you use it with Clipboard.
If you want Clipboard now to use it - then leave it alone.
Trying to free it you are trying to get it away from System.
Posted on 2003-03-01 15:24:32 by The Svin
OK, that explains it. Thank you very much.
Posted on 2003-03-01 16:16:16 by Delight