Sign in to follow this  
Followers 0
Jettison

_ShellExecute problem

19 posts in this topic

Hey there,

I've been browsing the forums for as long as I've been an Au3 user, but never quite came to registering until now. :rolleyes: Here's my problem.

I'm using SlimShady's mod of the _ShellExecute UDF because I need to read input from an input box and run it. So it should accept spaces, arguments, etc.. all in one string. _ShellExecute works perfectly, except one small problem. Running strings such as 'notepad' or 'cmd' do not work. In order for them to work I have to use the appropriate extensions. I noticed this was in the example without extensions, and I'm using the latest version, so any help would be appreciated.

Thanks!


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't know where I got this, or if I hacked it after the fact, but this is the one I use. It works pretty much like what you get with Start/Run:

_ShellExecute("notepad")

Func _ShellExecute($sCmd, $sArg="", $sFolder = "", $rState = @SW_SHOWNORMAL)
   $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
      "hwnd",   0, _
      "string", "", _
      "string", $sCmd, _
      "string", $sArg, _
      "string", $sFolder, _
      "int",    $rState)
   If @error Then Return 0
       
   $RetVal = $aRet[0]
   If $RetVal > 32 Then
       Return 1
   else  
    Return 0
   EndIf
EndFunc
Edited by lod3n

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites

That works marvellous! Many thanks.

Just a tad problem, don't know if it even can be considered one...

If I enter a non-existant path/program, it displays know error message. I probably should create my own with the return values shouldn't I? Just checking...


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

If I enter a non-existant path/program, it displays know error message. I probably should create my own with the return values shouldn't I? Just checking...

Search the forums. Slim Shady did the effort to add the error messages etc. :rolleyes:

Share this post


Link to post
Share on other sites

Ahh ok. That's the one that's been causing me troubles, but I'll look into how he did the error messages. Thanks! :rolleyes:


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Ahh ok. That's the one that's been causing me troubles, but I'll look into how he did the error messages. Thanks! :rambo:

Sorry. Did not see Slims mention. Well, you now can make the best of the 2 UDFs into 1 now. :rolleyes:

Share this post


Link to post
Share on other sites

Ok erm..problems.

I'm no UDF master, so be nice. :rolleyes:

Func _ShellExecute($sCmd, $sArg="", $sFolder = "", $rState = @SW_SHOWNORMAL)
;Original by "ezzetabi", improved by "SlimShady"
    Local $aRet, $Err, $RetVal, $sArg
    Local $ERROR_OUT_OF_MEMORY      = 0 ;The operating system is out of memory or resources.
    Local $ERROR_FILE_NOT_FOUND = 2 ;The specified file was not found. 
    Local $ERROR_PATH_NOT_FOUND = 3   ;The specified path was not found. 
    Local $ERROR_BAD_FORMAT         = 11    ;The .exe file is invalid (non-Microsoft Win32® .exe or error in .exe image). 
    Local $SE_ERR_FNF                   = 2 ;The specified file was not found. 
    Local $SE_ERR_PNF                   = 3 ;The specified path was not found. 
    Local $SE_ERR_ACCESSDENIED      = 5 ;The operating system denied access to the specified file. 
    Local $SE_ERR_OOM                   = 8 ;There was not enough memory to complete the operation. 
    Local $SE_ERR_SHARE             = 26    ;A sharing violation occurred. 
    Local $SE_ERR_ASSOCINCOMPLETE   = 27  ;The file name association is incomplete or invalid. 
    Local $SE_ERR_DDETIMEOUT        = 28      ;The DDE transaction could not be completed because the request timed out. 
    Local $SE_ERR_DDEFAIL           = 29    ;The DDE transaction failed. 
    Local $SE_ERR_DDEBUSY           = 30    ;The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed. 
    Local $SE_ERR_NOASSOC           = 31    ;There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable. 
    Local $SE_ERR_DLLNOTFOUND       = 32    ;The specified dynamic-link library (DLL) was not found.
    $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
        "hwnd",   0, _
        "string", "", _
        "string", $sCmd, _
        "string", $sArg, _
        "string", $sFolder, _
        "int",  $rState)
    If @error Then Return 0
      
    $RetVal = $aRet[0]
    If $RetVal > 32 Then
       Return 1
    Else 
        Switch $RetVal
            Case $ERROR_OUT_OF_MEMORY
                $Err = 'The operating system is out of memory or resources.'
            Case $ERROR_FILE_NOT_FOUND
                $Err = 'The specified file was not found.'
            Case $ERROR_PATH_NOT_FOUND
                $Err = 'The specified path was not found.'
            Case $ERROR_BAD_FORMAT
                $Err = 'The .exe file is invalid (non-Microsoft Win32® .exe or error in .exe image).'
              Case $SE_ERR_FNF
                $Err = 'The specified file was not found.'
              Case $SE_ERR_PNF
                $Err = 'The specified path was not found.'
            Case $SE_ERR_ACCESSDENIED
                $Err = 'The operating system denied access to the specified file.'
            Case $SE_ERR_OOM
                $Err = 'There was not enough memory to complete the operation.'
            Case $SE_ERR_SHARE
                $Err = 'A sharing violation occurred.'
            Case $SE_ERR_ASSOCINCOMPLETE
                $Err = 'The file name association is incomplete or invalid.'
            Case $SE_ERR_DDETIMEOUT
                $Err = 'The DDE transaction could not be completed because the request timed out.'
            Case $SE_ERR_DDEFAIL
                $Err = 'The DDE transaction failed.'
            Case $SE_ERR_DDEBUSY
                $Err = 'The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed.'
            Case $SE_ERR_NOASSOC
                $Err = 'There is no application associated with the given file name extension.'
            Case $SE_ERR_DLLNOTFOUND
                $Err = 'The specified dynamic-link library (DLL) was not found.'
        EndSwitch
        MsgBox(16, "AutoIt Error", $Err)
        Return 0
   EndIf
