( Since I can't post into my thread which is now in the FAQ section, I will
make the addition in this new thread. The referring thread is this one:

http://www.asmcommunity.net/board/showthread.php?threadid=5408

It contains undocumented bugs of 2k/xp ; case #1 and #2 are there. Now
let's continue to...
)

:alright: case #3:

On Windows 2k and XP platforms, some API functions cannot be called from
inside Dialog-Procedures ... They will fail mysteriously!

Examples:
socket() and sendto() of WinSock, as well as IPersistFile::Save
and sometimes MessageBox() will show 'broken' MsgBoxes (no caption/border)

Make sure to call them before creating the Dialogbox if possible or create your
windows the common way: RegisterClassEx() ; CreateWindowEx()



I hope to have saved you from wasting time finding stupid 2k/xp peculiarities.
This bug has once again wasted a few days of testing, just like the other two bugs,
mentioned in the thread above.


aweX <-
Posted on 2003-01-27 01:36:54 by aweX
socket stuff not working inside dlgprocs? geeez, I must have done mad haxx0r 1337 wizardry on my old chat app then.
Posted on 2003-01-27 04:30:59 by f0dder
Yeah that's exactly what 2k and XP require :tongue:

I had that stuff inside dlgproc: didn't work on 2k/xp
Put them before creation of dlgproc: worked!

Dialog created by DialogBoxParam via a "DialogEx"-template.

Can you show me that magic part of your code? :o



aweX <-
Posted on 2003-01-27 06:18:04 by aweX
nah..you are prolly wrong.Check your code/dialog procedure (preserve regs etc.)
Posted on 2003-01-27 07:43:03 by DZA
Now I tried it with CreateDialogParam(), but socket() still fails.
It seems like I have to register my own window messages and
use the messageloop outside of the dlgproc to do all the winsock work...

I have preserved the regs, as far as it is needed: Anyways, what
should preserving the regs do when a func fails inside the DlgProc,
that is: before exiting the proc.

Well, just try to make a simple dialog and use socket inside the dlgproc.
You will see ...


aweX <-
Posted on 2003-01-27 08:48:33 by aweX
i wrote a little dialog based mail checker thingie and the socket() function never failed for me... also I have never seen a 'broken' message box on my XP machine and I put more messageboxes in my dialogproc than message handlers sometimes... =/ Are you sure your 2K/XP installiation is not corrupt?
Posted on 2003-01-27 10:07:55 by BubbaFate
broken messagebox == misaligned stack. try this on a NT machine:


sub esp, 2
invoke MessageBox, NULL, offset szText, offset szText, MB_OK
.

then try replacing the "sub esp, 2" with "sub esp, 4".
Posted on 2003-01-27 12:48:03 by f0dder

broken messagebox == misaligned stack. try this on a NT machine:


sub esp, 2
invoke MessageBox, NULL, offset szText, offset szText, MB_OK
.

then try replacing the "sub esp, 2" with "sub esp, 4".

exactly. some time ago, i had to find out about that the hard way ;)
Posted on 2003-01-27 13:23:10 by Tola
hi awex
--
Well, just try to make a simple dialog and use socket inside the dlgproc.
You will see ...
--


this worked ok winxp pro:

#include <c:\masm32\include\resource.h>

#define IDD_DIALOG1 101

IDD_DIALOG1 DIALOG DISCARDABLE 0, 0, 217, 130
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "socket() in DlgProc"
FONT 8, "MS Sans Serif"
BEGIN
END


.386
.model flat, stdcall
option casemap:none

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

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


; --------------PROTOTYPES--------------------------

DlgProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

; --------------CONSTANTS---------------------------

.const

IDD_DIALOG1 equ 101

; ------------INITIALIZED DATA----------------------

.data

szTitle db "socket() within dlgProc",0
szError db "Winsock Error %lu",0
szOk db "socket created ok.",0

; ------------UNINITIALIZED DATA--------------------

.data?

hInstance dd ?
szBuffer db 256 dup (?)
WSAData WSADATA <?>
sock SOCKET ?

; ------------CODE----------------------------------

.code

START:

; ----------------------CODE ENTRY------------------------

invoke GetModuleHandle, NULL
mov hInstance, eax
invoke DialogBoxParam, hInstance, IDD_DIALOG1 ,NULL,ADDR DlgProc,NULL
invoke ExitProcess, eax

; ----------------------DIALOG PROC-----------------------

DlgProc PROC USES EDI ESI EBX hDlg:HANDLE, uMsg:DWORD, wParam:DWORD, lParam:DWORD

cmp uMsg, WM_INITDIALOG
jnz _1

invoke WSAStartup, 202h, ADDR WSAData
test eax, eax
jnz _winsockError

invoke socket, AF_INET, SOCK_DGRAM, 0
cmp eax, INVALID_SOCKET
jz _winsockError

