flat assembler
Message board for the users of flat assembler.

Index > Windows > Clipboard: copy text from window

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 07 Jan 2019, 18:07
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-openclipboard
Quote:
If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.
Which appears to be an error in the documentation. The documentation says to use NULL for hwnd to "associated with the current task", and the following code works without error.
Code:
hWndTextW_To_ClipBoard:
        virtual at rbp-.FRAME
                                rq 4    ; shadow forward
        rb (16-(($-$$) AND 15)) AND 15  ; only if padding needed
                .FRAME := $-$$
                                rq 1    ; saved RBP
                                rq 1    ; return address
                .hWnd           rq 1    ; our shadow
                .hMem           rq 1    ; our shadow
                .pBuffer        rq 1    ; our shadow
                .cchBuffer      rq 1    ; our shadow
        end virtual
        enter .FRAME,0

        mov [.hWnd],rcx
        call [GetWindowTextLengthW]
        ; note: can be greater than number of characters
        xchg ecx,eax
        jrcxz .return
        ; include null
        lea rdx,[rcx+1]
        mov [.cchBuffer],rdx
        push 2 ; GMEM_MOVEABLE
        pop rcx
        shl edx,1 ; widechar bytes
        call [GlobalAlloc]
        ; handle to memory object
        xchg rcx,rax
        jrcxz .return
        mov [.hMem],rcx
        call [GlobalLock]
        mov [.pBuffer],rax
        xchg r9,rax
        mov r8,[.cchBuffer]
        push 13 ; WM_GETTEXT
        pop rdx
        mov rcx,[.hWnd]
        call [SendMessageW]
        ; pass control to clipboard
        mov rcx,[.hMem]
        call [GlobalUnlock]
        xor ecx,ecx ; current task
        call [OpenClipboard]
        call [EmptyClipboard]
        mov rdx,[.hMem]
        push 13 ; CF_UNICODETEXT
        pop  rcx
        call [SetClipboardData]
        call [CloseClipboard]
.return:
        leave
        retn    
Coded for size because it's just a bunch of API calls.

If your window handles clipboard messages then the code is simply:
Code:
        mov rcx,[.hWnd]
        call [OpenClipboard]
        call [EmptyClipboard]
        xor edx,edx
        push 1 ; CF_TEXT
        pop  rcx
        call [SetClipboardData]
        call [CloseClipboard]    

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 07 Jan 2019, 18:07
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.