Jump to content

Recommended Posts

Posted (edited)

Hi all,

I'm trying to use _WinAPI_CreateProcess with Command Line Arguments as I have user input that can't be trusted, but can't use ShellExecute.

I'm having issues with it handling some command line parameters. For example:

#include <WinAPIProc.au3>

Local $tProcess = DllStructCreate($tagPROCESS_INFORMATION)
Local $tStartup = DllStructCreate($tagSTARTUPINFO)  

; Works
Run("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe C:\Users\example\Documents\index.pdf")

; Doesn't work
_WinAPI_CreateProcess("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", "C:\Users\example\Documents\index.pdf", 0, 0, 0, $CREATE_NEW_PROCESS_GROUP, 0, 0, $tStartup, $tProcess)

 

Per old forum topics, I know Run uses CreateProcess so obviously this is user error and I've screwed up my input but I've tried escaping slashes (\\ vs \), adding null at the end of the string, and a few other odds and ends without success.

Any advice is appreciated!

index.pdf

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Posted (edited)

Does this work for you?  If not, what does the _WinAPI_ShowLastError() message box say?

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)

$bSuccessful = _WinAPI_CreateProcess( _
                                     '', _
                                     '"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" C:\Users\example\Documents\index.pdf', _
                                     0, 0, 0, _
                                     $CREATE_NEW_PROCESS_GROUP, _
                                     0, 0, _
                                     $tStartup, _
                                     $tProcess _
                                     )

If Not $bSuccessful Then _WinAPI_ShowLastError()

Or (both do the same thing)

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)

$bSuccessful = _WinAPI_CreateProcess( _
                                     '"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"', _
                                     'C:\Users\example\Documents\index.pdf', _
                                     0, 0, 0, _
                                     $CREATE_NEW_PROCESS_GROUP, _
                                     0, 0, _
                                     $tStartup, _
                                     $tProcess _
                                     )

If Not $bSuccessful Then _WinAPI_ShowLastError()

 

Edited by TheXman
Corrected typo in script
Posted (edited)
3 hours ago, TheXman said:

Does this work for you?  If not, what does the _WinAPI_ShowLastError() message box say?

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)
$bSuccessful = False

$bSuccess = _WinAPI_CreateProcess( _
                                  '', _
                                  '"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" C:\Users\example\Documents\index.pdf', _
                                  0, 0, 0, _
                                  $CREATE_NEW_PROCESS_GROUP, _
                                  0, 0, _
                                  $tStartup, _
                                  $tProcess _
                                  )

If Not $bSuccessful Then ConsoleWrite(_WinAPI_ShowLastError() & @CRLF)

 

Yes that works but I still have concerns as I won't have full input over the input after the application name.

From MSDN, I could specify the Application in the first parameter and the additional command line parameters in the second parameter. Which does work for some other command line parameters, for example:

This works:

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)
$bSuccessful = False

$bSuccess = _WinAPI_CreateProcess( _
                                  'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe', _
                                  '--profile-directory=Default --inprivate', _
                                  0, 0, 0, _
                                  $CREATE_NEW_PROCESS_GROUP, _
                                  0, 0, _
                                  $tStartup, _
                                  $tProcess _
                                  )

If Not $bSuccessful Then ConsoleWrite(_WinAPI_ShowLastError() & @CRLF)

 

But, this seems to ignore the file:

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)
$bSuccessful = False

$bSuccess = _WinAPI_CreateProcess( _
                                  'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe', _
                                  'C:\Users\example\Documents\index.pdf', _
                                  0, 0, 0, _
                                  $CREATE_NEW_PROCESS_GROUP, _
                                  0, 0, _
                                  $tStartup, _
                                  $tProcess _
                                  )

If Not $bSuccessful Then ConsoleWrite(_WinAPI_ShowLastError() & @CRLF)

 

Perhaps this is an idiosyncrasy in Edge and not _WinAPI_CreateProcess...

 

EDIT: Maybe not?

 

Interestingly, this works:

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)
$bSuccessful = False

$bSuccess = _WinAPI_CreateProcess( _
                                  'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe', _
                                  '--inprivate --inprivate', _
                                  0, 0, 0, _
                                  $CREATE_NEW_PROCESS_GROUP, _
                                  0, 0, _
                                  $tStartup, _
                                  $tProcess _
                                  )

If Not $bSuccessful Then ConsoleWrite(_WinAPI_ShowLastError() & @CRLF)

 

but this does not:

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)
$bSuccessful = False

$bSuccess = _WinAPI_CreateProcess( _
                                  'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe', _
                                  '--inprivate', _
                                  0, 0, 0, _
                                  $CREATE_NEW_PROCESS_GROUP, _
                                  0, 0, _
                                  $tStartup, _
                                  $tProcess _
                                  )

If Not $bSuccessful Then ConsoleWrite(_WinAPI_ShowLastError() & @CRLF)

 

but this opens the file? but not in INPRIVATE mode?

#include <WinAPIProc.au3>

$tProcess    = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup    = DllStructCreate($tagSTARTUPINFO)
$bSuccessful = False

$bSuccess = _WinAPI_CreateProcess( _
                                  'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe', _
                                  '--inprivate C:\Users\example\Documents\index.pdf', _
                                  0, 0, 0, _
                                  $CREATE_NEW_PROCESS_GROUP, _
                                  0, 0, _
                                  $tStartup, _
                                  $tProcess _
                                  )

If Not $bSuccessful Then ConsoleWrite(_WinAPI_ShowLastError() & @CRLF)

 

It's looking like maybe _WinAPI_CreateProcess is not parsing the first parameter?

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

  • rcmaehl changed the title to _WinAPI_CreateProcess issues
Posted (edited)

I had a typo in my original script.  I corrected it in my post but you are using the version with the typo.  The boolean var should have beeen "$bSuccessful" throughout.

I think you missed the root cause of your original post's issue or at least what I was trying to point out.  If you have a path with embedded spaces, then the whole path needs to be quoted.  If you look at the code behind the _WinAPI_CreateProcess() UDF, it is using the Win32 CreateProcessW API.  If you carefully read the documentation, it states "If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin (see the explanation for the lpApplicationName parameter)".

I don't see how either of the 2 latest examples you posted will work since the application path is not quoted.  There is a difference between my examples and yours.  Look closely and you will see I have double quotes around the app's path. 

Edited by TheXman

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...