News:

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

FormatMessage Error

Started by raleeper, August 14, 2011, 01:42:23 AM

Previous topic - Next topic

raleeper

I get an error from

invoke FormatMessage,1000,eax,eax,0,OFFSET erbf,100,0

This is the same as code that worked before.  The parameters are

 1000 = flag, from system        eax is  from GetLastErrorr
0 is language - default
erbf  DB  200 DUP (?)       is the receiving buffer
100 is buffer size in tchars
0 is arguments - none/default

I can't figure out what's wrong,  When the code worked it was global, but that can't matter, can it?

Can anyone help?

Thanks

Fuller context is:
germs2:
invoke GetLastError
invoke FormatMessage,1000,eax,eax,   0,OFFSET erbf,100,0
invoke MessageBox, hwnd, ADDR erbf, ADDR mbcap, MB_CANCELTRYCONTINUE
int 3
retn


This is in WinMain, which is:

WinMain proc hInst,hPrevinst,CmdLine,CmdShow

LOCAL msg:MSG
LOCAL hwnd:HWND

invoke RegisterClassEx, OFFSET LFwc

mov edx, CW_USEDEFAULT

invoke CreateWindowEx,WS_EX_APPWINDOW,ADDR ClassName,ADDR AppName,\
WS_OVERLAPPEDWINDOW,edx,edx,edx,edx,eax,eax,hInst,eax
or eax, eax
  jz germs2

mov [hwnd], eax

invoke ShowWindow,hwnd,SW_SHOWMAXIMIZED

invoke UpdateWindow, hwnd

.while TRUE
invoke GetMessage, ADDR msg,0,0,0
.break .if (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.endw

                                  [gsva omitted]
ret


CreateWindowEx returns 0 and we get to germs2, which is in Winmain just before Winmain endp



Gunner

 FORMAT_MESSAGE_FROM_SYSTEM == 4096 in decimal.... don't use hardcoded numbers in your source... should use the equates... 

This works fine for me...
invoke   SetLastError, 500
invoke   GetLastError
invoke   FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM, NULL, eax, NULL, OFFSET erbf, 100, NULL;   lea      esi, SpammerInfo
invoke   MessageBox, NULL, offset erbf, NULL, MB_CANCELTRYCONTINUE


- edit -
Sorry, just noticed you use 16 base... grrrr

either way, just tried:
invoke   SetLastError, 500
invoke   GetLastError
.radix 16
invoke   FormatMessage, 1000, NULL, eax, NULL, OFFSET erbf, 100, NULL;   lea      esi, SpammerInfo
.radix 10
invoke   MessageBox, NULL, offset erbf, NULL, MB_CANCELTRYCONTINUE


and it works as expected
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

raleeper

Quote from: Gunner on August 14, 2011, 02:13:41 AM
FORMAT_MESSAGE_FROM_SYSTEM == 4096 in decimal.... FormatMessage expects decimal not hex AFAIK... don't use hardcoded numbers in your source... should use the equates...  replace 1000 with FORMAT_MESSAGE_FROM_SYSTEM or 4096 and it will work!

This works fine for me...
invoke   SetLastError, 500
invoke   GetLastError
invoke   FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM, NULL, eax, NULL, OFFSET erbf, 100, NULL;   lea      esi, SpammerInfo
invoke   MessageBox, NULL, offset erbf, NULL, MB_CANCELTRYCONTINUE


I must be missing something.  eax is 578 on return from GetLastError.  I still get error (0) from FormatMessage and erbf is still all 0s.
testp.lst and the debugger dissasembly show the last push before call FormatMessage as push 1000h.  Likewise with your set and get (Except, of course, eax is 500)
Should I have mentioned that my code is radix 16?

germs2:
invoke GetLastError

;invoke SetLastError, 500
;invoke GetLastError
invoke FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM, NULL, eax, NULL, OFFSET erbf, 100, NULL
invoke MessageBox, NULL, offset erbf, NULL, MB_CANCELTRYCONTINUE

retn

Gunner

I will dl and look at your source, but your return code is invalid handle

~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

raleeper

Quote from: Gunner on August 14, 2011, 02:51:42 AM
I will dl and look at your source, but your return code is invalid handle


That's nice to know.

I appreciate your effort.

raleeper

I fixed the CreateWindowEx error, so the priority on the FormatMessage problem has dropped.

The problem still exists; no change.  But if you want to drop it, Gunner, I don't mind at all.

I really appreciate the efforts of you and the other Forum experts.

Thank you. :bg

Gunner

Createwindow.....  You were passing it an invalid handle for its parent?
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

raleeper

Quote from: Gunner on August 14, 2011, 05:10:45 AM
Createwindow.....  You were passing it an invalid handle for its parent?
Exactly.  I was passing the return from RegisterClassEx, an atom.  I thought I was passing the module handle, but that's not right either.  Right (I think - it works) is 0 - no parent.

The bug was easy to find; I just looked at a working program.
I caused the problem for myself by doing 2 things at once, the misguided change in Create and the error routine.

Thanks.

Gunner

Well, the parent of your main windows, is the desktop..  there is an equate for that... HWND_DESKTOP which is 0  I caught that in your createwindow right before I turned my computer off..  Now if only I can find a text editor for android that I like, then I can view your code on my phone :)
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