EndFunc

That's..what I pieced together with lod3n's and Slim's code. It obviously doesn't work...I'm probably just thick, but I suppose that's the reason these forums were created. :rambo:


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Yep, I'm thick. :rolleyes: Just had to remove the first line which inits the variables...

Thanks for both of your help!


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Remove $sArg from the 1st line of local declarations below the comment. You cannot redeclare a parameter is the error.

:rolleyes:

Share this post


Link to post
Share on other sites

Ahh I just remove $sArg. Done and works like a charm. :rolleyes: Except one question. the input box I'm using is in a GUI with $WS_EX_TOOLWINDOW+$WS_EX_TOPMOST. Is there a way (besides having to change the style of the GUI before and after the error message) to have the GUI remain topmost, except for MsgBoxes? Stupid question...


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Use SplashTextOn() instead of the Msgbox(). SplashTextOn() has options to change it's state of behavior.

:rolleyes:

Share this post


Link to post
Share on other sites

Hmm...Only problem is that has to be set to be visible for a specific time, and I'd rather be able to be able to close it at will. :| A friend said that in other languages you'd use SetWidnowLong(). Is there a similar function in au3?


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Hmm, I figured it out. GUIDelete did the trick:

_FadeOut()
GUIDelete($GUI)
MsgBox(16, "AutoIt Error", $Err)

_FadeOut being my function to gradually fade the window out. Thanks for your help again!


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Problem. This won't load any parameters now. :rolleyes:

For example:

notepad.exe "test"

Gives an error.


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Any ideas? Here is my current code:

