AutoHotkey Community

It is currently May 26th, 2012, 12:58 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: December 30th, 2008, 1:23 am 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2008, 1:49 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2008, 2:15 am 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2008, 2:22 am 
Offline

Joined: July 30th, 2007, 11:32 pm
Posts: 581
Sorry, didn't see the "str" in the return type parameter


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 5th, 2009, 11:59 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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.


:shock: :D Thanks!

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Exabot [Bot], Google [Bot], Maestr0, poserpro and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group