 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
TheGood
Joined: 30 Jul 2007 Posts: 372
|
Posted: Tue Dec 30, 2008 12:23 am Post subject: How to get a string from its pointer and length |
|
|
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 |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 4367 Location: Qld, Australia
|
Posted: Tue Dec 30, 2008 12:49 am Post subject: |
|
|
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 |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 372
|
Posted: Tue Dec 30, 2008 1:15 am Post subject: |
|
|
| 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 |
|
 |
TheGood
Joined: 30 Jul 2007 Posts: 372
|
Posted: Tue Dec 30, 2008 1:22 am Post subject: |
|
|
| Sorry, didn't see the "str" in the return type parameter |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7159
|
Posted: Mon Jan 05, 2009 10:59 am Post subject: |
|
|
| 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. |
Thanks! _________________ Suresh Kumar A N |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|