AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Fixed Thx] dllcall(readfile) and FilePointerEx help

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Razlin



Joined: 05 Nov 2007
Posts: 400
Location: canada

PostPosted: Fri May 09, 2008 5:26 pm    Post subject: [Fixed Thx] dllcall(readfile) and FilePointerEx help Reply with quote

Hi guys,

I'm new to the dll business I hope to get it one day its painfully slow for some odd reason.

I'm trying to read from a file starting at offset character 50 for example.

I got the reading working fine from help file.

Code:
DllCall("ReadFile", UInt, hFile, str, TestString, UInt, BytesToRead, UIntP, BytesActuallyRead, UInt, 0)


info from msdn site http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx

Quote:
BOOL WINAPI ReadFile(
__in HANDLE hFile,
__out LPVOID lpBuffer,
__in DWORD nNumberOfBytesToRead,
__out_opt LPDWORD lpNumberOfBytesRead,
__inout_opt LPOVERLAPPED lpOverlapped
);


what I need help is with overlapped.

Quote:
If hFile is opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure, and ReadFile may return before the read operation is complete.


overlapped stuct from msdn

Quote:

typedef struct _OVERLAPPED {
ULONG_PTR Internal;
ULONG_PTR InternalHigh;
union {
struct {
DWORD Offset;
DWORD OffsetHigh;
};
DVOID Pointer;
};
HANDLE hEvent;
} OVERLAPPED,
*LPOVERLAPPED;


This means nothing to me.

how do I configure Offset in the overlapped stuct to be 50.

I probably aint even explainning this right

[ Moderator!: MSDN link fixed ]
_________________
-=Raz=-


Last edited by Razlin on Fri May 16, 2008 2:13 pm; edited 3 times in total
Back to top
View user's profile Send private message
poetbox



Joined: 07 Jan 2007
Posts: 59

PostPosted: Fri May 09, 2008 5:32 pm    Post subject: Reply with quote

Code:
FileRead, Contents, C:\Address List.txt
StringRight, OutputVar, Contents, StrLen(Contents)-50
msgbox %OutputVar%
Back to top
View user's profile Send private message
Razlin



Joined: 05 Nov 2007
Posts: 400
Location: canada

PostPosted: Fri May 09, 2008 6:11 pm    Post subject: Reply with quote

@Poetbox
the idea is to not use readfile and use the dllcall. reason bellow.


I have written an app already that uses readfile and does work but its not fast enough to read large files.

I was testing my app that I wrote on a 190mg file it worked great locally like less then 10 seconds to read but the logs are on network drives and using fileread was slow on network as it has to read in the entire file like 5-10 minutes.

if I use a dllcall I can "only" read the end of the file therefore reading will be done exceptionaly faster.
Hence why I need dllcall help.
thanks for your time and effort though
_________________
-=Raz=-
Back to top
View user's profile Send private message
Zippo()
Guest





PostPosted: Fri May 09, 2008 10:36 pm    Post subject: Reply with quote

Here is how you calculate the low byte/high byte of a file size:

Code:
a := 0xfffffff
b := a&0xffffffff
c := Floor(a/0xffffffff)
MsgBox % "LowByte = "b . " HighByte = " . c


Subtract your file pointer from the file size then run the math on it.
Back to top
Zippo()
Guest





PostPosted: Fri May 09, 2008 11:17 pm    Post subject: Reply with quote

Sorry I should explain that better. It's been a long week Sad

To read the last 50 bytes of a file, fill in the structure like this:
Code:
FileGetSize, filesize, filename.ext
filesize := filesize-50 ;Read the last 50 bytes of the file
filepointerlb := filesize&0xffffffff
filepointerhb := Floor(filesize/0xffffffff)

NumPut(0, OVERLAPPED, 0, "UInt")
NumPut(0, OVERLAPPED, 4, "UInt")
NumPut(filepointerlb, OVERLAPPED, 8, "UInt")
NumPut(filepointerhb, OVERLAPPED, 12, "UInt")
NumPut(0, OVERLAPPED, 16, "UInt")
NumPut(hEvent, OVERLAPPED, 20, "UInt") ;hEvent = handle returned from CreateEvent


I don't know what kind of problems you'll have if you don't sync the fileIO by waiting on an event, but you can specify 0 for hEvent. I've never had a use for this structure.

Like I said, it's been a long week. If I made any mistakes hopefully someone will correct me Smile
Back to top
Lexikos



