flat assembler
Message board for the users of flat assembler.
Index
> Windows > Problem with CreateFile and WriteFile |
Author |
|
madmatt 01 Oct 2004, 09:42
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] |
|||
01 Oct 2004, 09:42 |
|
S.T.A.S. 01 Oct 2004, 11:41
asyode wrote: program with reserved 380kB is not a good idea 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 why not post buggy code instead of one that works well? Perhaps, you didn't save some registers (eax, ecx, edx) you use in loop and they're corrupted by API call.. |
|||
01 Oct 2004, 11:41 |
|
asyode 01 Oct 2004, 12:15
S.T.A.S. wrote:
Compile this source and you will get 370kB exe :] Quote:
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 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. |
|||
01 Oct 2004, 12:15 |
|
decard 01 Oct 2004, 12:37
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. |
|||
01 Oct 2004, 12:37 |
|
asyode 01 Oct 2004, 13:27
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]. |
|||
01 Oct 2004, 13:27 |
|
S.T.A.S. 01 Oct 2004, 14:06
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 |
|||
01 Oct 2004, 14:06 |
|
asyode 01 Oct 2004, 14:23
S.T.A.S. wrote:
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 |
|||
01 Oct 2004, 14:23 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.