Jump to content

Using two variables in a function?


 Share

Recommended Posts

I have two separate programs, one x86 and one x64.
If $type is 86 then @ProgramFilesDir & "\MyProgram\x86.exe"

it should work with x64 @ProgramFilesDir & "\MyProgram\x64.exe"

In this case, the exe file in the $type variable does not work

Global $ProgramLocation

$type = @ScriptDir & "\Test.exe"

ShellExecute(_TypeLocationRun($ProgramLocation,$type))

Func _TypeLocationRun($ProgramLocation, $FileBinType)
    Local $stType = DllStructCreate("dword;")
    $aRet = DllCall("kernel32.dll", "hwnd", "GetBinaryType", "str", $FileBinType, "ptr", DllStructGetPtr($stType))
    Switch DllStructGetData($stType, 1)
        Case 0
            $FileBinType = "x86"
        Case 6
            $FileBinType = "x64"
    EndSwitch

    If StringInStr($FileBinType, "x86") Then
        $ProgramLocation = @ProgramFilesDir & "\MyProgram\x86.exe"
    Else
        $ProgramLocation = @ProgramFilesDir & "\MyProgram\x64.exe"
    EndIf
    Return $ProgramLocation
EndFunc   ;==>_TypeLocationRun

 

Edited by youtuber
Link to comment
Share on other sites

@ProgramFilesDir output depends on the Autoit script execution format. If you compile it as x86 then @ProgramFilesDir will be "C:\Program Files (x86)" otherwise as x64 "C:\Program Files".

You cannot use for this purpose @ProgramFilesDir. I would suggest

If StringInStr($FileBinType, "x86") Then
        $ProgramLocation = EnvGet("ProgramFiles(x86)") & "\MyProgram\x86.exe"
    Else
        $ProgramLocation = EnvGet("ProgramFiles") & "\MyProgram\x64.exe"
    EndIf

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

This code works fine for me, but I want to use it as a function to avoid code redundancy.

$type = @ScriptDir & "\Test.exe"

Local $stType = DllStructCreate("dword;")
$aRet = DllCall("kernel32.dll", "hwnd", "GetBinaryType", "str", $type, "ptr", DllStructGetPtr($stType))
Switch DllStructGetData($stType, 1)
    Case 0
        $FileBinType = "x86"
    Case 6
        $FileBinType = "x64"
EndSwitch

If StringInStr($FileBinType, "x86") Then
    $ProgramLocation = @ProgramFilesDir & "\MyProgram\x86.exe"
Else
    $ProgramLocation = @ProgramFilesDir & "\MyProgram\x64.exe"
EndIf

ShellExecute($ProgramLocation, $type)

or

#include <WinAPIFiles.au3>

Local $sText, $sPath = @ScriptDir & '\Test.exe'

If _WinAPI_GetBinaryType($sPath) Then
        Switch @extended
            Case $SCS_32BIT_BINARY
                $sText = 'x86'
            Case $SCS_64BIT_BINARY
                $sText = 'x64'
        EndSwitch
    EndIf

If StringInStr($sText, "x86") Then
    $ProgramLocation = @ProgramFilesDir & "\MyProgram\x86.exe"
Else
     $ProgramLocation = @ProgramFilesDir & "\MyProgram\x64.exe"
EndIf

ShellExecute($ProgramLocation, $sPath)

 

Edited by youtuber
Link to comment
Share on other sites

I think there may be a conflict with your Global $ProgramLocation and your local parameter Func _TypeLocationRun($ProgramLocation, $FileBinType) having the same name.  

Link to comment
Share on other sites

Not really sure why you have the first parameter in your function as it isn't required, I'd probably use somethine like:

Global $g_sFilepath = @ScriptDir & "\Test.exe"
ShellExecute(_TypeLocationRun($g_sFilepath), $g_sFilepath)

Func _TypeLocationRun($_sFilepath)
    Local $stType = DllStructCreate("dword;")
    Local $aRet = DllCall("kernel32.dll", "hwnd", "GetBinaryType", "str", $_sFilepath, "ptr", DllStructGetPtr($stType))
    Switch DllStructGetData($stType, 1)
        Case 0
            Return @ProgramFilesDir & "\MyProgram\x86.exe"
        Case 6
            Return @ProgramFilesDir & "\MyProgram\x64.exe"
        Case Else
            Return ""
    EndSwitch
EndFunc   ;==>_TypeLocationRun

 

Link to comment
Share on other sites

12 minutes ago, Subz said:

 

Global $g_sFilepath = @ScriptDir & "\Test.exe"
ShellExecute(_TypeLocationRun($g_sFilepath), $g_sFilepath)

Func _TypeLocationRun($_sFilepath)
    Local $stType = DllStructCreate("dword;")
    Local $aRet = DllCall("kernel32.dll", "hwnd", "GetBinaryType", "str", $_sFilepath, "ptr", DllStructGetPtr($stType))
    Switch DllStructGetData($stType, 1)
        Case 0
            Return @ProgramFilesDir & "\MyProgram\x86.exe"
        Case 6
            Return @ProgramFilesDir & "\MyProgram\x64.exe"
        Case Else
            Return ""
    EndSwitch
EndFunc   ;==>_TypeLocationRun

 

@Subz Yes this works great for me thank you :)

Link to comment
Share on other sites

I've a logical problem to understand how this will work properly.

 

Let's say Test.exe is a x86 file but the AutoIt script is compiled as x64. My understanding is that the AutoIt script should execute the appropriate exe in MyProgram folder.

In this case DllStructGetData($stType, 1) will return 0 (x86) but @ProgramFilesDir points to "C:\Program Files" the x64 folder because the AutoIt script is compiled as x64.

But it should execute "C:\Program Files (x86)\MyProgram\x86.exe", shouldn't it?

 

Example:

#AutoIt3Wrapper_UseX64=y
#include <WinAPIFiles.au3>

_WinAPI_GetBinaryType("c:\Program Files (x86)\AutoIt3\Au3Info.exe")
Switch @extended
    Case $SCS_32BIT_BINARY
        ConsoleWrite(EnvGet("ProgramFiles(x86)") & " <> " & @ProgramFilesDir & @CRLF)
EndSwitch

 

Btw, you can use

_WinAPI_GetBinaryType

too.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...