Search code examples
assemblyx86clipboarddosdosbox

How can I insert text in the clipboard from an assembly program running in DOSBox?


I have a db variable in assembly containing a string, like so:

STR_VAR db 'test$'

Is it possible to copy this variable to the clipboard, so that when a user presses Ctrl+V in another program (such as word) it will paste the text in the variable.

EDIT: Important info: I am using DOSBox to run the code


Solution

  • in TASM syntax there are a few additions and changes you need to make so it will work with the newest DOSBOX-X. Here is the final working version

    IDEAL
    MODEL small
    STACK 100h
    DATASEG
    
    STR_VAR DB 'test',0
    CODESEG
    start:
        mov ax, @data
        mov ds, ax
    
            mov     ax,  1700h   ; IdentifyWinOldApVersion
            int     2fh
            cmp     ax,  1700h
            jz      error_2
    
            mov     ax,  1701h   ; OpenClipboard
            int     2fh
            test    ax, ax
            jz      error_2
    
            mov     ax,  1709h   ; ClipboardCompact
            mov     cx, 5  ; STR_VAR.end - STR_VAR
            xor     si, si
            int     2fh
            ;cmp     dx, si
            ;jb      error_2
            ;ja      fits
            cmp dx, 0
            jne fits
            cmp ax, 0
            je error_2
            jmp fits
            cmp     cx, ax
            jb      error_2
    
    fits:
            mov     ax,  1703h   ; SetClipboardData
            mov     dx, 7        ; CF_OEMTEXT
            mov     bx, offset STR_VAR
            ; ES and SI:CX already set up
            push ds
            pop es
            int     2fh
            test    ax, ax
            jz      error_2
            
            mov     ax,  1708h   ; CloseClipboard
            int     2fh
    
    
           mov     ax,  4c00h
            int      21
    
    error_2:
            mov     ax,  4c01h
            int      21
    
    exit:
        mov ax, 4c00h
        int 21h
    END start