1

for the last few days or weeks. In Visual Studio C++, I am having hard time with ShellExecuteEx() trying to run an exe with a paramter inside an exe. The way it happens is that I run the debug program in Visual Studio (even outside too). The program would start the other program and it sucessfully run, but it does not generate an .txt file output. I wasn't sure if I used the parameter correctly. Here's the steps I am trying to achieve:

  1. Start MainTest.exe (the exe that will open ScreenCapture.exe)
  2. Cmd screen - MainTest.exe started and prepares to start ScreenCapture.exe using ShellExecuteEx
  3. UAC pops up, run as admin
  4. New cmd screen - ScreenCapture.exe started with parameters and directories
  5. ScreenCapture.exe completes command and closes
  6. CaptureText.txt generated by ScreenCapture.exe placed into same directroy as ScreenCapture.exe .

The problem is that I am not getting CaptureText.txt in my directory or any proof that the parameters are working in this program.

Now, if I run the ScreenCapture.exe without using Test.exe with the parameters, it works and generates the .txt file.

Here's the Code:

#include "stdafx.h"
#include <isostream>
#include <fstream>
#include <string>
#include <Windows.h>

int main()

{

    auto str = _T("C:\\Users\Engrsky\Pictures\Screenshot.png ScreenCapture -l eng")

    SHELLEXECUTEINFO shExInfo = {0};
    shExInfo.cbSize = sizeof(shExInfo);
    shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    shExInfo.hwnd = 0;
    shExInfo.lpVerb = _T("runas");                
    shExInfo.lpFile = _T("C:/Program File (x86)/Test/ScreenCapture.exe");           
    shExInfo.lpParameters = str;                  
    shExInfo.lpDirectory = 0;
    shExInfo.nShow = SW_SHOW;
    shExInfo.hInstApp = 0;  

}

EXAMPLE: Using just Command Prompt, ScreenCapture.exe works perfectly with the parameter. I entered it like this:

Adminstrator: Command Prompt

C:\Program File (x86)\Test> ScreenCapture "C:\\Users\Engrsky\Pictures\Screenshot.png ScreenCapture -l eng"

Then it would sucessfully run and write an output file called ScreenCapture.txt

However, when I tried to do run it using the exe I made(this file). I couldn't get an output generated.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Engrsky
  • 13
  • 1
  • 4
  • 2
    You probably got the .txt file, you just can't find it back. The kind of problem that starts by not using a full path for the file (like baz.txt instead of c:\foo\bar\baz.txt) and made worse by not setting the working directory for the program. Be sure to set lpDirectory to C:/Program File (x86)/Test as well. – Hans Passant Apr 24 '17 at 16:44
  • Thank you for the comment, I added lpDirectory and sadly no changes occured. Could the program be preventing it from being written to the directory? Is there a way I could set the entire program to run as admin to ensure everything had UAC permission. – Engrsky Apr 24 '17 at 18:06
  • I see a bunch of forward slashes that haven't been properly escaped in your string literal. Doesn't this generate compiler warnings at least? – MrEricSir Apr 24 '17 at 18:25
  • @MrEricSir Hello, if I made them into backward slashes...I would get errors *unrecognized character escape sequence* – Engrsky Apr 24 '17 at 18:56
  • @MrEricSir Update: You are right, I had to make all paths double-backward slashes and it helps find the path better. – Engrsky Apr 24 '17 at 18:59
  • Don't write the answer in the question, thanks. – Lightness Races in Orbit Apr 24 '17 at 19:26

1 Answers1

1

From the MSDN doc for the ShellExecuteEx function, you first need to initialize COM. The example there is to use:

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
Phil Brubaker
  • 1,257
  • 3
  • 11
  • 14
  • I wasn't sure how to use this function, I added this function to the beginning of the code before `auto str = etc etc` and I do not see a change. – Engrsky Apr 24 '17 at 18:03
  • Could be dealing with a few things here... one of which is 64-bit vs 32-bit. Read some of the comments here: [How to run application which requires admin rights from one that doesn't have them](http://stackoverflow.com/questions/11586139/how-to-run-application-which-requires-admin-rights-from-one-that-doesnt-have-th) – Phil Brubaker Apr 24 '17 at 18:28
  • You should also be sure to initialize all fields of that `SHELLEXECUTEINFO` structure properly - the [doc](https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784(v=vs.85).aspx) I'm looking at shows more fields than it appears you're initializing. – Phil Brubaker Apr 24 '17 at 18:32
  • Thank you! It works! I had to raise the entire program application *UAC Execution level* to *requireAdministrator*. I found it through your link and related. I used the method for VS2015, from [here](http://stackoverflow.com/questions/8915744/is-it-possible-for-the-executable-to-ask-for-administrator-rights-windows-7?noredirect=1&lq=1). *Project's Propeties -> Linker -> Manifest File -> UAC Execution level -> requireAdministrator* – Engrsky Apr 24 '17 at 19:10