mov sock, eax

invoke MessageBox, hDlg, ADDR szOk, ADDR szTitle, MB_OK

mov eax,1
ret

_1:
cmp uMsg, WM_CLOSE
jnz _returnNull
invoke closesocket, sock
invoke WSACleanup
invoke EndDialog, hDlg, 0
mov eax,1
ret

_returnNull:
xor eax,eax
ret

_winsockError:
cmp eax, INVALID_SOCKET
jnz @F
invoke WSAGetLastError
@@:
invoke wsprintf, ADDR szBuffer, ADDR szError, eax
invoke MessageBoxA, hDlg, ADDR szBuffer , NULL, MB_OK or MB_ICONERROR
mov eax, 1
ret

DlgProc ENDP

END START
Posted on 2003-01-27 18:29:02 by david

I have preserved the regs, as far as it is needed: Anyways, what
should preserving the regs do when a func fails inside the DlgProc,
that is: before exiting the proc.
Well, I hope you're not making the mistake of storing the socket in a LOCAL variable, and expecting it to be unchanged between messages. LOCALs don't work that way, whether it's ASM, C, or Pascal.
Posted on 2003-01-27 18:46:49 by tenkey
I don't manipulate the stack with anything besides push and pop.

Ok it seems like it's time for a little proof before you all think I want to fool you...

I have attached my little shortcut editor. Opens a ".lnk", extracts the info via
COM and can also save the file by using IPersistFile::Save ... but that function
fails on 2k and XP systems. Furthermore, the error MessageBox is displayed
broken. (Referring to shortcut.asm, lines 217 and 228)

Please, if you could take a quick look into it because those errors are haunting
me for months in every app I create. :mad:


aweX <-
Posted on 2003-01-28 03:09:35 by aweX
ok I checked my other winsock apps and I found out that the stack is
actually misaligned! It's caused by this line of code:

invoke htons, ax

Where ax contains the port (fetched via GetDlgItemInt).
Since ports are held in words, I used it that way ...
It generated those messy opcodes:

PUSH 00 (afterwards: esp=...3C)
PUSH ax (afterwards: esp=...3A)
call htons (afterwards: esp=...3E !!!!!)

After returning, the ESP reg was ending in "...2E", which is blatantly
not dword aligned.

Still, the following sendto() won't work.
I'm going to look for other misalignments now.


-- EDIT:

I was just checking shortcut.asm and stumbled across this:


coinvoke psl, IShellLink, SetIconLocation, ADDR szBuff, ax


Again, there's ax passed instead of eax... I don't have xp
here to test it, but that must be causing the problems in that app.

aweX <-
Posted on 2003-01-28 04:32:22 by aweX
Well, finally everything works on every version of Windows.

Thank you very very much (to all of you) for helping me through this.


I'll contact hutch to make some changes to the initial post of
this thread ... :grin:



aweX <-
Posted on 2003-01-28 05:07:29 by aweX
"I'll contact hutch to make some changes to the initial post of this thread" - to make it appear like you didn't flame microsoft for your own faults? ;)
Posted on 2003-01-28 06:07:00 by f0dder
It wasn't flaming ... just a (wrong) bug-report.

Remember: It's still M$'s fault to make 2k/xp err on non-dword aligned data!
It is not necessary to do so.
Posted on 2003-01-28 06:56:15 by aweX
perhaps it's necessary for reasons you don't understand? :)
I dunno. sure, it seems somewhat strang, but maybe there's reasons for it.
And it's not like it's much of a problem anyway.
Posted on 2003-01-28 07:06:46 by f0dder
What's wrong with a little invective towards Microsoft, they have broad enough shoulders to handle their own messups and enough PR to avoid most of the consequences.

If its documented its a new feature, if not its a bug and if its changed from an earlier version and not documented, a bug is still a bug. Whats the big deal, Microsoft have been doing this for years. :tongue:

Regards,

hutch@movsd.com
Posted on 2003-01-30 05:21:14 by hutch--
awex, this error


ok I checked my other winsock apps and I found out that the stack is
actually misaligned! It's caused by this line of code:

invoke htons, ax

Where ax contains the port (fetched via GetDlgItemInt).
Since ports are held in words, I used it that way ...
It generated those messy opcodes:

PUSH 00 (afterwards: esp=...3C)
PUSH ax (afterwards: esp=...3A)
call htons (afterwards: esp=...3E !!!!!)

After returning, the ESP reg was ending in "...2E", which is blatantly
not dword aligned.




is a really annoying MASM bug.

Mysteriously it will occur only if the variable of size 2 or 1 is unsigned. If you tell MASM it is signed all works well.

In this case, put a

assume AX:SWORD

before the code and the generated code is ok

Japheth
Posted on 2003-01-31 00:10:30 by japheth