News:

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

push/pop pointer for invoke

Started by Mark Jones, July 04, 2005, 08:40:44 PM

Previous topic - Next topic

Mark Jones

Hi guys, what's the correct way to pass an ADDR across the stack? I must have tried a hundred different things because Olly isn't liking my program. :)


                    push dword ptr szTemp1
                    call verifyerr
                    push dword ptr szTemp2
                    call verifyerr
                ret
verifyerr:
            pop eax
            invoke MessageBox,hWnd,eax,addr szVerifyErr,MB_OK
            ret
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

chep

I'd say push OFFSET szTemp1 ?

EDIT:
Well it will be better if I open at least one eye... :red

The first thing you're doing in your "procedure" is to pop the *return address*. As a consequence, the ret gives the control back to... a string!

this works fine:
  push offset szTemp1
  call verifyerr
  push offset szTemp2
  call verifyerr
  ret
verifyerr:
  mov  eax, [esp+4]
  invoke MessageBox,0,eax,addr szVerifyErr,MB_OK
  ret

Mark Jones

Aaaha, that's the ticket. :)

Sheesh, I tried dereferencing the pointer then PUSHing it, then pushing it normally and POPing it, then dereferencing, even tried LEA eax, dword ptr szTemp1 / push eax, etc...  :lol Thanks Chep. :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

raymond

You should also clean the stack after each call (or wihtin your verifyerr routine). Otherwise, your last ret will land you in trouble.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Mark Jones

 Hi Raymond, Like this?


                invoke CreateFile,addr szTemp5,GENERIC_READ,\  ; open file for reading
                       0,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
                mov hFile,eax
                invoke GetLastError             ; does file exist?
                test eax,eax                    ; if so, eax will be 0
                jnz allgood                     ; if file exists, exit tests
                push offset szTemp5             ; if no exist, push file path
                call verifyerr                  ; generate error msg
                pop eax                         ; balance stack?
allgood:
                invoke CloseHandle,hFile
                ret                             ; end procedure
verifyerr:
                mov eax,[esp+4]
                invoke MessageBox,hWnd,eax,addr szVerifyErr,MB_OK
                ret
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Mirno

Raymond means you need a ret 4 in verifyerr, rather than a plain ret, it saves the ugly pop eax.

Mirno

Mark Jones

 Oh that is much cleaner, thanks.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Mark Jones

Couldn't get that to work. Even tried manipulating stack values, but it wouldn't work correctly. In the end, used a proc instead.


myproc proc
                invoke CreateFile,addr szTemp5,GENERIC_READ,\  ; test file exist
                       0,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
                mov hfile,eax
                invoke GetLastError             ; does file exist?
                test eax,eax                    ; if so, eax will be 0
                jz allgood                     ; if file exists, exit tests
                invoke verifyerr,addr szTemp5                  ; generate error msg
allgood:
                invoke CloseHandle,hfile
                ret                             ; end procedure
myproc endp

verifyerr proc string:DWORD
    invoke MessageBox,hWnd,string,addr szVerifyErr,MB_OK
    ret
verifyerr endp
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Rifleman

Mark,
I think that is the best and most stable version of your code.  If verifyerr is a generic error reporter then that is a good version otherwise, if it is only called once, I would remove it and just put the MessageBox invoke code in the main proc.  It all depends upon what you are doing and I only see a small portion of your code.

Paul

Mark Jones

Thanks Paul. The code is/was a mess, which is why I didn't post more of it. :) Basically I'm checking to see if a series of files exist, then asking to overwrite them, then checking to make sure the files were created. Like this:


myproc proc
    invoke VerifyOverwrite,addr szTemp5     ; overwrite first file?
    .if eax==1                              ; if yes,
        invoke ShellExecute,hWnd,0,addr lpMyFile,addr params,0,SW_HIDE
    .endif
    invoke VerifyWritten,addr szTemp5       ; was it written?

    invoke VerifyOverwrite,addr szTemp4     ; overwrite second file?
    .if eax==1
        invoke temp1,addr szTemp1,addr szTemp4
    .endif
    invoke VerifyWritten,addr szTemp4       ; was third file written?
    .if def==1
        invoke VerifyWritten,addr szTemp3   ; was fourth file written?
    .endif
myproc endp


VerifyWritten proc lpLocation:DWORD
LOCAL hFile:DWORD

    invoke CreateFile,lpLocation,GENERIC_READ,\  ; test for file exist
           0,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
    mov hFile,eax
    invoke GetLastError             ; does file exist?
    test eax,eax                    ; if so, eax will be 0
    jz @F
    invoke MessageBox,hWnd,lpLocation,addr szVerifyErr,MB_OK
@@:
    invoke CloseHandle,hFile        ; file exists
    ret                             ; end procedure
VerifyWritten endp


VerifyOverwrite proc lpLocation:DWORD
LOCAL hFile,result:DWORD

    mov [result],1                  ; default to overwrite
    invoke CreateFile,lpLocation,GENERIC_READ,\
            0,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
    mov hFile,eax
    invoke GetLastError             ; does file exist?
    test eax,eax                    ; if so, eax will be 0
    jnz @F                          ; and skip "overwrite?" msg
    invoke MessageBox,hWnd,lpLocation,addr szOverwrite,MB_YESNO
    .if eax==IDNO
        mov [result],0              ; do not overwrite
    .endif
@@:
    invoke CloseHandle,hFile
    mov eax,[result]
    ret
VerifyOverwrite endp
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Rifleman

Mark,
Looks good to me and I am glad to see comments in the code.  I wish everyone used comments in published code.  It helps the newbies.

Paul

Mark Jones

I always comment my code. It's the only way I can keep track of what is going on. :toothy
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08