News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

New member

Started by Mike, July 29, 2016, 10:03:31 PM

Previous topic - Next topic

Mike

Hello Everyone,

I am new to this forum and masm. I have written a few 16-bit programs in TASM before and am going to try to upgrade one of these for masm. I suspect that maybe it will be a rewrite, rather than just an upgrade.

I have a question about the call to close the clipboard in routine GetClipboardText, which can be found in \masm32\m32lib\getcbtxt.asm. It seems to me that the call to CloseClipboard always returns eax set to 1 regardless if text is available or not. This means that the following instruction sequence will not be able to report an empty clipboard.

    .else                                       ; else
      xor eax, eax                              ; set return to zero
      invoke CloseClipboard                     ; close the clipboard
      jmp bye               ; eax = 1 now
    .endif

It should be changed to

    .else                                       ; else
      invoke CloseClipboard                     ; close the clipboard
      xor eax, eax                              ; set return to zero
      jmp bye
    .endif

Would you agree or is there something I have not understood here?

Regards
Mike

fearless

Hi Mike,

CloseClipboard
Return Values:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero


It appears that eax == 1 is fine, as in it is closing the clipboard.

To empty the clipboard, with EmptyClipboard, the following info means that the clipboard shouldnt be closed:

EmptyClipboard
Remarks:
Before calling EmptyClipboard, an application must open the clipboard by using the OpenClipboard function. If the application specifies a NULL window handle when opening the clipboard, EmptyClipboard succeeds but sets the clipboard owner to NULL.


So if you are going to empty the clipboard , open it first, do whatever you need, then empty the clipboard, then close it. Hope that helps

jj2007

Mike,

That code looks a bit old, must be from the very early stages of the Masm32 project. And you are right, the retval doesn't look good. It should look more or less like this:

  invoke OpenClipboard, NULL ; open clipboard
  xor edi, edi ; default retval: 0
  .if rv(IsClipboardFormatAvailable, CF_TEXT) ; if text available
invoke GetClipboardData, CF_TEXT ; get pointer to text
xchg eax, ebx
invoke StrLen, ebx ; get text length
lea esi, [eax+1]
mov edi, alloc(esi) ; allocate that much memory
cst edi, ebx ; copy string
  .endif
  invoke CloseClipboard ; close the clipboard
  xchg eax, edi ; return memory handle or zero
  pop edi


@fearless: Mike is right, with CloseClipboard as last instruction, the proc will always report "text available". Use Clip$() instead ;)

Vortex

Here is a quick example :

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib

.data

text1       db 'Text in the clipboard',0
app         db  'C:\WINDOWS\notepad.exe', 0
WndClass    db  'Notepad', 0
ChildClass  db  'Edit', 0

.data?

startinfo   STARTUPINFO            <?>
processinfo PROCESS_INFORMATION    <?>

.code

start:

    invoke  OpenClipboard,0         ; clear the content
    invoke  EmptyClipboard          ; of the clipboard
    invoke  CloseClipboard

    invoke  SetClipboardText,\      ; write to the clipboard
            ADDR text1

    invoke  CreateProcess,ADDR app,\
            0,0,0,0,0,0,0,\
            ADDR startinfo,\
            ADDR processinfo
           
    invoke  Sleep,500
    invoke  FindWindow,ADDR WndClass,0
    invoke  FindWindowEx,eax,0,ADDR ChildClass,0
    invoke  SendMessage,eax,WM_PASTE,0,0

    invoke  ExitProcess,0

END start

Mike

Thank you guys for very interesting code examples!

@jj2007: I like your version of GetClipboardText and will use that code instead of the original. Thanks!