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 

How to get a string from its pointer and length

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



Joined: 30 Jul 2007
Posts: 372

PostPosted: Tue Dec 30, 2008 12:23 am    Post subject: How to get a string from its pointer and length Reply with quote

As the title says, I need the most efficient (read, fastest) way to retrieve a string if you only know its address and its length.
This is what I have right now:

Code:
sTest := "This is a test sentence."

MsgBox % PtrToStr(&sTest, StrLen(sTest))   ;%

ExitApp

PtrToStr(ptr, len) {
   VarSetCapacity(s, len, 0)
   Loop %len%
      s .= Chr(*(ptr + A_Index - 1))
   Return s
}

Is there a faster way to do this?

Edit: I forgot to add, I tried messing around with RtlCopyString and RtlCopyBytes, but I get an ErrorLevel of -4 (function not found)
Back to top
View user's profile Send private message Visit poster's website
Lexikos



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

PostPosted: Tue Dec 30, 2008 12:49 am    Post subject: Reply with quote

If the string is always null-terminated (as with all AutoHotkey strings), the easiest way is:
Code:
sTest := "This is a test sentence."
MsgBox % DllCall("MulDiv", int, &sTest, int, 1, int, 1, str)
MulDiv(x,1,1) returns x unmodified, but DllCall interprets the return value as a string.

If the string isn't null-terminated or you want only a portion of the string, you may use lstrcpyn via DllCall.
Back to top
View user's profile Send private message Visit poster's website
TheGood



Joined: 30 Jul 2007
Posts: 372

PostPosted: Tue Dec 30, 2008 1:15 am    Post subject: Reply with quote

Lexikos wrote:
MulDiv(x,1,1) returns x unmodified, but DllCall interprets the return value as a string.

Very interesting behaviour. Is that an AHK thing, or the dll itself? Is it because AHK knows it's a string pointer that was passed and not any number?
Back to top
View user's profile Send private message Visit poster's website
TheGood



Joined: 30 Jul 2007
Posts: 372

PostPosted: Tue Dec 30, 2008 1:22 am    Post subject: Reply with quote

Sorry, didn't see the "str" in the return type parameter
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Mon Jan 05, 2009 10:59 am    Post subject: Reply with quote

Lexikos wrote:
Code:
sTest := "This is a test sentence."
MsgBox % DllCall("MulDiv", int, &sTest, int, 1, int, 1, str)
MulDiv(x,1,1) returns x unmodified, but DllCall interprets the return value as a string.


Shocked Very Happy Thanks!
_________________
Suresh Kumar A N
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