A RawPath ? No parameters, no variables ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

A RawPath ? No parameters, no variables ?

15 Aug 2020, 19:51

If I get command lines from the registry, I would get some things like that :

C:\sys\AutoHotkey\SheBang\SheBang.exe %1
"C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE" /n "%1"
%SystemRoot%\system32\NOTEPAD.EXE %1

I cannot do a call to FileExist() on such kind of strings.

So, I've dev this function but it's not perfect and can probably be replaced by a more efficient one.

Code: Select all

get_RawPath( CmdPath ){
	if RegExMatch( CmdPath, "%([^%]+)%", o_Match ) {
		valueEnv := EnvGet(o_Match.Value(1))
		if valueEnv
			CmdPath := StrReplace( CmdPath, "%" o_Match.Value(1) "%" , valueEnv,,, Limit:=1 )
	}
	arr := StrSplit( CmdPath, ['%','/'] )
	if arr.Length > 0
		return Trim( StrReplace( arr[1], '`"' ) )
	else
		return Trim( StrReplace( CmdPath, '`"' ) )
}

f := get_RawPath( '"C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE" /n "%1"' )
MsgBox f "`n" (FileExist( f )?"Exists":"Doesn't exists")

; C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE
; Exists

Does someone know another way to do that ?
I guess there's probably a WinAPI function to do it.
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: A RawPath ? No parameters, no variables ?

17 Aug 2020, 04:04

Thanks @madsounds. I think you found it.

I'm not very confident using dllCall but I did that :

Code: Select all

ExpandEnvironmentStrings( strSrc, sizeDst :=2048 ){
	; Expands environment-variable strings and replaces them with the values defined for the current user.
	; DWORD ExpandEnvironmentStringsA(
	;   LPCSTR lpSrc,
	;   LPSTR  lpDst,
	;   DWORD  nSize
	; );
	BUFFER_Src := bufferAlloc( 2*(strLen( strSrc )) + 2 )  ; from AHK v2.a122 64 Unicode UTF-16 LE with BOM
	strPut( strSrc, BUFFER_Src.ptr,, "CP0" )
	BUFFER_Dst := bufferAlloc( sizeDst ) ; max sizeDst = 32*1024
	result := dllCall( "ExpandEnvironmentStringsA", 'Ptr',BUFFER_Src.ptr, 'Ptr',BUFFER_Dst.ptr, 'UInt',2048, 'UInt' )
	;msgBox "Src: " strGet( BUFFER_Src.ptr, "CP0" ) "`nresult: " result "`n" "Dst: " strGet( BUFFER_Dst.ptr, "CP0" )
	if result == 0 {
		return ""  ; throw Exception("dllCall fails")
	} else if result > sizeDst {
		return ""  ; throw Exception("BUFFER_Dst too small")
	} else {
		return strGet( BUFFER_Dst.ptr, "CP0" )
	}
}
msgBox ExpandEnvironmentStrings( "%SystemRoot%\system32\NOTEPAD.EXE %1" )
; C:\Windows\system32\NOTEPAD.EXE %1
Any criticism of this code is welcome. Especially about buffer sizes and encodings.
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: A RawPath ? No parameters, no variables ?

17 Aug 2020, 04:30

We can dllCall( "ExpandEnvironmentStringsW"… remplacing "CP0" by "CP1200".
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: A RawPath ? No parameters, no variables ?

18 Aug 2020, 02:11

Great! I don't understand yet completely what it does, but saved your function just in case :-)
User avatar
6Tom
Posts: 33
Joined: 26 Dec 2022, 21:28

Re: A RawPath ? No parameters, no variables ?

16 Apr 2023, 20:30

Hi, FredOoo.

I'm new. Although I cannot fully understand the meaning of every sentence in your function, it is very useful and it works very well!

For the latest AutoHotkey 2.0.2, it seems necessary to replace bufferAlloc with buffer, right?

Thank you for sharing!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Archimede, Nishanth, Panaku and 28 guests