News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Help me programming

Started by jayanthd, March 03, 2013, 02:13:37 AM

Previous topic - Next topic

jayanthd

Hello!

I am trying my hands at Windows assembly programming using MASM32. My first program which was a Hello World! program was successful. My second program is reading a file and displaying the first 10 characters of the file contents in a message box. I want to use all the windows api function one by one and learn. I don't know whether the file is reading or not. The code compiles successfully but when I run it gives a Hello! World message and on clicking the OK button it displays the second message box with the right title but it has to display the first 10 characters of the file as message which it is not doing so. Please tell me what am I doing wrong.  :(

fileread.zip
extract the files to c:\masm32
Here is my code

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data
HelloWorld db "Hello World!", 0
FileData db "C:\MASM32\text.dat", 0
msgTitle db "File Read Example", 0
.data?
filebuff db ?
.code
start:
invoke MessageBox, NULL, addr HelloWorld, addr HelloWorld, MB_OK
invoke ReadFile, GENERIC_READ, addr filebuff, 10, addr FileData, 0
invoke MessageBox, NULL, addr filebuff, addr msgTitle, MB_OK
invoke ExitProcess, 0
end start

;BOOL ReadFile(

    ;HANDLE hFile, // handle of file to read
    ;LPVOID lpBuffer, // address of buffer that receives data 
    ;DWORD nNumberOfBytesToRead, // number of bytes to read
    ;LPDWORD lpNumberOfBytesRead, // address of number of bytes read
    ;LPOVERLAPPED lpOverlapped // address of structure for data
   ;);

dedndave

you have to open the file to get a handle, first
use the CreateFile function...
        INVOKE  CreateFile,offset szFileName,GENERIC_READ,FILE_SHARE_READ,
                NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
        or      eax,eax
        mov     hFile,eax
        jz      OpenError


szFileName is the null-terminaed filename string
it will return a value in EAX
if it is non-zero, it is the file handle - you may use it to read the file
if it is zero, there was an error, you may use GetLastError to get the error code

if the file handle is non-zero, you should use CloseHandle when done to close it

jayanthd

#2
It's not working...

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data
HelloWorld db "Hello World!", 0
FileData db "C:\MASM32\text.dat"
msgTitle db "File Read Example", 0
noofbytes db 10
szFileName db "text.dat", 0

.data?
filebuff db ?
hFile dd ?
.code
start:
invoke MessageBox, NULL, addr HelloWorld, addr HelloWorld, MB_OK
INVOKE  CreateFile,addr szFileName,GENERIC_READ,FILE_SHARE_READ,
                NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
        or      eax,eax
        mov     hFile,eax
       
invoke ReadFile, hFile, addr filebuff, 43, addr FileData, 0
invoke MessageBox, NULL, addr filebuff, addr msgTitle, MB_OK
invoke ExitProcess, 0
end start

;BOOL ReadFile(

    ;HANDLE hFile,   // handle of file to read
    ;LPVOID lpBuffer,   // address of buffer that receives data 
    ;DWORD nNumberOfBytesToRead,   // number of bytes to read
    ;LPDWORD lpNumberOfBytesRead,   // address of number of bytes read
    ;LPOVERLAPPED lpOverlapped    // address of structure for data
  ;


Whenever I compile a code using masm32 my AVG IS 2013 tells that there is trojan in the created file...


It's working dave   :biggrin:


I modified the code a little and now it shows better message...  ;)



Should I include invoke CloseHandle, hFile after invoke ReadFile?

dedndave

#3
ok - you have a few issues

first, the lpNumberOfBytesRead parameter for Readfile is a pointer to a DWORD variable
this variable receives the number of bytes actually read
no need to initialize it
you are currently pointing that parameter to the FileData variable, so it overwrites the file name string
it doesn't matter, in this case, because you no longer need the string
however, i don't think that's what you really want

second, your buffer is one byte in size
you are reading 43 bytes
so, the read operation will overwrite any data that follows the buffer
in this case, that includes the hFile variable   :P

finally - a few minor points
try to get used to using "Hungarian Notation" for variable names (google that term)
it isn't necessary, but it will help you to read MSDN documents, as well as a lot of example code
and - the masm32rt.inc file will take care of the includes for you

oh - and you forgot to terminate FileData with a null   :biggrin:
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

        .DATA

szFileName     db "C:\MASM32\text.dat", 0
szHelloWorld   db "Hello World!", 0
szMsgTitle     db "File Read Example", 0
szFileError    db "Error Reading File", 0

;***********************************************************************************************

        .DATA?

hFile          dd ?
nNumberOfBytes dd ?
baFileBuffer   db 44 dup(?)

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  MessageBox, NULL, offset szHelloWorld, offset szMsgTitle, MB_OK
        INVOKE  CreateFile, offset szFileName, GENERIC_READ, FILE_SHARE_READ,
                NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
        mov     hFile, eax
        mov     edx, offset szFileError
        cmp     eax, INVALID_HANDLE_VALUE
        jz      ShwMsg

        INVOKE  ReadFile, hFile, offset baFileBuffer, 43, offset nNumberOfBytes, NULL
        mov     edx, offset szFileError
        or      eax, eax
        jz      ShwMsg

        mov     edx, offset baFileBuffer

ShwMsg: INVOKE  MessageBox, NULL, edx, offset szMsgTitle, MB_OK
        INVOKE  CloseHandle, hFile
        INVOKE  ExitProcess, 0

_main   ENDP

;###############################################################################################

        END     _main

sinsi

CreateFile returns INVALID_HANDLE_VALUE on error (-1), not zero.

dedndave

oops - i forgot   :P

fixed the post above

thanks sinsi   :t

jayanthd

#6
Thanks Dave and sinsi.


Now I want to do a new project which will be a text editor like notepad. What all apis should I use to write a masm32 assembly code for a simple text editor. It should be able to open, close, create new files. I should have all the menus that windows notepad has. Just show me an example code for creating a menu item and also a seperator in menus and
a richtext box. I also need to open the font and color dialog boxes.  :biggrin: 

jj2007

Have a look at the \Masm32\examples folder. If you spend the whole Sunday reading, you will know everything you need, and even more.

Then, when you are ready with reading, and still not satisfied, have a look at TinyIDE.

dedndave

another one is Iczelion's Tutorials
as i recall, at the end of the tut's, you have a little editor   :P