Func _ShellExecute($sCmd, $sArg="", $sFolder = "", $rState = @SW_SHOWNORMAL)
;Original by "ezzetabi", improved by "SlimShady"
    Local $aRet, $Err, $RetVal
    Local $ERROR_OUT_OF_MEMORY = 0
    Local $ERROR_FILE_NOT_FOUND = 2
    Local $ERROR_PATH_NOT_FOUND = 3
    Local $ERROR_BAD_FORMAT = 11 
    Local $SE_ERR_FNF = 2
    Local $SE_ERR_PNF = 3
    Local $SE_ERR_ACCESSDENIED = 5
    Local $SE_ERR_OOM = 8
    Local $SE_ERR_SHARE = 26
    Local $SE_ERR_ASSOCINCOMPLETE = 27
    Local $SE_ERR_DDETIMEOUT = 28
    Local $SE_ERR_DDEFAIL = 29
    Local $SE_ERR_DDEBUSY = 30
    Local $SE_ERR_NOASSOC = 31
    Local $SE_ERR_DLLNOTFOUND = 32
    $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
        "hwnd",   0, _
        "string", "", _
        "string", $sCmd, _
        "string", $sArg, _
        "string", $sFolder, _
        "int",  $rState)
    If @error Then Return 0
      
    $RetVal = $aRet[0]
    If $RetVal > 32 Then
       Return 1
    Else 
        Switch $RetVal
            Case $ERROR_OUT_OF_MEMORY
                $Err = 'The operating system is out of memory or resources.'
            Case $ERROR_FILE_NOT_FOUND
                $Err = 'The specified file was not found.'
            Case $ERROR_PATH_NOT_FOUND
                $Err = 'The specified path was not found.'
            Case $ERROR_BAD_FORMAT
                $Err = 'The .exe file is invalid (non-Microsoft Win32® .exe or error in .exe image).'
              Case $SE_ERR_FNF
                $Err = 'The specified file was not found.'
              Case $SE_ERR_PNF
                $Err = 'The specified path was not found.'
            Case $SE_ERR_ACCESSDENIED
                $Err = 'The operating system denied access to the specified file.'
            Case $SE_ERR_OOM
                $Err = 'There was not enough memory to complete the operation.'
            Case $SE_ERR_SHARE
                $Err = 'A sharing violation occurred.'
            Case $SE_ERR_ASSOCINCOMPLETE
                $Err = 'The file name association is incomplete or invalid.'
            Case $SE_ERR_DDETIMEOUT
                $Err = 'The DDE transaction could not be completed because the request timed out.'
            Case $SE_ERR_DDEFAIL
                $Err = 'The DDE transaction failed.'
            Case $SE_ERR_DDEBUSY
                $Err = 'The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed.'
            Case $SE_ERR_NOASSOC
                $Err = 'There is no application associated with the given file name extension.'
            Case $SE_ERR_DLLNOTFOUND
                $Err = 'The specified dynamic-link library (DLL) was not found.'
        EndSwitch
        _FadeOut()
        GUIDelete($GUI)
        MsgBox(16, "AutoIt Error", $Err)
        Return 0
   EndIf
EndFunc

It won't accept any parameters :rolleyes:


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites
:rolleyes: Is it okay to bump?

[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

I had this laying around somewhere. I apparently made it in January and never posted it. Strange.

Func _ShellExecute($sCmd, $sArg = "", $sFolder = "", $rState = @SW_SHOWNORMAL)

    $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
            "hwnd", 0, _
            "string", "", _
            "string", $sCmd, _
            "string", $sArg, _
            "string", $sFolder, _
            "int", $rState)
    If @error Then Return 0
    
    $RetVal = $aRet[0]
    If $RetVal > 32 Then Return 1
    Switch $RetVal
        Case 0 ;$ERROR_OUT_OF_MEMORY
            $Err = 'The operating system is out of memory or resources.'
        Case 2 ;$ERROR_FILE_NOT_FOUND
            $Err = 'The specified file was not found.'
        Case 3 ;$ERROR_PATH_NOT_FOUND
            $Err = 'The specified path was not found.'
        Case 11 ;$ERROR_BAD_FORMAT
            $Err = 'The .exe file is invalid (non-Microsoft Win32® .exe or error in .exe image).'
        Case 2 ;$SE_ERR_FNF
            $Err = 'The specified file was not found.'
        Case 3 ;$SE_ERR_PNF
            $Err = 'The specified path was not found.'
        Case 5 ;$SE_ERR_ACCESSDENIED
            $Err = 'The operating system denied access to the specified file.'
        Case 8 ;$SE_ERR_OOM
            $Err = 'There was not enough memory to complete the operation.'
        Case 26 ;$SE_ERR_SHARE
            $Err = 'A sharing violation occurred.'
        Case 27 ;$SE_ERR_ASSOCINCOMPLETE
            $Err = 'The file name association is incomplete or invalid.'
        Case 28 ;$SE_ERR_DDETIMEOUT
            $Err = 'The DDE transaction could not be completed because the request timed out.'
        Case 29 ;$SE_ERR_DDEFAIL
            $Err = 'The DDE transaction failed.'
        Case 30 ;$SE_ERR_DDEBUSY
            $Err = 'The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed.'
        Case 31 ;$SE_ERR_NOASSOC
            $Err = 'There is no application associated with the given file name extension.'
        Case 32 ;$SE_ERR_DLLNOTFOUND
            $Err = 'The specified dynamic-link library (DLL) was not found.'
    EndSwitch

    MsgBox(16, "AutoIt Error", $Err)
    Return 0
EndFunc   ;==>_ShellExecute

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Yes, but that doesn't work with parameters..I want to enter -anything - in my runbox, so f.ex.:

