如何从在 DOSBox 中运行的汇编程序在剪贴板中插入文本?

问题描述

我在程序集中有一个包含字符串的 db 变量,如下所示:

STR_VAR db 'test$'

是否可以将此变量复制到剪贴板,以便当用户在另一个程序(例如 word)中按 Ctrl+V 时,它会将文本粘贴到变量中。

编辑: 重要信息: 我正在使用 DOSBox 运行代码

解决方法

实际上有一个用于访问主机剪贴板的 DOS 官方 API,在 Windows 3.x 和 9x 中支持。它在文章 Q67675 中进行了简要描述,之前位于 Microsoft 的知识库中。

这是一个代码示例(NASM 语法):

org 0x100

        mov     ax,0x1700   ; IdentifyWinOldApVersion
        int     0x2f
        cmp     ax,0x1700
        jz      error

        mov     ax,0x1701   ; OpenClipboard
        int     0x2f
        test    ax,ax
        jz      error

        mov     ax,0x1709   ; ClipboardCompact
        mov     cx,STR_VAR.end - STR_VAR
        xor     si,si
        int     0x2f
        cmp     dx,si
        jb      error
        ja      .fits
        cmp     cx,ax
        jb      error

.fits:
        mov     ax,0x1703   ; SetClipboardData
        mov     dx,7        ; CF_OEMTEXT
        mov     bx,STR_VAR
        ; ES and SI:CX already set up
        int     0x2f
        test    ax,ax
        jz      error
        
        mov     ax,0x1708   ; CloseClipboard
        int     0x2f

        mov     ax,0x4c00
        int     0x21

error:
        mov     ax,0x4c01
        int     0x21

STR_VAR:
        db      'test'
.end:

我非常怀疑香草 DOSBox 是否真的支持这个 API(尽管它显然在 DOSBox-X 中得到支持,一个分支)。

,

在 TASM 语法中,您需要进行一些添加和更改,以便它可以与最新的 DOSBOX-X 一起使用。 这是最终的工作版本

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,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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...