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 

[Solved] Create struct and get data from it.

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



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Sun Feb 07, 2010 10:02 am    Post subject: [Solved] Create struct and get data from it. Reply with quote

Hi,

I'm sure it has been asked thousand times. But I can't find a tutorial how to do it. Sorry.
I have a DLLCall that expects an struct:
Code:
typedef struct {
    char *name;
    char *driver;
    DWORD flags;
} BASS_DEVICEINFO;
How do i calculate the right Capacity? And how do I afterwards get the data with NumGet() out of it? Thanks.

If you know a tutorial on structs in AHK, please point me to it.
_________________
Ciao
toralf


Last edited by toralf on Mon Feb 08, 2010 6:01 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Sun Feb 07, 2010 10:24 am    Post subject: Reply with quote

I found one tutorial: http://www.autohotkey.com/forum/topic35843.html
But I'm not so sure I understood.

Here is what i THINK the code is supposed to be...

Since in the struct are two pointers for strings and one is an integer, the capacity should be 12 (3*4)
Code:
VarSetCapacity(BASS_DEVICEINFO,12)


Then the DLL is called with the address of the BASS_DEVICEINFO struct
Code:
DllCall(BASS_DLLPATH . BASS_DLL . "\BASS_GetDeviceInfo", UInt, device, UInt, &BASS_DEVICEINFO)


The pointers to the strings and the number can then be retrieved with NumGet from the struct
Code:
pName := NumGet( BASS_DEVICEINFO+0 , 0, "UInt" )
pDriver := NumGet( BASS_DEVICEINFO+4 , 0, "UInt" )
Flags := NumGet( BASS_DEVICEINFO+8 , 0, "UInt" )
Unfortunately that doesn't work. :|
Any help?
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Sun Feb 07, 2010 10:59 am    Post subject: Reply with quote

Do the pointers returned in that struct point to memory locations inside your script's memory space or elsewhere?

I'm guessing that those strings are in some other process an you'll need ReadProcessMemory to get them.
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Sun Feb 07, 2010 11:11 am    Post subject: Reply with quote

Could be. I'm not sure.
The DLL is loaded with the AHL script.
How could I test this?
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
nick



Joined: 24 Aug 2005
Posts: 549
Location: Berlin / Germany

PostPosted: Sun Feb 07, 2010 11:30 am    Post subject: Reply with quote

Moin,

wenn es ANSI ist, probier mal sowas / if it is ANSI, you might try something like this:
Code:
pName := NumGet(BASS_DEVICEINFO, 0, "UInt")
pDriver := NumGet(BASS_DEVICEINFO, 4, "UInt")
Flags := NumGet(BASS_DEVICEINFO , 8, "UInt")

VarSetCapacity(Name, DllCall("lstrlenA", Uint, pName) + 1, 0)
DllCall("lstrcpyA", Str, Name, Uint, pName)
MsgBox, %Name%


_________________
nick Wink
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Sun Feb 07, 2010 12:04 pm    Post subject: Reply with quote

Thanks a lot,
That solved it.

Is there a shorter way to do it?
It seems to be quite complicated.
First to create a Var with a capacity of the string length the pointer is pointing at.
Then copying the string the pointer is pointing at into the var.

If not, I can live with it.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Mon Feb 08, 2010 9:21 am    Post subject: Reply with quote

You could always create a function to retrieve the string. Here's a trick:
Code:
StrGetA(p) {
    return DllCall("MulDiv", "int", p, "int", 1, "int", 1, A_IsUnicode ? "astr":"str")
}
MulDiv returns p as-is, but DllCall sees the return type is "str" so interprets the return value (p) as a string pointer.
Code:
VarSetCapacity(bdi, 12, 0)
if !DllCall("LoadLibrary", "str", "bass.dll")
|| !DllCall("bass\BASS_GetDeviceInfo", "uint", 1, "uint", &bdi)
    ExitApp

MsgBox % "
(
name: "   StrGetA(NumGet(bdi,0)) "
driver: " StrGetA(NumGet(bdi,4)) "
flags: "          NumGet(bdi,8)
)
Tested okay with AutoHotkey v1.0.48.05 and ANSI/Unicode builds of AutoHotkey_L. (Btw, AutoHotkey_L and AutoHotkeyU have StrGet() built-in, but it is currently incapable of retrieving ANSI strings in Unicode builds.)
Back to top
View user's profile Send private message Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Mon Feb 08, 2010 6:00 pm    Post subject: Reply with quote

Thanks a lot Lexikos. DllCall are not my cup of tea....
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Guest






PostPosted: Tue Feb 09, 2010 1:19 am    Post subject: Reply with quote

toralf wrote:
That solved it.

...instead of simply marking this solved, could someone create or point me to current struct handling info/code? I know there's ahkstructlib, last I checked no one has updated it in a while...is that currently the best option?...still?...would someone please make a full struct handling API? I don't like using NumGet() directly, cuz you need to know the offsets...plus most structs have LPSTR fields, then you only have a pointer to a string & need to do "something else" to get/set that string...I'm pretty good savvy with AutoHotkey/computers, but structs have been giving me fits for years...MSDN or Google will tell me a struct field is LPSTR (or DWORD) & in NumGet() I'm supposed to know to use UInt or <whatever>? what about LPCSTR? is that UInt too?
Back to top
a_h_k



Joined: 02 Feb 2008
Posts: 626

PostPosted: Tue Aug 03, 2010 3:22 am    Post subject: Reply with quote

Have a look at this ---> Easy WinAPI - WinAPI Parser, DllCall with single hotkey Question
_________________
My Ahk Site
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Tue Aug 03, 2010 3:40 am    Post subject: Reply with quote

Thanks, but no source makes it less-than-useful. Anything else?...with source?
Back to top
a_h_k



Joined: 02 Feb 2008
Posts: 626

PostPosted: Tue Aug 03, 2010 6:04 am    Post subject: Reply with quote

Did you see this part?

Easy WinAPI - WinAPI Parser, DllCall with single hotkey wrote:
Limitations
. . .
3) It does not parse structs. use Lexiko's StructParser or corrupt's ahkStructLib2 together

And they both have source Smile
_________________
My Ahk Site
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Tue Aug 03, 2010 1:38 pm    Post subject: Reply with quote

a_h_k wrote:
Did you see this part?

...yes, I know about StructParser (& that's actually pretty helpful {it's just not a Struct API}) & I already mentioned that I know about ahkstructlib. Maybe I'll smash them together & try to make my own version of struct-handling code? So I guess the answer is no, there's nothing new in the vanilla-AHK struct-handling front? Actually, if I hold out long enough, I'll just use IronAHK & (I'm guessing) that should have all the struct-handling I need!
Back to top
Display posts from previous:   
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