Jump to content

ShellExecute with DllCall


SumTingWong
 Share

Recommended Posts

Just want to mention that it is now possible to call ShellExecute with DllCall.

Example:

$ret = DllCall("shell32.dll", "long", "ShellExecute", _
"hwnd", 0, "string", "", _
"string", "C:\Your Doc.doc", "string", "", _
"string", "", "long", @SW_SHOWNORMAL)
Edited by pacman
Link to comment
Share on other sites

An other idea:

In this version you can use also "Verbs", arguments and a different starting dir.

$sVerb = ''
$sFile = 'cmd'
$sArguments = ' /k dir'
$sStartingDir = 'e:\'

$aRet = DllCall("shell32.dll", "long", "ShellExecute", "hwnd", 0, "string", $sVerb, _
"string", $sFile, "string", $sArguments, "string", $sStartingDir, "long", @SW_SHOWNORMAL)

If $aRet[0] <= 32 Then
   MsgBox(0,'','Error executing ' & $aRet[3])
EndIf

Pacman? What $iRet should include? I always see it as a empty string... :)

Edit: Ok, as MSDN say here it is actually a interger... But I can't read the value anyway.

Edit2: As Autoit help says it is a array. And this solves the problem. :) Modified the script allowing a error message if something goes wrong.

Edited by ezzetabi
Link to comment
Share on other sites

  • 3 months later...

I improved ezzetabi's ShellExecute.

It now works almost exactly like Run.

No full path necessary and you can hide, minimize the application window.

And ofcourse the purpose of this function: run non-executables.

The ShellExecute UDF and it's dependent function are attached uploaded.

Here are some examples.

#include "_ShellExecute.au3"

;Example 1
_ShellExecute('notepad')

Sleep(5000)

;Example 2
_ShellExecute('ping 127.0.0.1')

Sleep(5000)

;Example 3
_ShellExecute('cmd /c START notepad', @SystemDir, @SW_HIDE)

Sleep(5000)

;Example 4
_ShellExecute(@ProgramFilesDir & '\AutoIt3\AutoIt.chm')

Sleep(5000)

;Example 5
_ShellExecute('http://www.autoitscript.com', @SystemDir)

I just uploaded my latest version here: http://www.autoitscript.com/fileman/users/SlimShady/_ShellExecute.au3

Edited by SlimShady
Link to comment
Share on other sites

  • 8 months later...
  • 4 weeks later...

_ShellExecute seems to have some problems, if the filename contains spaces, is that right?

I tried to call it with the argument 'winword "testfile 01.doc"', but the application start failed -

to say it more correctly: winword started, but wasn't able to find the file.

I also tried 'winword "testfile{SPACE}01.doc"' - but it was the same.

As far as i understand it, the space is interpretet as a separator for arguments, right?

But that shouldn't be, if there is a fileextension (.doc in this case) on the right side of the string, or not?

cu

Link to comment
Share on other sites

Just comment out the code as below. However, this will ruin the point of these steps (cheezy hack)B) ...

;~    If ValidFilename($sCmd) Then
;~       $sArg = ""
;~    Else
;~       $sNewCmd = ValidFilename($sCmd, 1)
;~       If StringInStr($sNewCmd, @LF) Then
;~          $sNewCmd = StringSplit($sNewCmd, @LF)
;~          $sCmd = $sNewCmd[1]
;~          $sArg = $sNewCmd[2]
;~       Else
         $sArg = ""
;~       EndIf
;~    EndIf

Notice, one line above is NOT commented out! Now it will allow this to run:

_ShellExecute('C:\Documents and settings\Jeff\My Documents\Collective exp.doc', @DocumentsCommonDir)

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

@w0uter:

%20 doesn't work; i didn' t check the other option yet (short dos-name)

@jefhal:

ok, that works for the moment (if you doesn't wantto add arguments), but at the end it should be possible to add arguments.

thx

Link to comment
Share on other sites

_ShellExecute seems to have some problems, if the filename contains spaces, is that right?

No.

I tried to call it with the argument 'winword "testfile 01.doc"'

Why? Use Run() or RunWait() instead.

, but the application start failed -

obviously.

to say it more correctly: winword started, but wasn't able to find the file.

It's very important to always use a full path.

Not only with this function, all functions and applications.

I also tried 'winword "testfile{SPACE}01.doc"' - but it was the same.

B)

As far as i understand it, the space is interpretet as a separator for arguments, right?

True.

But that shouldn't be, if there is a fileextension (.doc in this case) on the right side of the string, or not?

You're talking very weird now.

If you have spaces in a filename or path, surround it wth quotes.

Example:

"C:\Program Files\AutoIt3\AutoIt3.exe"

Run('"C:\Program Files\AutoIt3\AutoIt3.exe" "' & @DesktopDir & '\Test.au3"')

or

#include "_ShellExecute.au3"

_ShellExecute('"' & @DesktopDir & '\Test.au3"')

I discovered that the old version did not work well.

I just uploaded my latest version here: http://www.autoitscript.com/fileman/users/SlimShady/_ShellExecute.au3

Link to comment
Share on other sites

  • 1 month later...

_ShellExecute seems to have some problems, if the filename contains spaces, is that right?

I tried to call it with the argument 'winword "testfile 01.doc"', but the application start failed -

to say it more correctly: winword started, but wasn't able to find the file.

I also tried 'winword "testfile{SPACE}01.doc"' - but it was the same.

As far as i understand it, the space is interpretet as a separator for arguments, right?

But that shouldn't be, if there is a fileextension (.doc in this case) on the right side of the string, or not?

cu

The reason it failed is because used it wrong below i wrote a example and it works so i hope this can help on what ever your trying to do.

$run = "winword.exe"; executable you want to run
$args = "path\testfile 01.doc"; arguments
$verb = "Open"; other valide functions are edit, print, or just leave it blank
$dir = ""; path of executabe can leave blank if it's in the system path, c:\windows\, c:\windows\system32 etc..
$sw = 3; hide = 0, maximize = 3, minimize = 6

$dll = DllOpen("shell32.dll")

$ret = Dllcall($dll,"long","ShellExecuteA", _
            "hwnd",0, _
            "str",$func, _
            "str",$run, _
            "str",$args, _
            "str",$dir, _
            "int",$sw) 
            
DllClose($dll)

thats one way or you can change run to $run = "path\testfile 01.doc and leave $args blank and use defaulted application.

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