flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem with CreateFile and WriteFile

Author
Thread Post new topic Reply to topic
asyode



Joined: 30 Sep 2004
Posts: 6
Location: Zielona Góra, Poland
asyode
One problem.
The code below works well. But when i try to optimize it [cause program with reserved 380kB is not a good idea;)] using loop I get error. An error occurs when I just move lines 23 and 24 before InternetReadFile invoke. Actually it's not error but program writes nothing and doesn't even close the handle (I can't delete created file). I dont know why - but I think I have to miss something Wink

Code:

format PE GUI 4.0
entry start

include 'e:\fasm\include\win32a.inc'

b equ byte
section '.code' code readable executable

start:

        invoke  InternetOpen,szAgent,0,0,0,0
        mov     [InternetHandle],eax

        invoke  InternetOpenUrl,eax,szURL,0,0,0,0
        mov     [FileHandle],eax

        invoke  InternetReadFile,[FileHandle],FileBuffer,368*1024,BytesRead
        mov     eax,[BytesRead]
        mov     b[FileBuffer+eax],0
        invoke  MessageBox,0,FileBuffer,szAgent,0

        invoke  CreateFile, sciezka, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 ;trzeci arg FILE_SHARE_READ + FILE_SHARE_WRITE
        mov     [FileOnDiskHandle],eax
        invoke  WriteFile,[FileOnDiskHandle],FileBuffer,[BytesRead],_written,0
        invoke  CloseHandle,[FileOnDiskHandle]

        invoke  CreateProcess, prog, 0, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, progStartInfo, pi
        invoke  CloseHandle,[pi.hThread]


        invoke  InternetCloseHandle,[FileHandle]
        invoke  InternetCloseHandle,[InternetHandle]
        invoke  ExitProcess,0



section '.data' data readable writeable

szAgent         db 'dler',0
szURL           db 'http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe',0
szHeader        db 'Host: http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe',0

sciezka         db 'C:\putty8.exe',0
prog            db 'C:\putty8.exe',0
progStartInfo   STARTUPINFO
pi              PROCESS_INFORMATION

InternetHandle  dd ?
FileHandle      dd ?
BytesRead       dd ?
FileBuffer      rb 368*1024;1024

FileOnDiskHandle dd ?
BytesWritten dd ?


LongIntBuff: times 11 db 0



_written dd ?


section '.idata' import data readable writeable

library kernel32,'KERNEL32.DLL',\
        wininet,'WININET.DLL',\
        user32,'USER32.DLL'

import  kernel32,\
        ExitProcess,'ExitProcess',\
        CloseHandle,'CloseHandle',\
        CreateFile,'CreateFileA',\
        WriteFile,'WriteFile',\
        GetLastError,'GetLastError',\
        CreateProcess,'CreateProcessA',\
        GetStartupInfo,'GetStartupInfoA'

import  user32,\
        MessageBox,'MessageBoxA'


import  wininet,\
        InternetOpen,'InternetOpenA',\
        InternetReadFile,'InternetReadFile',\
        InternetOpenUrl,'InternetOpenUrlA',\
        InternetCloseHandle,'InternetCloseHandle'
    
Post 30 Sep 2004, 22:28
View user's profile Send private message MSN Messenger ICQ Number Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
Why not just allocate the memory, and then free it when your done using it.
Like this:
Code:
invoke  GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,[sizeofmemneeded]
cmp     eax,NULL
je      Error
mov     [bufferhandle],eax
invoke  GlobalLock,[bufferhandle]
mov     [bufferpointer],eax

and then free it when done:

cmp     [bufferpointer],NULL
je      execute_more_code
invoke  GlobalUnlock,[bufferpointer]
invoke  GlobalFree,[bufferhandle]
    
Post 01 Oct 2004, 09:42
View user's profile Send private message Reply with quote
S.T.A.S.



Joined: 09 Jan 2004
Posts: 173
Location: Ru#27
S.T.A.S.
asyode wrote:
program with reserved 380kB is not a good idea
IMHO it's rather normal. This data take no place on HDD (FASM doesn't add zero bytes to the end of .exe file). And OS loader does its job well - actually sections of PE file are mapped to memory (by 4k blocks) only if accessed by the program.

Of cource, madmatt's solution is better in many cases (especially in big programs).
Just one small addition: LocalAlloc/GlobalAlloc are kept for compatibility with old win32s software (and AFAIK contains weird bugs).
It's better to use HeapAlloc or VirtualAlloc instead.

asyode wrote:
but I think I have to miss something Wink

Yep Wink why not post buggy code instead of one that works well? Wink
Perhaps, you didn't save some registers (eax, ecx, edx) you use in loop and they're corrupted by API call..
Post 01 Oct 2004, 11:41
View user's profile Send private message Reply with quote
asyode



Joined: 30 Sep 2004
Posts: 6
Location: Zielona Góra, Poland
asyode
S.T.A.S. wrote:
asyode wrote:
program with reserved 380kB is not a good idea
IMHO it's rather normal. This data take no place on HDD (FASM doesn't add zero bytes to the end of .exe file). And OS loader does its job well - actually sections of PE file are mapped to memory (by 4k blocks) only if accessed by the program.

Compile this source and you will get 370kB exe :]

Quote:

asyode wrote:
but I think I have to miss something Wink

Yep Wink why not post buggy code instead of one that works well? Wink
Perhaps, you didn't save some registers (eax, ecx, edx) you use in loop and they're corrupted by API call..

I don't think it's because of registers modyfing cause all needed values are stored in memory...
I said that program crashes when I move those two lines - it's not very hard to do it yourself, but ok: Here's the bad code Wink

