Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ execute command line - errorcode

Posted on 2015-01-27
3
Medium Priority
?
109 Views
Last Modified: 2015-01-28
Hi Experts,

How can I run an exe from code and get the value of errorcode (as we would in a batch file)?

Thanks!
Mike
0
Comment
Question by:thready
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40574421
The ise is to evaluate 'GetExitCodeProcess()', e.g. like

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd, BOOL bShow)
{
	STARTUPINFO         si;
	PROCESS_INFORMATION pi;

	BOOL                bRes;

	DWORD               dwCode  =   0;

	MSG				   msg;

	ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

	si.cb           =   sizeof  (   STARTUPINFO);
	si.dwFlags      =   STARTF_USESHOWWINDOW;
	si.wShowWindow  =   bShow ? SW_SHOWNORMAL : SW_HIDE;

	bRes    =   CreateProcess   (   NULL,
								   pszCmd,
								   NULL,
								   NULL,
								   TRUE,
								   NORMAL_PRIORITY_CLASS,
								   NULL,
								   NULL,
								   &si,
								   &pi
							   );

	while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
																   &pi.hProcess,
																   FALSE,
																   INFINITE,
																   QS_ALLINPUT
															   )
		   )
		   {
			   while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
					   {
						   DispatchMessage     (   &msg);
					   }
		   }

	GetExitCodeProcess  (   pi.hProcess,    &dwCode);

	CloseHandle (   pi.hProcess);
	CloseHandle (   pi.hThread);

	return  (   dwCode);
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 40574422
Sorry about the code formatting, but you'basically call this function with the command line to execute and the the return value (or %ERRORCODE%) back in return.
0
 
LVL 1

Author Closing Comment

by:thready
ID: 40575763
Awesome!  :-)
Thanks!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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