Page 1 of 1

A RawPath ? No parameters, no variables ?

Posted: 15 Aug 2020, 19:51
by FredOoo
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.

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

Posted: 16 Aug 2020, 21:26
by madsounds
Maybe this one: https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsa

I'm not really into DllCall, maybe I'll try later to make a function :-)

Re: A RawPath ? No parameters, no variables ?

Posted: 17 Aug 2020, 04:04
by FredOoo
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.

Re: A RawPath ? No parameters, no variables ?

Posted: 17 Aug 2020, 04:30
by FredOoo
We can dllCall( "ExpandEnvironmentStringsW"… remplacing "CP0" by "CP1200".

Re: A RawPath ? No parameters, no variables ?

Posted: 18 Aug 2020, 02:11
by madsounds
Great! I don't understand yet completely what it does, but saved your function just in case :-)

Re: A RawPath ? No parameters, no variables ?

Posted: 16 Apr 2023, 20:30
by 6Tom
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!