I'm getting remnants of old characters in my files.

stray letters here and there that should not be there.

Is this common when MemoryMapping isn't used?
Posted on 2003-03-18 05:31:41 by Homer
Afternoon, Homer.

Could you show your code for using Writefile? (initialization of varibles/etc as well).

I've *never* had a problem using WriteFile.

How are you using it? Is the buffer you're writing filled correctly? (i.e. you zero out the buffer before filling it with data again?). Maybe you're writing the "data length + one" by mistake?

Posted on 2003-03-18 06:00:01 by Scronty

mov [buffer], 0
mov [ImageCounter],0
mov [ImageCount],0
mov [TotalChars],0
mov [FilePointer], 20
mov [hFile],0
mov eax,0
mov ofn.lStructSize,sizeof ofn
mov ofn.lpstrFilter,offset ofnFilter
mov ofn.lpstrFile,offset buffer
mov ofn.nMaxFile,sizeof buffer
mov ofn.lpstrTitle,offset ofnTitle
invoke GetOpenFileName, addr ofn

;invoke MessageBox,NULL, ADDR FileInUse,ADDR AppName,MB_OK
invoke BitmapFromFile, addr buffer
mov hBitmap,eax
invoke InvalidateRect,hWnd,NULL,TRUE

mov hFile,eax
invoke SetFilePointer, hFile,0,NULL,FILE_BEGIN
invoke ReadFile,hFile, addr ImageCount, 4, OFFSET NumberOfBytesRead,NULL
invoke CloseHandle, hFile

;Then I perform some math on a string taken from the file
invoke atodw, addr ImageCount
mov TotalChars, eax
mov ImageCounter, eax
inc ImageCounter
mov eax, 128
mul TotalChars
add FilePointer, eax

mov [ImageCount],0
invoke dwtoa,ImageCounter, addr ImageCount
;counter updated ready to be re-writen (does so just fine)
mov [hFile],0
mov hFile, eax

invoke SetFilePointer,hFile,0,NULL,FILE_BEGIN
invoke WriteFile,hFile,addr ImageCount,4,OFFSET NumberOfBytesRead,NULL

;check my buffer
invoke MessageBox,NULL,addr buffer, addr AppName,MB_OK

invoke SetFilePointer, hFile,FilePointer, 0,FILE_BEGIN
invoke WriteFile,hFile, ADDR buffer,128,OFFSET NumberOfBytesRead,NULL
invoke CloseHandle,hFile

The problem lies in the string that the buffer writes.
I've checked the buffer as you can see and it appears as it should.
When i look at the file the buffer writes correctly, only there are stray letters after the string.

I've attached one such file it so you can have a better idea.
Posted on 2003-03-18 06:10:01 by Homer
I don't think there is a need to mov ,0
All I can say is that you code is quite sloppy.
Is ImageCount an array of bytes? If so there is no need for mov ,0

Interesting.... can show wat is in buffer?
I have this feeling that filepointer is causing the problem.
Posted on 2003-03-18 06:26:15 by roticv
Thanks for the critique.

I know theres no need to zero it, but I'm trying all the tricks up my short sleeve.

So... my code is sloppy?

say something usefull!


i display the contents of the buffer with a MessageBox. The problem isn't with the buffer. is it?

I'll see what I can do with FilePointer
I had the same feeling, but just don't see the problem
Posted on 2003-03-18 06:32:50 by Homer
Send me the code.... I will help you to debug... roticv87@hotmail.com
Posted on 2003-03-18 06:50:31 by roticv
Thanks, I appreciate the offer.
But I don't feel comfortable sending my code out.

Thanks anyway... You're very kind.:)

I'm going to mess around with the buffer

the buffer MUST be an array no?

because it is: buffer db 512 dup (?)

i noticed that when i mov , 0

it only clears one character.

Im thinking I must set up a loop 512 times?
Posted on 2003-03-18 07:09:33 by Homer
Use the api RtlZeroMemory, save your troubles... :grin:


lea esi,buffer
mov ecx,128
mov DWORD PTR [esi],0
add esi,4
loop loop1
Posted on 2003-03-18 07:10:43 by roticv
Wow, that works!:cool:

mov DWORD PTR ,0

what's that mean though?
Posted on 2003-03-18 07:20:31 by Homer
esi is a pointer. The DWORD ptr calls the cpu to assume that the data which esi is pointing to is DWORD. For this code I used Dword ptr instead of byte pointer as buffer aligns to dword and thus less loops is needed.
Posted on 2003-03-18 07:23:09 by roticv
I see, It works well. I just need to tweak out some side effects.
thanks a bunch
Posted on 2003-03-18 07:31:09 by Homer
Well, well, go and clean your code right now.
Or the CPU ( Code Patrol Unit ) will interupt your instruction pointer to your activities.


Enjoy your work, P1

PS: What you working on?
Posted on 2003-03-18 09:48:32 by Pone
I don't know where to start :tongue:

That loop roticv contributed keeps my images from being displayed!
Are those CPU things messing with me?


I'm working on something...
It's original so you can reverse engineer it when I'm done with it, or just make a better one:(

Really it's just something for my friends...
but I imagine everyone will want it :grin:
Posted on 2003-03-18 09:59:37 by Homer
Just an attempt at some humor, to encourage you to keep coding along.

Regards, P1
Posted on 2003-03-18 11:29:18 by Pone
homer, you might want to take a look at www.madwizard.org , especially the "win32asm basic tutorials" available under downloads.
Posted on 2003-03-18 12:21:04 by f0dder
Afternoon, Homer.

All these comments, and no-one's picked up on where the mistake has been made;)

As has been already noted, the reason you see stray letters in the string is because the buffer hasn't been zeroed out.
This is because you are writing 128 bytes to the file, and not just the length of the string. Remember that when you write/read from files, it doesn't stop at the first zero (0) encounted (like which would happen when using MessageBox).

Instead of zeroing out the buffer, you could just write out the number of characters in the buffer (dwtoa appends a zero automatically - that's why it displays correctly in MessageBox).
Instead of using:
invoke WriteFile,hFile, ADDR buffer,128,OFFSET NumberOfBytesRead,NULL

you could try...
invoke StrLen, ADDR buffer

invoke WriteFile,hFile, ADDR buffer,eax,OFFSET NumberOfBytesRead,NULL

Posted on 2003-03-18 16:06:06 by Scronty
thx scronty... i might use that for a different file type
but i set the writes to 128 to manage the file, ie i know where any given picture is at. 1 = 20, 2 = 148, 3 =276...

however if you could tell me how to use ReadFile to stop reading when it encounters a ,0 that would be great.
Save on alot of algorhytm bugs and file size.
Posted on 2003-03-18 16:53:52 by Homer
Afternoon, Homer.

For ReadFile, you'd have to load up a buffer and parse it with whatever you're looking for. There's no "magic" setting for it to do it automagically:grin: :alright: .


Depending upon what's actually *in* the file you're parsing, you may find using fscanf is what you're after (if you've got msvcrt.lib).

Posted on 2003-03-18 23:18:31 by Scronty
Alright. I'll check it out.
Posted on 2003-03-18 23:36:29 by Homer