Code:
format PE GUI 4.0
entry start

include 'e:\fasm\include\win32a.inc'

b equ byte
section '.code' code readable executable

start:

        invoke  InternetOpen,szAgent,0,0,0,0
        mov     [InternetHandle],eax

        invoke  InternetOpenUrl,eax,szURL,0,0,0,0
        mov     [FileHandle],eax

         ;these two lines are moved...
        invoke  CreateFile, sciezka, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 ;trzeci arg FILE_SHARE_READ + FILE_SHARE_WRITE
        mov     [FileOnDiskHandle],eax

        invoke  InternetReadFile,[FileHandle],FileBuffer,368*1024,BytesRead
        mov     eax,[BytesRead]
        mov     b[FileBuffer+eax],0
        invoke  MessageBox,0,FileBuffer,szAgent,0

        ;...from here
        invoke  WriteFile,[FileOnDiskHandle],FileBuffer,[BytesRead],_written,0
        invoke  CloseHandle,[FileOnDiskHandle]

        invoke  CreateProcess, prog, 0, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, progStartInfo, pi
        invoke  CloseHandle,[pi.hThread]


        invoke  InternetCloseHandle,[FileHandle]
        invoke  InternetCloseHandle,[InternetHandle]
        invoke  ExitProcess,0



section '.data' data readable writeable

szAgent         db 'dler',0
szURL           db 'http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe',0
szHeader        db 'Host: http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe',0

sciezka         db 'C:\putty8.exe',0
prog            db 'C:\putty8.exe',0
progStartInfo   STARTUPINFO
pi              PROCESS_INFORMATION

InternetHandle  dd ?
FileHandle      dd ?
BytesRead       dd ?
FileBuffer      rb 368*1024;1024

FileOnDiskHandle dd ?
BytesWritten dd ?


LongIntBuff: times 11 db 0



_written dd ?


section '.idata' import data readable writeable

library kernel32,'KERNEL32.DLL',\
        wininet,'WININET.DLL',\
        user32,'USER32.DLL'

import  kernel32,\
        ExitProcess,'ExitProcess',\
        CloseHandle,'CloseHandle',\
        CreateFile,'CreateFileA',\
        WriteFile,'WriteFile',\
        GetLastError,'GetLastError',\
        CreateProcess,'CreateProcessA',\
        GetStartupInfo,'GetStartupInfoA'

import  user32,\
        MessageBox,'MessageBoxA'


import  wininet,\
        InternetOpen,'InternetOpenA',\
        InternetReadFile,'InternetReadFile',\
        InternetOpenUrl,'InternetOpenUrlA',\
        InternetCloseHandle,'InternetCloseHandle'
    


When this will start working I'll be able to change the buffer to 4B f.e., and close it [transfering from net to file] into the loop.
Post 01 Oct 2004, 12:15
View user's profile Send private message MSN Messenger ICQ Number Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
asycode wrote:
Compile this source and you will get 370kB exe :]


declare youre data this way and you will have 2kb exe:

Code:
szAgent         db 'dler',0
szURL           db 'http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe',0
szHeader        db 'Host: http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe',0

sciezka         db 'C:\putty8.exe',0
prog            db 'C:\putty8.exe',0
LongIntBuff: times 11 db 0

progStartInfo   STARTUPINFO
pi              PROCESS_INFORMATION

InternetHandle  dd ?
FileHandle      dd ?
BytesRead       dd ?

FileOnDiskHandle dd ?
BytesWritten dd ?    


You have just to declare unitialized data after initialized variables.
Post 01 Oct 2004, 12:37
View user's profile Send private message Visit poster's website Reply with quote
asyode



Joined: 30 Sep 2004
Posts: 6
Location: Zielona Góra, Poland
asyode
Ok, ok, thanks for all responds but consider that i want to know what is wrong after moving those 2 lines. Nothing more.
I need to know that, and correct the code, before I can close it into the loop [regular use of InternetReadFile].
Post 01 Oct 2004, 13:27
View user's profile Send private message MSN Messenger ICQ Number Reply with quote
S.T.A.S.



Joined: 09 Jan 2004
Posts: 173
Location: Ru#27
S.T.A.S.
asyode, sorry for my question, but I didn't figured out which lines were with these numbers - maybe some copy/paste problems.

IMHO the problem is here:
Code:
        mov     eax,[BytesRead]
        mov     b[FileBuffer+eax],0     

You just overwrite FileOnDiskHandle value which is located at FileBuffer+368*1024 (amount of bytes read)

Try this:
Code:
FileBuffer      rb 368*1024+4; I think 1 willl be enough, but we should align next DWORD    
Post 01 Oct 2004, 14:06
View user's profile Send private message Reply with quote
asyode



Joined: 30 Sep 2004
Posts: 6
Location: Zielona Góra, Poland
asyode
S.T.A.S. wrote:

IMHO the problem is here:
Code:
        mov     eax,[BytesRead]
        mov     b[FileBuffer+eax],0     

You just overwrite FileOnDiskHandle value which is located at FileBuffer+368*1024 (amount of bytes read)

Try this:
Code:
FileBuffer      rb 368*1024+4; I think 1 willl be enough, but we should align next DWORD    


Yeah, You're right. I rewrote it and remarked this too.
First I added one byte [for the terminating zero] and seems it's enough.
Thanks for concern and help Smile
Post 01 Oct 2004, 14:23
View user's profile Send private message MSN Messenger ICQ Number 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 YouTube, Twitter.

Website powered by rwasa.