News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Copy listbox items to clipboard

Started by Seb, September 27, 2006, 11:40:58 PM

Previous topic - Next topic

japheth


this is an *unoptimised* version how it could be done:


invoke GlobalAlloc,GMEM_MOVEABLE AND GMEM_DDESHARE,ecx ; I also tried GMEM_FIXED.
mov gbuf,eax
invoke GlobalLock,gbuf
mov byte ptr [eax],0   ;<--- this must be done outside of loop
mov glock,eax
push ebx
xor ebx,ebx
.while (ebx<cnt)
invoke SendDlgItemMessage,hwnd,IDC_CONSOLE,LB_GETTEXT,ebx,addr item
inc ebx
.continue .if !eax ; Move to next if failed.
;mov itemlen,eax ; Store len of item.
invoke szCatStr,glock,addr item ; Copy item.
invoke lstrlen, glock   ;<--- added
mov ecx,[glock]
mov byte ptr [eax+ecx+0],0dh
mov byte ptr [eax+ecx+1],0ah
mov byte ptr [eax+ecx+2],00h
.endw
invoke GlobalUnlock,gbuf
pop ebx


Seb

Hi japheth,

thanks a lot, your latest code snippet solved the problems! :U And thanks to the rest, too! There is just one minor bug though; it will not copy at all or properly (bogus data bug...) if one attempts to copy several times in a row, but I'll look into that later. The important thing is that I got a functional base to work on.

Thanks again!

Regards,
Seb

sinsi

G'day Seb,

Found this in the PSDK
Quote
If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.

So it seems you can OpenClipboard with a NULL handle but not use SetClipboardData...
Light travels faster than sound, that's why some people seem bright until you hear them.

Seb

Hi sinsi, thanks for your answer.

I tried passing the hWnd as parameter to OpenClipboard, but the result was the same as before (succeeds to copy once, fails the other times). The odd thing is that, passing NULL to OpenClipboard worked just fine (also to copy to clipboard multiple times) in C++.

Regards,
Seb

sinsi

Maybe this has a bearing?
Quote
After SetClipboardData is called, the system owns the object identified by the hMem parameter. The application can read the data, but must not free the handle or leave it locked until the CloseClipboard function is called.

As a couple of people have said, post your source in a .zip and let us see the other bit.
Light travels faster than sound, that's why some people seem bright until you hear them.

Seb

Hi gents,

OK, I've wrapped up and zipped an example which contains a part of the code (I decided not to post my entire project because of some sensitive parts).

Thanks!

Regards,
Seb

[attachment deleted by admin]

sinsi

Well, seems to work just fine on my machine (XP Home SP2).

testing
testing2
testing3
testing4
Light travels faster than sound, that's why some people seem bright until you hear them.

Seb

Hi sinsi,

OK, I see. Did you also try copying the listbox contents multiple times in a row?

Regards,
Seb

sinsi

Oops. The 2nd time it pastes nothing, 3rd time garbage. 2 demerits for me...

Well, on the 2nd copy, windbg gives two errors:
QuoteInvalid Address specified to RtlGetUserInfoHeap
and if you continue,
QuoteInvalid Address specified to RtlFreeHeap
And on any subsequent copies no error is returned but the memory contains garbage.

In my quick'n'dirty test program, I got that error from putting the test string in .const
Light travels faster than sound, that's why some people seem bright until you hear them.

sinsi

OK. When you pass SetClipboardData a memory handle, you must not lock/unlock/free the handle.
Apparently the system owns the handle from then on (and presumably frees it when you call CloseClipboard).

Getting rid of the GlobalFree in your code makes it work, but doesn't seem to change mem usage in Task Manager.
Light travels faster than sound, that's why some people seem bright until you hear them.

Seb

Hi sinsi,

thanks a lot for your help! :U That did indeed solve the problem! I could've never figured that out myself, especially not since I knew practically the same code worked fine in C++.

Thanks again!

Regards,
Seb

sinsi

My pleasure, Seb. As an added bonus, I now know how to use the clipboard  :bg
Light travels faster than sound, that's why some people seem bright until you hear them.