Solved

Help using ShellExecuteEx

Posted on 2000-04-10
8
206 Views
Last Modified: 2010-04-02
I have developed two applications (A, B). What I wanted to do is to run B from A. I used the following code, in the application A, to execute B:

SHELLEXECUTEINFO aShellExecStruct;
      aShellExecStruct.cbSize = sizeof(SHELLEXECUTEINFO);
      aShellExecStruct.fMask = SEE_MASK_NOCLOSEPROCESS;
      aShellExecStruct.hwnd = ::GetDesktopWindow();
      aShellExecStruct.lpVerb = "open";
      aShellExecStruct.lpFile ="B.exe";
      aShellExecStruct.lpParameters = lpszPathName;
      aShellExecStruct.lpDirectory = "c:\\temp";
      aShellExecStruct.nShow = SW_SHOWNORMAL;
      
      
      BOOL aResult = ::ShellExecuteEx(&aShellExecStruct);      
      if (aResult)
      {
            HANDLE aProcHandle = aShellExecStruct.hProcess;
            if (aProcHandle)
            {
                  ::WaitForSingleObject(aProcHandle, INFINITE);
            }
      }
      else
      {
            AfxMessageBox("cannot run B");
      }

The problem I have now is that, after closing B, it should return two variables so I could use them in A. I can't figure out how to do this.
Any help will be greatly appreciated,
regards,
Carlos
0
Comment
Question by:qocarlos
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 2701177
There are several ways of doing IPC, but as you only want to pass 2 variables, I assume named pipes, mailslots and sockets will be quite an overhead.

My suggestions (in order of preference) would be to
- use the registry as an intermediate storage (e.g. HKLM\Software\MyCompany\B) and add the variables as values here or
- let B.exe write the results to a file which can be read by A.

Feel free to ask if you need more information!
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2701206
Have you tried GetExitCodeProcess(...)

some pseudo code (i.e. I've not tested this)

if (aProcHandle)
{
   HANDLE hDupProcess;
   ::WaitForSingleObject(aProcHandle, INFINITE);
   DWORD dwExitCode;
   ::GetExitCodeProcess(aProcHandle,&dwExitCode);

}

- you may have to dupicate the process handle before WaitForSingleObject(...)

some more pseudo code

if (aProcHandle)
{
   HANDLE hDupProcess;
   ::DuplicateHandle(...,aProcHandle,...,hDupProcess,...);
   ::WaitForSingleObject(aProcHandle, INFINITE);
   DWORD dwExitCode;
   ::GetExitCodeProcess(hDupProcess,&dwExitCode);

}

0
 
LVL 86

Expert Comment

by:jkr
ID: 2701227
It's not necessary to 'DuplicateHandle()' - well, I also thought of using the exit code, but as qocarlos wants to pass two variables, it doesn't seem to be an option (hmm, and we still don't know what type of data these variables contain...)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:qocarlos
ID: 2701228
Hi jkr,
Thanks for your answer!
I can write the variables from B (either to the registry or to a file) but I need to do it only in the case that B has be executed from A. If B was executed like a stand-alone application,
I don't want to write this file.
How can I know this? and also, which is the best place in B to save the variables (they belong to the document derived class)?

Carlos
0
 
LVL 3

Author Comment

by:qocarlos
ID: 2701252
About the GetExitCodeProcess(...) function:
The variables I need to get are in double floating format (for instance, numbers 123.732 and -89.12)
I think I can't do this with GetExitCodeProcess or am I wrong?

Carlos
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2701275
oops sorry - I thought he wanted to get the return code (1 value) not two seperate values :)

yup - registry or file - or you could send a message to the other app with the data in
0
 
LVL 86

Expert Comment

by:jkr
ID: 2701339
>>If B was executed like a stand-alone
>>application, I don't want to write
>>this file.

You could do this by passing an additional argument on the command line, e.g.

"b.exe /saveresults"

and check it from within b.exe by using

if ( strstr ( GetCommandLine(), "/saveresults"))
{
 // ...
}

I think the best place to store them is either

HKLM\Software\MyCompany\b

or

HKCU\Software\MyCompany\b

The problem with using a file is that both apps have to agree on a location where to store/look for it...

0
 
LVL 3

Author Comment

by:qocarlos
ID: 2703219
Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question