News:

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

SendMessage EM_SETSEL ACCESS DENIED!!!

Started by paulfaz, November 13, 2006, 03:50:58 PM

Previous topic - Next topic

paulfaz

Hi All,

Ive got what appears to be a strange problem, ive just started out trying to learn ASM and im getting serious headaches already... Firstly, ive got some code that reads a file using the readfile / createfile apis, in chunks, then sends a EM_SETSEL and an EM_REPLACESEL to the editbox which should update the editbox with my new chunk of data.

It appears to half work.... for some reason, the whole file is never in my editbox, only some of it, after "attempting" to debug it, using a debugger i didnt really get to far so i added some code to debug it, i used an example of getlasterror, and it keeps throwing me "access denied" errors after each

PUSH 0
PUSH -1
PUSH EM_SETSEL
PUSH hWndCTL_FC
CALL SendMessage


and i have absolutely no idea why? any help would be much appreciated.......  I also tried using ...

PUSH text_len
PUSH text_len
PUSH EM_SETSEL
PUSH hWndCTL_FC
CALL SendMessage


where text_len was populated with the value of the text length in the edit box using ...

PUSH hWndCTL_FC ;Handle to edit box
CALL GetWindowTextLength
MOV text_len, EAX



Below is the majority of the code, ive removed the window creation "be gentle im still learning" etc...



                                                                                PUSH 0
PUSH FILE_ATTRIBUTE_ARCHIVE
PUSH OPEN_EXISTING
PUSH 0
PUSH FILE_SHARE_READ
PUSH GENERIC_READ
LEA EAX, b_FileBuf
PUSH EAX
CALL CreateFile
MOV hFile, EAX ;FileHandle

PUSH CTL_FILECONTENT
PUSH hDlg
CALL GetDlgItem
MOV hWndCTL_FC, EAX ;Get Handle to Control

FileReadLoop:
PUSH 0
LEA EAX, bytes_read
PUSH EAX
PUSH BUFFER_SIZE
LEA EAX, content_buffer
PUSH EAX
PUSH hFile
CALL ReadFile

CMP [bytes_read], 0
JE EndLoop

PUSH hWndCTL_FC
CALL GetWindowTextLength
MOV text_len, EAX

PUSH text_len
PUSH text_len
PUSH EM_SETSEL
PUSH hWndCTL_FC
CALL SendMessage

;;LEA EAX, errortest
;PUSH EAX
;CALL HandleError

LEA EAX, content_buffer
PUSH EAX
PUSH 0
PUSH EM_REPLACESEL
PUSH hWndCTL_FC
CALL SendMessage



PUSH BUFFER_SIZE
LEA EAX, content_buffer
PUSH EAX
CALL RtlZeroMemory

JMP FileReadLoop
EndLoop:

PUSH hFile
CALL CloseHandle





TNick

This may be the problem:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceui40/html/cerefem_limittext.asp
QuoteWhen you create an edit control, the amount of text a user can enter in the control is initially limited to 30,000 characters. Use EM_LIMITTEXT to change this limit.

Regards,
Nick

paulfaz

Hi, thanks for the response..... Im not sure that is the issue, ive stripped it down to a console app now, and it does similar things producing access denied errors... im creating a new forum topic for it, cos ive completely changed the app now down to rare bones...

Thanks for the input..

Tedd

Post your app (or the bones version) -- it's easier than guessing :wink
No snowflake in an avalanche feels responsible.

paulfaz

Hi Ted,

Ok below is all my source, ive removed all the GUI stuff and gone for a console app for the time being so i can bare bone it... Ive ran it through Ollydbg, but my limited debugging knowledge doesnt help.... Olly exits with an exit code "C0000005" which apparently is an access violation.  Im basically trying to read a file in, in chunks and then display it to the screen, when i run it without printing the output to the screen it "appears" to work, but there no output so i cant tell, when i use the writeconsole method it crashes, ive also tried the printf method, and up above with a gui updating the edit box...

Ive read some other stuff that lead me to believe i might be producing some kind of overflow??? or that windows has moved my pointer so the pointer to bytes read or the buffer isnt right anymore?? if that makes any sense...

Because im reading the file in a chunk and then displaying it to the screen, i can see that its looping through at least 10 loops before it crashes.... the size of the file im trying is 4mb.. i plan to do much bigger files which is why i havent loaded the whole file into a buffer...



.386
.model flat, stdcall
option casemap :none

include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\user32.inc
include c:\asmdev\win32\msvcrt.inc
include c:\asmdev\win32\handleer.asm

include c:\masm32\include\comdlg32.inc
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\msvcrt.lib


.const
;BUFFER_SIZE equ 52768
BUFFER_SIZE equ 768
.data

fn_Name db "c:\disp1.txt",0
str_ErrorTitle db "Error",0
fn_BytesRead dd 0
.data?
hnd_File HANDLE ?
hnd_ConsoleOut HANDLE ?
buf_File db BUFFER_SIZE dup(?)
lpcchWritten   DWORD ?


.code

start:
PUSH STD_OUTPUT_HANDLE
CALL GetStdHandle
MOV hnd_ConsoleOut, EAX

