hello,

I am trying to read the bytes of a file while the process is active, but I am getting an error on CreateFile() function under Windows 98.

xor ebx, ebx
xcall CreateFile, lpFile, GENERIC_READ, FILE_SHARE_READ, ebx, OPEN_EXISTING, ebx, ebx


I have tested my program under Windows XP and it is working alright, how do I open a file with read rights under 98

thanks
Posted on 2004-08-26 19:19:28 by Jnrz
From the info you post it should work well... could you give some more details, or post some code? :notsure:
Posted on 2004-08-26 19:35:50 by QvasiModo
You are not supplying valid file attributes. Use FILE_ATTRIBUTE_NORMAL or other.
Posted on 2004-08-27 07:00:56 by omega_red
besides, nice code optimization pushing registers instead of zeros... :-D
Posted on 2004-08-27 07:16:41 by Dom
hello,

well i'm trying to read the bytes of an exe file while it is open, and win98 throws me an error. (that is what i think, because on XP is works just fine)
I am using a code similar to this:

ebx = 0



.data
szErrorOpenFile db "The program couldn't open the file %s",0

.code
xcall CreateFile, lpFile, GENERIC_READ, FILE_SHARE_READ, ebx, OPEN_EXISTING, ebx, FILE_ATTRIBUTE_NORMAL
.if eax != -1
; here i work with the handle of the file opened
.else
ccall wsprintf, addr szBuffer, offset szErrorOpenFile, lpFile
xcall MessageBox, hMainWnd, addr szBuffer, ebx, ebx
.endif


and on windows98 the else part is executed.
Posted on 2004-09-01 01:03:28 by Jnrz
There is a post that deals with self-deleting executeables->they are trying to access a running executeable as well:
http://www.asmcommunity.net/board/viewtopic.php?t=4190&highlight=self+delete

i remember they once talked about calling FreeLibrary...this might help you, too...try the call before deleting...

Dom
Posted on 2004-09-01 01:37:46 by Dom
What error code does it return? Use GetLastError to get the error code.
Posted on 2004-09-01 04:12:21 by Sephiroth3
see what i mean about XP doing us a favor on me code.
Posted on 2004-09-01 08:45:44 by cmax
Maybe something about the filename formating which only XP is handleing properly :?
Posted on 2004-09-01 10:10:49 by Eóin
Nah... for some reason, the stack contents being something that is "readable memory" and "sortof printable".
Posted on 2004-09-01 10:16:22 by f0dder
You are not supplying valid file attributes. Use FILE_ATTRIBUTE_NORMAL or other.

Nope, that can't be the problem. FILE_ATTRIBUTES_NORMAL == 0
Posted on 2004-09-01 16:19:38 by QvasiModo


.data
szErrorOpenFile db "The program couldn't open the file %s",0

.code
xcall CreateFile, lpFile, GENERIC_READ, FILE_SHARE_READ, ebx, OPEN_EXISTING, ebx, FILE_ATTRIBUTE_NORMAL
.if eax != -1
; here i work with the handle of the file opened
.else
ccall wsprintf, addr szBuffer, offset szErrorOpenFile, lpFile
xcall MessageBox, hMainWnd, addr szBuffer, ebx, ebx
.endif

The parameters are in the wrong order... but it should work just the same, since FILE_ATTRIBUTE_NORMAL == 0 :grin:

Perhaps this is the problem?
Windows Me/98/95: This string must not exceed MAX_PATH characters.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createfile.asp
Posted on 2004-09-01 16:24:48 by QvasiModo
You are not supplying valid file attributes. Use FILE_ATTRIBUTE_NORMAL or other.

Nope, that can't be the problem. FILE_ATTRIBUTES_NORMAL == 0

Well, you are wrong. I've run on the same problem some time ago.
#define FILE_ATTRIBUTE_READONLY             0x00000001  

#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_ARCHIVE 0x00000020
#define FILE_ATTRIBUTE_DEVICE 0x00000040
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define FILE_ATTRIBUTE_TEMPORARY 0x00000100
#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
#define FILE_ATTRIBUTE_COMPRESSED 0x00000800
#define FILE_ATTRIBUTE_OFFLINE 0x00001000
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
Posted on 2004-09-01 16:42:23 by omega_red
I use CreateFile with the attributes set to 0 all the time, and it always worked, both in Windows 98 and in Windows 2000. That shouldn't be a problem. I don't know if the attribute part is even used when it opens a file that already exists.
I think that the error code would help. The problem could be as stupid as having run out of file handles.
Posted on 2004-09-02 12:31:23 by Sephiroth3
Well, you are wrong. I've run on the same problem some time ago.
#define FILE_ATTRIBUTE_READONLY             0x00000001  

#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_ARCHIVE 0x00000020
#define FILE_ATTRIBUTE_DEVICE 0x00000040
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define FILE_ATTRIBUTE_TEMPORARY 0x00000100
#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
#define FILE_ATTRIBUTE_COMPRESSED 0x00000800
#define FILE_ATTRIBUTE_OFFLINE 0x00001000
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000

You are absolutely right, my apologies :)
In my defense, the FILE_ATTRIBUTE_NORMAL flag is overridden by any other file attribute, so I (incorrectly) assumed it was zero without ever checking ;)
Posted on 2004-09-02 16:25:51 by QvasiModo
I use CreateFile with the attributes set to 0 all the time, and it always worked, both in Windows 98 and in Windows 2000. That shouldn't be a problem. I don't know if the attribute part is even used when it opens a file that already exists.
I think that the error code would help. The problem could be as stupid as having run out of file handles.

If it's happening every time you run it on Win9x then this is not likely to be the problem (unless you're creating over 60,000 handles, if that's the case rethink your algos! :) ). Did you fix the parameter order? As omega_red pointed out, FILE_ATTRIBUTE_NORMAL does not equal 0 like I thought.
Posted on 2004-09-02 16:33:23 by QvasiModo
Extract from MSDN::CreateFile Remarks

When opening an existing file, CreateFile performs the following actions:

Combines the file flags specified by dwFlagsAndAttributes with existing file attributes. CreateFile ignores the file attributes specified by dwFlagsAndAttributes.


So i don't think it is related to the Attribute settings; Might be a sharing violation?. (otherwise could be xxxA vs xxxW?)

sharing violation can occur after the first time the file is opened if the second and subsequent open operations specify either a sharing mode that is different from the access mode specified in the first open operation.


does this apply cross-process?
When multiple processes try to open...
Seems so.

Otherwise i donno. :?

That MAX_PATH thing... well i just keep on changing the current directory as the user is browsing and then call CreateFile with only the Filename specified and no path; So that problem is overcome.
Posted on 2004-09-03 01:45:26 by Black iCE
Are you sure that the current directory is set to a correct value? (Maybe there's a wrong slash?) Check with GetFullPathName that the right file is being accessed. But you really should use the error code from GetLastError instead of guessing...
Posted on 2004-09-03 10:55:18 by Sephiroth3