News:

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

HELP for file operation

Started by bomz, August 22, 2010, 11:41:02 AM

Previous topic - Next topic

bomz

Quote
.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
File_Read          db "QH.HLP",0
File_Write          db "QH_01.HLP",0

.data?
Handle_Read         HANDLE ?
Handle_Write         HANDLE ?
bytesRead          dd ?
bytesWrite          dd ?
buffer db 32768 dup(?)
hMemory            dd ?

.code
start:
invoke CreateFile,addr File_Read,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL OR FILE_FLAG_NO_BUFFERING,0
mov Handle_Read, eax
invoke CreateFile,addr File_Write,GENERIC_WRITE,0,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL OR FILE_FLAG_NO_BUFFERING,0
;invoke CreateFile,addr File_Write,GENERIC_WRITE,0,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0
mov Handle_Write, eax

;invoke ReadFile, Handle_Read,addr buffer,sizeof buffer,addr bytesRead,NULL
;invoke WriteFile,Handle_Write,addr buffer,bytesRead, addr bytesWrite,NULL
;cmp bytesWrite, 32768
;je Next

invoke LocalAlloc,LMEM_FIXED,33554432
mov  hMemory,eax
Next:
invoke ReadFile,Handle_Read,hMemory,33554432,addr bytesRead,NULL
invoke WriteFile,Handle_Write,hMemory,bytesRead, addr bytesWrite,NULL
cmp bytesWrite, 33554432
je Next
invoke LocalFree,hMemory

invoke CloseHandle, Handle_Read
invoke CloseHandle, Handle_Write

exit:
invoke ExitProcess,0
end start

This simple programe can copy any files except HLP. You may change name of HLP file to RAR or IMA... - not copy. You may change name of any not HLP file to HLP it's copy good.
Whats the reason? Try it. --- link removed --- this is the help of MASM 6.11, I try some other to
If copy using simple buffer - all is OK, if no write cache - all is OK, you may copy any file including HLP. I leave this strings for trying

-- image removed ---

PS A little progress - it's not copy COM files to. But not EXE
PPS and BAT files

Twister

Link & image has been removed? Why? :eek

Well, the thing I can see is that you are misusing 'ADDR'.

Wouldn't it be best to use the CopyFile Function? http://msdn.microsoft.com/en-us/library/aa363851%28VS.85%29.aspx

bomz

if I miss ADDR - any files not copied. This construction copies files two time quickly and more safely for hard disk. Prevent "cache writing" gets 3 second on each Gygabyte

tenkey

The code, as posted, is able to read 32 MB into a 32 KB buffer.
Check if file sizes make a difference.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

bomz

How, in your opinion, puting 32 mb in 32 kb buffers affects reading and writing of COM files?
There is no errors in this code, at least simple.

Lack of writing cache (FILE_FLAG_NO_BUFFERING as FILE_FLAG_WRITE_THROUGH) makes impossible for some files type (HLP COM BAT but not IMA ISO AVI RAR ZIP) to move FilePointer (invoke SetFilePointer) accept the case when you read to buffer in the same memory block which system allocate for code.

donkey

Quote from: tenkey on August 22, 2010, 05:09:31 PM
The code, as posted, is able to read 32 MB into a 32 KB buffer.
Check if file sizes make a difference.

Not sure where you see that, he uses a global buffer for the 32mb read/write and has commented out the 32 KB read/write.

Anyway,

Are there any error messages after CreateFile or Read/Write file ? Perhaps there is an issue with security software blocking the creation of the .exe file then the handle would be invalid and should throw an error. And yes I know that .com files are executable but it is still a good idea to check the error codes after your major API calls. For example you have not even checked to see if the files were created before reading and writing, you should at least check the return for a valid handle.

Edgar
"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

bomz

It's only part of my code that I make to highlight the problem. my "global" code treat all possible error events. now I may shurely say that filepointer not move in the case of HLP BAT COM files. I can't create ZERO FILE of the same file size in the case of HLP BAT COM files. but IMA ISO AVI RAR ZIP filepointer move OK

donkey

Quote from: bomz on August 23, 2010, 02:52:09 AM
It's only part of my code that I make to highlight the problem. my "global" code treat all possible error events.

So are there any error codes returned directly after CreateFile ?

Is the file created and left empty ? Is there no file created ? You have not said what the problem is just left it to us to guess and when the code looks fine that a pretty hard thing to do. Also if your error checking is inline with the posted code it may be affecting the execution path of the program, if it is not inline then how can you tell where the failure is. Lots of things here that need to be addressed before you'll get a useful answer.
"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

bomz

#8


The global question - how to prevent cache writing it gives 3-5% speed of copying
Intuitively it's possible to note that HLP COM BAT files are "the systems files", but how correctly use prevent cache writing flags?

donkey

You need to work out your error trapping a bit more, the WriteFile operation was returning the error "Error 87 > The parameter is incorrect.", you can solve this by removing FILE_FLAG_NO_BUFFERING from the output CreateFile. Also FILE_ATTRIBUTE_NORMAL is not really necessary.

invoke CreateFile,addr File_Write,GENERIC_WRITE,0,0,CREATE_NEW,0,0

Before using the FILE_FLAG_NO_BUFFERING flag you should have read this:

http://msdn.microsoft.com/en-us/library/cc644950%28v=VS.85%29.aspx

Edgar
"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

bomz

FILE_ATTRIBUTE_NORMAL - I try all possible flags including NULL
before WriteFile function in full code SetFilePointer create full size ZERO file

AVI files copy good up too 3 gygabyte size - I haven't biger for trying. HASH summe correct I use torrent rehash

donkey

Quote from: bomz on August 23, 2010, 03:37:17 AM
FILE_ATTRIBUTE_NORMAL - I try all possible flags including NULL
before WriteFile function in full code SetFilePointer create full size ZERO file

AVI files copy good up too 3 gygabyte size - I haven't biger for trying. HASH summe correct I use torrent rehash

I tried your code as is and got the same results as you, I removed the FILE_FLAG_NO_BUFFERING flag and every file I tested copied OK, the data in the destination file was complete and faithful to the original.

So is your problem solved ?

Edgar
"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

bomz

files atribute the same in all cases, of course
using overlapped structure for handly set FilePointer not decide the problem

bomz

NOT SOLVED - how to prevent writing cache? Or how to use prevent buffering flags for writefile

buffer db 1048576 dup(?) - compiling time more than 15 minutes it's impossible to wait bigger buffer

donkey

Quote from: bomz on August 23, 2010, 03:48:55 AM
NOT SOLVED - how to prevent writing cache? Or how to use prevent buffering flags for writefile

I posted a link to the article at MSDN that explains how to use the flag. From how I read it you must calculate your buffer size as a multiple of the sector size of the drive you are writing to, first get the sector size then use a multiple of that as your buffer size.
"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