flat assembler
Message board for the users of flat assembler.

Index > Windows > CreateFile problem

Author
Thread Post new topic Reply to topic
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf
I want open a file, but I have strange problem. When i set in second parameter(in createFile) on GENERIC_READ program crashes( there appears windowsXP error window), but if it`s set on GENERIC_WRITE or 0 program works proparetly( except that ReadFile can't read Twisted Evil ). Does somebody know what causes this error?

Atachment:
Code:
invoke CreateFile, sfname, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0    
Post 27 Mar 2006, 14:15
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
post your binary so we can see if it crashes on other computers. because probably the bug is on your's side.
Post 27 Mar 2006, 14:36
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf
Code:
format PE GUI 4.0
entry start

include 'C:\Program Files\FASM\INCLUDE\win32a.inc'

section '.data' data readable writeable

hsFile  dd 0
haSpace dd 0
asSize dd 0

sfname db 'game.esp',0

etFC db 'Otwarcie pliku game.esp nie powiodBo si.',0
etMA db 'Alokacja pamici nie powiodBa si.',0

section '.code' code readable executable

start:
   invoke CreateFile, sfname, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
   cmp    eax, INVALID_HANDLE_VALUE
   je     err_fc
   mov    [hsFile], eax

   invoke GetFileSize, [hsFile], 0
   cmp    eax, 0ffffffffh
   je     err_fc
   mov    [asSize], eax

   invoke GlobalAlloc, GMEM_FIXED, [asSize]
   test   eax, eax
   jz     err_ma
   mov    [haSpace], eax

   invoke ReadFile, [hsFile], [haSpace], [asSize], 0, 0
   test   eax, eax
   jz     err_fc

   jmp fin

err_fc:
   invoke MessageBox, 0, etFC, etFC, MB_OK
   jmp    fin

err_ma:
   invoke MessageBox, 0 etMA, etMA, MB_OK
fin:
   invoke GlobalFree, [hsFile]
   invoke ExitProcess, 0

section '.idata' import data readable writeable

library k32, 'KERNEL32.DLL',\
        u32, 'USER32.DLL'

import k32,\
       ExitProcess,'ExitProcess',\
       GetModuleHandle, 'GetModuleHandleA',\
       GetModuleFileName, 'GetModuleFileNameA',\
       CreateFile, 'CreateFileA',\
       GetFileSize, 'GetFileSize',\
       GlobalAlloc, 'GlobalAlloc',\
       ReadFile, 'ReadFile',\
       GlobalFree, 'GlobalFree'

import u32,\
       MessageBox, 'MessageBoxA'    

Sorry, now i found that READFILE makes problem, but i need help anyway Razz
Post 27 Mar 2006, 15:24
View user's profile Send private message Reply with quote
lilljocke



Joined: 28 Dec 2004
Posts: 34
Location: Sweden
lilljocke
When i coding something with CreateFile i sometimes must Write FILE_SHARE_READ OR FILE_SHARE_WRITE. Mabye this will help.



etFC db 'Otwarcie pliku game.esp nie powiodBo si.',0
etMA db 'Alokacja pamici nie powiodBa si.',0

section '.code' code readable executable

start:
invoke CreateFile, sfname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
cmp eax, INVALID_HANDLE_VALUE
je err_fc
mov [hsFile], eax

invoke GetFileSize, [hsFile], 0
cmp eax, 0ffffffffh
je err_fc
mov [asSize], eax

invoke GlobalAlloc, GMEM_FIXED, [asSize]
test eax, eax
jz err_ma
mov [haSpace], eax

invoke ReadFile, [hsFile], [haSpace], [asSize], 0, 0
test eax, eax
jz err_fc

jmp fin

err_fc:
invoke MessageBox, 0, etFC, etFC, MB_OK
jmp fin

err_ma:
invoke MessageBox, 0 etMA, etMA, MB_OK
fin:
invoke GlobalFree, [hsFile]
invoke ExitProcess, 0

section '.idata' import data readable writeable

library k32, 'KERNEL32.DLL',\
u32, 'USER32.DLL'

import k32,\
ExitProcess,'ExitProcess',\
GetModuleHandle, 'GetModuleHandleA',\
GetModuleFileName, 'GetModuleFileNameA',\
CreateFile, 'CreateFileA',\
GetFileSize, 'GetFileSize',\
GlobalAlloc, 'GlobalAlloc',\
ReadFile, 'ReadFile',\
GlobalFree, 'GlobalFree'

import u32,\
MessageBox, 'MessageBoxA'
Post 27 Mar 2006, 16:18
View user's profile Send private message Visit poster's website Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf
no. That is not that what causes error :/
Post 27 Mar 2006, 16:26
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
two problems:

A) from MSDN If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.

you are passing lpOverlapped as NULL aswell as lpNumberOfBytesRead

B) you are using GlobalFree on your file handle, you should be using GlobalFree on your pointer to allocated memory and CloseHandle on your file handle

working version:

Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

section '.data' data readable writeable

hsFile      dd 0
haSpace     dd 0
asSize      dd 0
asBytesRead dd 0

sfname db 'ok.txt',0

etFC db 'Otwarcie pliku game.esp nie powiodBo si.',0
etMA db 'Alokacja pamici nie powiodBa si.',0

section '.code' code readable executable

start:
   invoke CreateFile, sfname, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
   cmp    eax, INVALID_HANDLE_VALUE
   je     err_fc
   mov    [hsFile], eax

   invoke GetFileSize, [hsFile], 0
   cmp    eax, 0ffffffffh
   je     err_fc
   mov    [asSize], eax

   invoke GlobalAlloc, GMEM_FIXED, [asSize]
   test   eax, eax
   jz     err_ma
   mov    [haSpace], eax

   invoke ReadFile, [hsFile], [haSpace], [asSize], asBytesRead, 0 ;needs lpNumberOfBytesRead
   test   eax, eax
   jz     err_fc

   ;invoke MessageBox, 0, [haSpace], 0, 0 ;show contents of file

   jmp fin

err_fc:
   invoke MessageBox, 0, etFC, etFC, MB_OK
   jmp    fin

err_ma:
   invoke MessageBox, 0 etMA, etMA, MB_OK
fin:
   invoke CloseHandle, [hsFile] ;close file handle (no memory leak)
   invoke GlobalFree, [haSpace] ;free memory

   invoke ExitProcess, 0

section '.idata' import data readable writeable

library k32, 'KERNEL32.DLL',\
        u32, 'USER32.DLL'

import k32,\
       ExitProcess,'ExitProcess',\
       GetModuleHandle, 'GetModuleHandleA',\
       GetModuleFileName, 'GetModuleFileNameA',\
       CreateFile, 'CreateFileA',\
       GetFileSize, 'GetFileSize',\
       GlobalAlloc, 'GlobalAlloc',\
       ReadFile, 'ReadFile',\
       GlobalFree, 'GlobalFree',\
       CloseHandle, 'CloseHandle'

import u32,\
       MessageBox, 'MessageBoxA'
    

_________________
redghost.ca
Post 27 Mar 2006, 23:04
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Barf



Joined: 17 Sep 2004
Posts: 34
Location: Poland
Barf
thanx. Now works ok
Post 28 Mar 2006, 12:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.