blackbox.exe -exec @BBCore.MinimizeAll

But also

blackbox -exec @BBCore.MinimizeAll

This is why I used Slim's UDF..but that one doesn't allow not specifying extension..:rambo:

[EDIT]

Ok..apparently parameters work with this one, but NOT just running things like 'notepad' or 'cmd' - you need to add exe at the end. :rolleyes:

[EDIT again]

Er..nevermind that, sorry. It only works with the other extension because of a function I created...here's bits and pieces of my code:

Func _RunThis()
    Global $run = GUICtrlRead($input)
    _CheckEnv()
    _CheckAlias()
    _CheckRun()
    $strleft = StringLeft($run, 1)
    $strleft4 = StringLeft($run, 4)
    $strmid = StringMid($run, 2, 1)
    If $strleft = "`" Then
        $run = StringTrimLeft($run, 1)
        $runex = 1
    ElseIf $strleft = "@" Then
        $runex = 2
    ElseIf $alias <> "" Then
        $run = $alias
    Else
        $run = StringFormat("%s", $run)
    ;MsgBox(0, "", $run)
    EndIf
    
    If $runex = 1 Then
        _DOSCmd($run)
    ElseIf $runex = 2 Then
        _ExecBroam($run)
    ElseIf $runex = 3 Then
        _ShellExecute($rPath, $rArgs)
    EndIf
    _WriteHist($run)
EndFunc

...

Func _ShellExecute($sCmd, $sArg = "", $sFolder = "", $rState = @SW_SHOWNORMAL)

    $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
            "hwnd", 0, _
            "string", "", _
            "string", $sCmd, _
            "string", $sArg, _
            "string", $sFolder, _
            "int", $rState)
    If @error Then Return 0
   
    $RetVal = $aRet[0]
    If $RetVal > 32 Then Return 1
    Switch $RetVal
        Case 0;$ERROR_OUT_OF_MEMORY
            $Err = 'The operating system is out of memory or resources.'
        Case 2;$ERROR_FILE_NOT_FOUND
            $Err = 'The specified file was not found.'
        Case 3;$ERROR_PATH_NOT_FOUND
            $Err = 'The specified path was not found.'
        Case 11;$ERROR_BAD_FORMAT
            $Err = 'The .exe file is invalid (non-Microsoft Win32® .exe or error in .exe image).'
        Case 2;$SE_ERR_FNF
            $Err = 'The specified file was not found.'
        Case 3;$SE_ERR_PNF
            $Err = 'The specified path was not found.'
        Case 5;$SE_ERR_ACCESSDENIED
            $Err = 'The operating system denied access to the specified file.'
        Case 8;$SE_ERR_OOM
            $Err = 'There was not enough memory to complete the operation.'
        Case 26;$SE_ERR_SHARE
            $Err = 'A sharing violation occurred.'
        Case 27;$SE_ERR_ASSOCINCOMPLETE
            $Err = 'The file name association is incomplete or invalid.'
        Case 28;$SE_ERR_DDETIMEOUT
            $Err = 'The DDE transaction could not be completed because the request timed out.'
        Case 29;$SE_ERR_DDEFAIL
            $Err = 'The DDE transaction failed.'
        Case 30;$SE_ERR_DDEBUSY
            $Err = 'The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed.'
        Case 31;$SE_ERR_NOASSOC
            $Err = 'There is no application associated with the given file name extension.'
        Case 32;$SE_ERR_DLLNOTFOUND
            $Err = 'The specified dynamic-link library (DLL) was not found.'
    EndSwitch

    MsgBox(16, "AutoIt Error", $Err)
    Return 0
EndFunc

...

Func _CheckRun()
    $findExt = StringInStr($run, ".")
    If $findExt <> 0 Then
        $len = StringLen($run)
        Global $rPath = StringTrimRight($run, $len-($findExt+3))
    ;Global $rPath = StringFormat("%s", $rPath)
        Global $rArgs = StringTrimLeft($run, $findExt+3)
        Global $runex = 3
    Else
        Global $runex = 0
    EndIf
EndFunc
Edited by Jettison

[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Ok added your function in a new test script, and tried running this:

_ShellExecute("C:\Blackbox\blackbox.exe -exec @BBCore.MinimizeAll")

And I get a 'File cannot be found' error.


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
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
Sign in to follow this  
Followers 0