Sign in to follow this  

Opening and closing a program programatically

Recommended Posts

I have a windows application (which does not create a window) I execute it like this.
   HINSTANCE m_HinstanceCallLogger = ShellExecute( m_WinBase, "open", "../CallLogger/CallLogger.exe", "", "", 0 );

How do I close this program (elegantly) when the calling program exits?

Share this post

Link to post
Share on other sites
You'll need to use ShellExecuteEx, and the use WaitForSingleObject with a timeout. See below for some sample code which could be used to poll the application for running status. This is fairly inefficient because of the 0 timeout but it should be simple to modify.

class SafeShellExecInfo
SafeShellExecInfo(const string& file, const string& parameters, const string& directory)
: file_(file), parameters_(parameters), directory_(directory), handleValid_(false)
// Prepare the raw structure
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.hwnd = NULL; // no parent window
ShExecInfo.lpVerb = NULL; // not trying to "open" a file
ShExecInfo.lpFile = file_.c_str(); // set the file to execute
ShExecInfo.lpParameters = parameters_.c_str(); // set the parameters to the executable
ShExecInfo.lpDirectory = directory_.c_str(); // set the working directory
ShExecInfo.nShow = SW_HIDE; // don't give child processes windows
ShExecInfo.hInstApp = NULL; // don't care about this value

void shellExecute()
ShellExecuteEx( &ShExecInfo ); // start the process
handleValid_ = true;

bool isStarted()
return handleValid_; //

bool isRunning()
assert(handleValid_ == true);
return WaitForSingleObject(ShExecInfo.hProcess,0) == WAIT_TIMEOUT;

bool handleValid_;
const string file_;
const string parameters_;
const string directory_;

Share this post

Link to post
Share on other sites
Using a registry key is basically the same thing as using a file.

Memory mapping seems to somplicated for what I am trying to do.

I was thinking of something as simple as send and recv without actually needing all that winsock code.

Isnt there a way to set envrionment veriables or something? That way I could transfer data easily without too much code.

send would become set_env_variable
recv would become read_env_variable

Share this post

Link to post
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

Sign in to follow this