1

I have developed a UI using visual C++, application type is diolog based.

For executing another application (CLI) from my UI,I used ShellExecute() function. This application finishes its job and disappears as the code runs in my UI.

From UI side, how to know whether application has done its job successfully?

Can any one please help to implement communication between My visual C++ code and another foreign application opening from that code.

Here is my code snippet for executing application,

CString cmd = "/C command parameters";
HINSTANCE hinst = ShellExecute(0, "open", "bin\\My application.exe", cmd, 0, SW_SHOW);

In My application.exe, there will be printing "successful" in last line if succeeded its operation, Is there any method to read lines from UI side.

TiYan
  • 125
  • 2
  • 9
  • Find the return code of that app to determine if it has finished the job successfully. – Asesh Nov 16 '18 at 05:20
  • Use `CreateProcess` + `WaitForSingleObject` https://stackoverflow.com/a/42544/4603670 if you want to wait for the process to finish, or `ShellExecuteEx`... – Barmak Shemirani Nov 16 '18 at 05:55
  • 2
    Using `CreateProcess` you can also [redirect the output](https://learn.microsoft.com/en-us/windows/desktop/procthread/creating-a-child-process-with-redirected-input-and-output) of `My application.exe` to a pipe of your own application, so you can read the output. – zett42 Nov 16 '18 at 08:11
  • `CString cmd = "param1= " + variable1 + " param2= " + variable2;` I have given second parameter of CreateProcess() like this.But i got error CString cannot be converted to LPSTR.Can any one please suggest some solution.How to give command like this. – TiYan Nov 16 '18 at 10:10
  • `cmd.GetBuffer(0)` I have found a solution,Thank you all for helping and supporting. I got the application return code from the function`GetExitCodeProcess(processInfo.hProcess, &exitCode);` – TiYan Nov 16 '18 at 10:13
  • 1
    You probably mean `"/C command parameters";`instead of `"\C command parameters";`, please confirm and/ or correct. You may [edit] your question. – Jabberwocky Nov 16 '18 at 13:13
  • 3
    `cmd.GetBuffer(0)` is only a solution, if you understand what [CString::GetBuffer](https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/csimplestringt-class#getbuffer) does, and which requirements it places on you. If you don't, you just created a new bug. – IInspectable Nov 16 '18 at 13:34

1 Answers1

-1

Return value can cast to int.

  Type: HINSTANCE

If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.

  Return code                                 Description
   0                   The operating system is out of memory or resources.
  ERROR_FILE_NOT_FOUND The specified file was not found.
  ERROR_PATH_NOT_FOUND The specified path was not found.
  ERROR_BAD_FORMAT The .exe file is invalid (non-Win32 .exe or error in .exe image).
  SE_ERR_ACCESSDENIED The operating system denied access to the specified file.
  SE_ERR_ASSOCINCOMPLETE The file name association is incomplete or invalid.
  SE_ERR_DDEBUSY The DDE transaction could not be completed because other DDE transactions were being processed.
  SE_ERR_DDEFAIL The DDE transaction failed.
  SE_ERR_DDETIMEOUT The DDE transaction could not be completed because the request timed out.
  SE_ERR_DLLNOTFOUND The specified DLL was not found.
  SE_ERR_FNF The specified file was not found.
  SE_ERR_NOASSOC 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.
  SE_ERR_OOM There was not enough memory to complete the operation.
  SE_ERR_PNF The specified path was not found.
  SE_ERR_SHARE A sharing violation occurred.

See the link.

karel
  • 5,489
  • 46
  • 45
  • 50
  • I hope return value is whether shellexecute() is failed to open My appliaction.exe not the return value of My application.exe. – TiYan Nov 16 '18 at 05:57
  • 1
    @TiYan Return value only indicates whether `ShellExecute` is successful. With this method you don't know if application did its job successfully. Better use `CreateProcess` instead. – zett42 Nov 16 '18 at 08:14
  • @zett42 Thank you, But when i gives second parameter of CreateProcess() as cmd `CString cmd = "\C command parameters";` I got error something like Cstring cannot be converted to LPSTR, Can you suggest a solution. – TiYan Nov 16 '18 at 10:05
  • 1
    Solution: Read [working with strings](https://learn.microsoft.com/en-us/windows/desktop/learnwin32/working-with-strings), followed by the documentation for [CreateProcessW](https://learn.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessw). Browsing through the [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/1889329) is never wrong, if compiler errors are puzzling to you. – IInspectable Nov 16 '18 at 10:23