News:

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

[Resolved] Show hwnd in MessageBox

Started by fragpuff, January 12, 2009, 07:20:31 AM

Previous topic - Next topic

fragpuff

I just started learning assembly a couple hours ago, coming from Visual Basic 6 which I am really experienced with (and very little C++ knowledge).

So far, so good, not having any trouble understanding anything.

One thing (which is probably really simple) that I need. I came up with a small program to find the notepad window, and it works, but how do I show the result (which is a DWORD/HWND) as the text in a messagebox?

I need to convert the dword into a string somehow...but not sure.

I am using the (latest?) MASM32 from the download page (masm32.com).

Here is the code...

.386
.MODEL flat, stdcall
option casemap:none


; Include directives.
include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib

; Initialized data.
.DATA
    szWindowCaption     db      "Untitled - Notepad", 0
    szMsgCaptionFound   db      "Window Found!", 0
    szMsgTextFound      db      "Notepad was found!", 0
    nSleepTime          equ     400
   

; Uninitialized data.
.DATA?
    nWinHandle          HWND    ?


; Program code.
.CODE

Start:

    .WHILE (!EAX)
        invoke  FindWindow, NULL, ADDR szWindowCaption
    .ENDW

    mov     nWinHandle, EAX
    invoke  MessageBox, NULL, ADDR szMsgTextFound, ADDR szMsgCaptionFound, MB_OK
    invoke  MessageBox, NULL, ADDR nWinHandle, ADDR szMsgCaptionFound, MB_OK
    invoke  ExitProcess, 0
   
End Start

fragpuff

Nevermind, I got it by using the dwtoa function.

.486
.MODEL flat, stdcall
option casemap:none

; Function prototypes
dwtoa proto :DWORD,:DWORD

; Include directives.
include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib

; Initialized data.
.DATA
    szWindowCaption     db      "Untitled - Notepad", 0
    szMsgCaptionFound   db      "Window Found!", 0
    szMsgTextFound      db      "Notepad was found!", 0
    nSleepTime          equ     400
   

; Uninitialized data.
.DATA?
    nWinHandle          HWND    ?
    szWinHandle         db      ?


; Program code.
.CODE

align 4

dwtoa proc dwValue:DWORD, lpBuffer:DWORD

    ; -------------------------------------------------------------
    ; convert DWORD to ascii string
    ; dwValue is value to be converted
    ; lpBuffer is the address of the receiving buffer
    ; EXAMPLE:
    ; invoke dwtoa,edx,ADDR buffer
    ;
    ; Uses: eax, ecx, edx.
    ; -------------------------------------------------------------

    push ebx
    push esi
    push edi

    mov eax, dwValue
    mov edi, [lpBuffer]

    or eax,eax
    jnz sign

  zero:
    mov word ptr [edi],30h
    ret

  sign:
    jns pos
    mov byte ptr [edi],'-'
    neg eax
    inc edi

  pos:
    mov ecx, 3435973837
    mov esi, edi

    .while (eax > 0)
      mov ebx,eax
      mul ecx
      shr edx, 3
      mov eax,edx
      lea edx,[edx*4+edx]
      add edx,edx
      sub ebx,edx
      add bl,'0'
      mov [edi],bl
      inc edi
    .endw

    mov byte ptr [edi], 0       ; terminate the string

    ; We now have all the digits, but in reverse order.

    .while (esi < edi)
      dec edi
      mov al, [esi]
      mov ah, [edi]
      mov [edi], al
      mov [esi], ah
      inc esi
    .endw

    pop edi
    pop esi
    pop ebx

    ret

dwtoa endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

Start:

    .WHILE (!EAX)
        invoke  FindWindow, NULL, ADDR szWindowCaption
    .ENDW

    mov     nWinHandle, EAX
    invoke  dwtoa, EAX, ADDR szWinHandle
    invoke  MessageBox, NULL, ADDR szMsgTextFound, ADDR szMsgCaptionFound, MB_OK
    invoke  MessageBox, NULL, ADDR szWinHandle, ADDR szMsgCaptionFound, MB_OK
    invoke  ExitProcess, 0
   
End Start

jj2007

Quote from: fragpuff on January 12, 2009, 07:20:31 AM
Start:
    .WHILE (!EAX)
        invoke  FindWindow, NULL, ADDR szWindowCaption
    .ENDW

[/quote]

Test what happens if Notepad is not present. And consider using [url=http://www.ollydbg.de/]OllyDebug[/url] :bg

fragpuff

Quote from: jj2007 on January 12, 2009, 11:16:47 AM
Quote from: fragpuff on January 12, 2009, 07:20:31 AM
Start:
    .WHILE (!EAX)
        invoke  FindWindow, NULL, ADDR szWindowCaption
    .ENDW

[/quote]

Test what happens if Notepad is not present. And consider using [url=http://www.ollydbg.de/]OllyDebug[/url] :bg


It will loop indefinitely until it is found...I had code to use the Sleep() API so it would not utilize 100% CPU, but removed it before posting.

And I already have OllyDbg, but why would you even suggest that?

jj2007

Quote from: fragpuff on January 12, 2009, 03:36:02 PM
It will loop indefinitely until it is found...I had code to use the Sleep() API so it would not utilize 100% CPU, but removed it before posting.

And I already have OllyDbg, but why would you even suggest that?

Because I didn't know that 100% CPU use was "by design", and wanted to help you what I erroneously thought was a coding bug.