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 

Variable empty using DllCall( "Readfile" ...

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



Joined: 01 Nov 2006
Posts: 17

PostPosted: Tue Oct 20, 2009 12:42 pm    Post subject: Variable empty using DllCall( "Readfile" ... Reply with quote

The following code worked in 1.0.47.06 but fails in 1.0.48.05 (error handling code not shown). The TextLen is retrieved correctly but the Text variable is empty (because the Temp variable is empty after the call to the FileRead function to retrieve the text).
Code:

;Get length of text in file (including trailing null)
FileRead( handle, Temp, 4 )
TextLen := NumGet( Temp )

;Get text
FileRead( handle, Temp, TextLen )
Text := Temp

;FileRead function
FileRead( handle, ByRef Result, Length )
{
   VarSetCapacity( Result, Length, 0 )
   DllCall( "ReadFile", handle, Result, Length, ... )
}

If I remove the use of the Temp variable when retrieving the text it works fine. For example:
Code:

;Get length of text in file (including trailing null)
FileRead( handle, Temp, 4 )
TextLen := NumGet( Temp )

;Get text
FileRead( handle, Text, TextLen )

It appears that AHK doesn't like the fact that the Temp variable was previously used to get the text length before it was filled with the null terminated text string.

[ Moderator!: Moved from Bug Reports ]
Back to top
View user's profile Send private message
TL



Joined: 01 Nov 2006
Posts: 17

PostPosted: Thu Oct 22, 2009 3:01 pm    Post subject: User error - Variable empty using DllCall( "Readfile&qu Reply with quote

Problem was fixed by making the following change to the ReadFile dll call:

From:
Code:

RtnCode := DllCall( "ReadFile", "UInt", Handle, "UInt", &TextRead, ... )

To:
Code:

RtnCode := DllCall( "ReadFile", "UInt", Handle, "Str", TextRead, ... )

Apparently, the pointer to the variable to receive the result needs to be specified as a "Str" instead of passing an address to the variable with "UInt".
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Thu Oct 22, 2009 4:40 pm    Post subject: Reply with quote

This would also work:
Code:
VarSetCapacity( TextRead, Length, 32 )
RtnCode := DllCall( "ReadFile", "UInt", Handle, "UInt", &TextRead, ... )
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