Joined: 17 Oct 2006
Posts: 2592
Location: Australia, Qld

PostPosted: Sat May 10, 2008 1:02 am    Post subject: Reply with quote

OVERLAPPED structures are only 20 bytes - the union means that Offset and Pointer share the same memory, and hEvent is at 16, not 20.
Quote:
Code:
a := 0xfffffff
b := a&0xffffffff
c := Floor(a/0xffffffff)
MsgBox % "LowByte = "b . " HighByte = " . c
Technically that should be:
Code:
a := 0xfffffff
b := a&0xffffffff
c := a >> 32
MsgBox % "LowDword = " . b . " HighDword = " . c

You may find it simpler to set the file pointer with SetFilePointerEx, and simply read however many bytes you need. For instance, to read the last 50 bytes, set the file pointer to the file size minus 50 bytes, then read 50 bytes. OVERLAPPED structures are typically only used for overlapped I/O (i.e. the program continues whatever it was doing while the system reads the file into memory.)
Back to top
View user's profile Send private message
Razlin



Joined: 05 Nov 2007
Posts: 400
Location: canada

PostPosted: Thu May 15, 2008 1:45 pm    Post subject: Reply with quote

This is really advanced for me at this point but hopefully I can get a hand at figuring this out.
Here is my attempt with filepointerEx

DO NOT RUN THIS CODE
As I dont know what it will do.
In theory it shouldnt do anything but its playing with file handles and with my luck something I did is wrong.


am I going in the right direction with this?
and what values do I put where its Orange?

Code:

/*
SetFilePointerEx(
  __in       HANDLE hFile,
  __in       LARGE_INTEGER liDistanceToMove,
  __out_opt  PLARGE_INTEGER lpNewFilePointer,
  __in       DWORD dwMoveMethod
);

dwMoveMethod
0 FILE_BEGIN
1 FILE_CURRENT
2 FILE_END
*/

;### Get file handle    this section should be fine.   "should be"
GENERIC_READ = 0x80000000  ; Open the file for reading rather than writing.
OPEN_EXISTING = 3  ; This mode indicates that the file to be opened must already exist.
FILE_SHARE_READ = 0x1 ; This and the next are whether other processes can open the file while we have it open.
FILE_SHARE_WRITE = 0x2
filename = c:\test.txt
hFile := DllCall("CreateFile", str, FileName, UInt, GENERIC_READ, UInt, FILE_SHARE_READ|FILE_SHARE_WRITE, UInt, 0, UInt, OPEN_EXISTING, Uint, 0, UInt, 0)
;###


;###Numput stuff
VarsetCapacity(NewFilePointer, 16, 0)    ;I think it's 16.

;This is where I'm the most confused / lost / helpless / ... ...
;Whats right here and whats wrong here.. or is there anything right lol.
numput(hfile,    NewFilePointer, 0, "Int")
numput(10,       NewFilePointer, 4, "UInt")
numput(DWORD,    NewFilePointer, 8, "UInt")
numput(3,       NewFilePointer, 12, "UInt")

ReturnValue := DllCall("SetFilePointerEx", UInt, &NewFilePointer)

DllCall("CloseHandle", UInt, hFile)  ; Close the file.


Thanks again to all whom have learned this and can shed some light.
_________________
-=Raz=-
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2592
Location: Australia, Qld

PostPosted: Thu May 15, 2008 10:27 pm    Post subject: Reply with quote

SetFilePointerEx accepts four parameters, not a structure with four fields.
Code:
BOOL SetFilePointerEx(
  HANDLE hFile,                    ; UInt
  LARGE_INTEGER liDistanceToMove,  ; Int64
  PLARGE_INTEGER lpNewFilePointer, ; Int64P
  DWORD dwMoveMethod               ; UInt
);
Back to top
View user's profile Send private message
Razlin



Joined: 05 Nov 2007
Posts: 400
Location: canada

PostPosted: Fri May 16, 2008 2:04 pm    Post subject: Reply with quote

@Lexikos
Thank you.
Thank you.
Thank you.


I dont know why I was trying to make it a struct.. I guess the overlapped got me all mixed up.
then I had to use numput and that got me all mixed up.

and then boom... you saved the day.

Thank you, thank you.. I got it working.

Code:
ReturnValue := DllCall("SetFilePointerEx", UInt, hfile, int64, -charcount, Int64P, newval, Uint, 2)


Woot.
_________________
-=Raz=-
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group