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

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
User avatar
FredOoo
Posts: 118
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: 118
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: 118
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: 53
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 :-)

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 1 guest