Jump to content

Possible to simulate array passing for Windows API Calls?


Recommended Posts

I was looking at the function PathFindOnPath, and while it's nice that it looks at the paths in the %PATH% environment variable, I wanted to be able to supply additional ones in the second parameter. Unfortunately, what it says for the 2nd parameter is a 'null-terminated array of directories' (or NULL).

So from looking around the web to get an idea of how 'array' is setup in memory, the conclusion I came to was that it must be a list of pointers to strings. So I tried this code out, and its not finding the file I want (and I know it to exist in the first directory I set):

$stFolderPaths=DllStructCreate("wstr[256];wstr[256];wstr[256]")
DllStructSetData($stFolderPaths,1,@DesktopDir&'\t2')
DllStructSetData($stFolderPaths,2,@ScriptDir)
DllStructSetData($stFolderPaths,3,@WorkingDir)
$stPathPtrs=DllStructCreate("ptr;ptr;ptr;ptr")
DllStructSetData($stPathPtrs,1,DllStructGetPtr($stFolderPaths))
DllStructSetData($stPathPtrs,2,DllStructGetPtr($stFolderPaths,2))
DllStructSetData($stPathPtrs,3,DllStructGetPtr($stFolderPaths,3))
DllStructSetData($stPathPtrs,4,Ptr(0))

Local $aRet=DllCall("shlwapi.dll","int","PathFindOnPathW","wstr","notepad.txt","ptr",DllStructGetPtr($stPathPtrs))
If Not @error Then ConsoleWrite("Return values of PathFindonPath call: BOOL:"&$aRet[0]&", String:"&$aRet[1]&@CRLF)

Of course, the above doesn't work. I'm probably missing something obvious, but I was sure I was following the logic as it appeared on a few sites.

Any help is appreciated.. thanks

Link to comment
Share on other sites

Hrm, does the string "wchar" say you anything? :)

Holy crap, what a stupid move! haha, thank you for pointing it out. It HAD to be obvious didn't it. I do that occasionally (mix up my DLLCall variable descriptors with my DLLStruct ones). Thanks so much man.

By the way, for those that don't get it, this one line change is what fixes it:

$stFolderPaths=DllStructCreate("wchar[256];wchar[256];wchar[256]")

*edit: Just a note: 256 was kinda random, I forgot that MAX_PATH is 260, so that could have been used instead, or the actual length of the string that its going to contain (more memory efficient).

Edited by Ascend4nt
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...