the problem may be in the WndProc, not the CreateWindowEx parameters
make sure you are using the correct hWnd for DefWindowProc

jj2007

Quote from: raleeper on August 14, 2011, 05:06:18 AM
I fixed the CreateWindowEx error, so the priority on the FormatMessage problem has dropped.

include \masm32\include\masm32rt.inc  ; make sure Masm32 macros are in...
; no other includes please, unless you are doing really exotic stuff
....
   print LastError$(), 13, 10  ; console mode
   MsgBox 0, LastError$(), "Hi", MB_OK  ; any mode

(but still it would be nice to know where the bug was, right? :bg)

raleeper

Quote from: dedndave on August 14, 2011, 06:03:55 AM
the problem may be in the WndProc, not the CreateWindowEx parameters
make sure you are using the correct hWnd for DefWindowProc
Well, I don't have the CreateWindowEx problem since I started (or went back to) passing 0 (which I thought meant none, but which Gunner informed me was the definition of HWND_DESKTOP) as the hparent.
I have a hard time shedding my linear thinking,  It never occurred to me that the OS might call the WndProc before it finishes WinMain.
So I'll check to make sure I'm using the correct hWnd for DefWindowProc.

Thank you

raleeper

Quote from: jj2007 on August 14, 2011, 07:14:40 AM
Quote from: raleeper on August 14, 2011, 05:06:18 AM
I fixed the CreateWindowEx error, so the priority on the FormatMessage problem has dropped.

include \masm32\include\masm32rt.inc  ; make sure Masm32 macros are in...
; no other includes please, unless you are doing really exotic stuff
....
   print LastError$(), 13, 10  ; console mode
   MsgBox 0, LastError$(), "Hi", MB_OK  ; any mode

(but still it would be nice to know where the bug was, right? :bg)

I'll need some time to digest this.

Thanks Jochen.

raleeper

I am giving up on the FormatMessage Error problem.
The return from FormatMessage is
ERROR_NOACCESS 3e6h = 998d Invalid access to memory location.

I don't need FormatMessage.  I'll just use the debugger to look at the return from GetLastError, or better yet have my program display it, then look it up.
More trouble, sure, but I'll just curse Microsoft.

Thanks





jj2007

Quote from: raleeper on August 14, 2011, 05:21:53 PM
I am giving up on the FormatMessage Error problem.

Don't give up so easily, no mystery is safe in the Masm32 Forum:

include \masm32\include\masm32rt.inc

.radix 16  ; test with and without
MyTest PROTO: DWORD, :DWORD

.code
AppName db "Masm32 is great!", 0
Hello db "A message:", 0

start:
invoke MyTest, offset AppName, addr Hello
exit

MyTest proc arg1:DWORD, arg2:DWORD
LOCAL msg:MSG, wx:HWND
  MsgBox 0, arg1, arg2, MB_OK
  ret
MyTest endp

end start


Olly reveals that the LOCALs are, well, a bit odd. Check for sub esp, xx :bg

Interestingly enough, JWasm produces the same nonsense.