Could anybody tell me why isn't this working, crashing all the time when I send the EM_STREAMIN message?




; It uses Nasm and the sc macro, nothing else
; The function receives the Rich Edit control handle in eax, the filename in [esp+8] and the stream_in flags in [esp+4]
; I have omitted the Externs and EQUs, they are correct, the function is GLOBAL (the callback too)

StreamFileIn:
mov dword [hwn], eax
mov eax, [esp+8]
sc CreateFileA,eax,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
cmp dword eax, INVALID_HANDLE_VALUE
je .end
mov edx, [esp+4]
mov dword [flags], edx
mov dword [edstr.dwCookie], eax ; edstr is an instance of the STREAMIN structure, three dwords size
mov dword [edstr.dwError], 0
mov dword [edstr.pfnCallback], EditStreamCallback
sc SendMessageA,[hwn],EM_STREAMIN,[flags],edstr
sc SendMessageA,[hwn],EM_SETMODIFY,0,0
sc CloseHandle,[edstr.dwCookie]
mov dword edx, [edstr.dwError]
cmp dword edx, 0
je .end
xor eax, eax
.end:
ret 8

EditStreamCallback:
pop dword [edstr.dwCookie]
pop dword [pbBuff]
pop dword [cb]
pop dword [pcb]
sc ReadFile,[edstr.dwCookie],[pbBuff],[cb],[pcb],0
xor dword eax, 1
ret

[section .bss]

flags resd 1
edstr:
.dwCookie: resd 1
.dwError: resd 1
.pfnCallback: resd 1



I haven't been able to make it work. The control has been created, and could even change text with SetWindowText, or the font. EM_STERAMIN never worked (nor EM_STREAMOUT). Any help would be welcome.

Thank you,

Wavemaker
Posted on 2001-11-14 07:44:52 by Wavemaker
(sorry if I tell you things that you alrady know,
but the nasm syntax looks not familar to me)

one odd thing is the missing "offset" in

mov dword , EditStreamCallback

the other thing is the "pop" of the parameters in the callback,
there you pop at first the dwCookie, but at first the return
address should be on the stack, so I would write:

EditStreamCallback:
pop dword lpReturn
pop dword
pop dword
pop dword
pop dword
sc ReadFile,,,,,0
xor dword eax, 1

jmp lpReturn
Posted on 2001-11-14 08:09:48 by beaster
The missing "offset" is not actually missing - it's a nice feature of
nasm. Like requiring for memory access.

But it indeed looks like he's popping the return address off the stack,
which is bad :).

Wavemaker, you should get yourself a debugger. OllyDbg, for instance.
They can be really helpful in tracing bugs ;)
Posted on 2001-11-14 08:21:45 by f0dder
THANKS, THANKS A LOT! I've been fighting with this for weeks, didn't know that callback functions were not called, but 'jumped'! BTW, is it the same for hook functions, like when you hit the "Apply" button on a Font Dialog Box?

Anyway, thanks
Posted on 2001-11-14 14:26:03 by Wavemaker