PUSH 0
PUSH FILE_ATTRIBUTE_ARCHIVE
PUSH OPEN_EXISTING
PUSH 0
PUSH FILE_SHARE_READ
PUSH GENERIC_READ
LEA EAX, fn_Name
PUSH EAX
CALL CreateFile
MOV hnd_File, EAX

FileReadLoop:
PUSH 0
LEA EAX, fn_BytesRead
PUSH EAX
PUSH BUFFER_SIZE-1
LEA EAX, buf_File
PUSH EAX
PUSH hnd_File
CALL ReadFile

CMP [fn_BytesRead], 0
JE FileReadEnd

LEA EAX, lpcchWritten
PUSH EAX
PUSH SIZEOF buf_File
LEA EAX, buf_File
PUSH EAX
PUSH hnd_ConsoleOut
CALL WriteConsole

;LEA EAX, str_ErrorTitle
;PUSH EAX
;CALL HandleError

PUSH BUFFER_SIZE
LEA EAX, buf_File
PUSH EAX
CALL RtlZeroMemory
JMP FileReadLoop

FileReadEnd:

PUSH BUFFER_SIZE
LEA EAX, buf_File
PUSH EAX
CALL RtlZeroMemory

PUSH hnd_File
CALL CloseHandle

PUSH 0
CALL ExitProcess

END start

Tedd

One simple error -- you didn't push the correct number of paramters for the call to WriteConsole, as a result the stack contents are incorrect from the point on.... la la la... cR@$h!
If you use "invoke" instead of pushing the parameters yourself then you get this checking for free and it will complain if you make that mistake.

Also, I changed the 'nNumberOfCharsToWrite' parameter to WriteConsole to be the number of characters ACTUALLY read from the file, as opposed to the size of the whole buffer (which it may not have read.)


.code
start:
    invoke GetStdHandle, STD_OUTPUT_HANDLE
    mov hnd_ConsoleOut,eax

    invoke CreateFile, ADDR fn_Name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
    mov hnd_File,eax

  @@:
    invoke ReadFile, hnd_File,ADDR buf_File,BUFFER_SIZE-1,ADDR fn_BytesRead,0
    cmp [fn_BytesRead],0
    je @finished

;***HERE***
    ;;invoke WriteConsole, hnd_ConsoleOut,ADDR buf_File,SIZEOF buf_File,ADDR lpcchWritten,**MISSING_PARAMETER**
    invoke WriteConsole, hnd_ConsoleOut,ADDR buf_File,fn_BytesRead,ADDR lpcchWritten,0
   
    ;invoke HandleError, ADDR str_ErrorTitle

    ;invoke RtlZeroMemory, ADDR buf_File,BUFFER_SIZE    ;--pointless
    jmp @B

  @finished:
    ;invoke RtlZeroMemory, ADDR buf_File,BUFFER_SIZE    ;--pointless

    invoke CloseHandle, hnd_File

    invoke ExitProcess, 0
end start

No snowflake in an avalanche feels responsible.

paulfaz

Oh my Tedd & TNick, your both the strongest Headache Tablets availble... Many thanks for your help, Tedd, ill be using the invoke method from now on, i dont know why i just prefer pushing and popping things, it makes me feel like a real ASM programmer for some reason.. When i use Invoke i feel like im writing C/C++ :( <weirdo..

TNick, you were right aswell, it was a limit on the edit box, ive put a line in now which sets the limit each time it sends a message by determining the size of the text already in there and then adding the size of the buffer to it...

It works a treat... Thanks... ILL be back!!

hutch--

Paul,

You will get over it, invoke is handy for high level API code but you always have the option of push/call syntax if it affords you an advantage.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

donkey

Quote from: paulfaz on November 14, 2006, 04:12:05 PM
Oh my Tedd & TNick, your both the strongest Headache Tablets availble... Many thanks for your help, Tedd, ill be using the invoke method from now on, i dont know why i just prefer pushing and popping things, it makes me feel like a real ASM programmer for some reason.. When i use Invoke i feel like im writing C/C++ :( <weirdo..

TNick, you were right aswell, it was a limit on the edit box, ive put a line in now which sets the limit each time it sends a message by determining the size of the text already in there and then adding the size of the buffer to it...

It works a treat... Thanks... ILL be back!!

Well, outside of the invoke debate which is a silly debate, everyone has their coding preferences and invoke doesn't make it more or less assembly language. Why are you bothering to LEA every address in the data segment ? You can simply push the offset and save a superfluous instruction. LEA is rarely necessary to calculate an address, even when it is a LOCAL.

PUSH 0
LEA EAX, fn_BytesRead
PUSH EAX
PUSH BUFFER_SIZE-1
LEA EAX, buf_File
PUSH EAX
PUSH hnd_File
CALL ReadFile


Should look like this...

PUSH 0
PUSH offset fn_BytesRead
PUSH BUFFER_SIZE-1
PUSH offset buf_File
PUSH hnd_File
CALL ReadFile
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

paulfaz

Im not sure mate, lol, i think a lot of it is because i started learning 16bit, where you have to LEA everything...or at least to my knowledge you do, so im just used to doing that...