Using the main( ) function returned value to execute another program using Windows Powershell


Using the main( ) function returned value to execute another program using Windows Powershell

In every program we write the int main() function is the most important function among all the functions.When the compiler begins compiling the program it will start from the main() function.In fact every other sub-program is somehow linked to the main() function,for instance the first function called in every program is linked from main().The functions which the main() calls interact with main() by returning a value of different types or sometimes it returns void type;meaning it returns nothing.If the function return a value it can be used to perform certain other operations.And we know that since main() is also a function,it is a common norm for the main() to return an int type value to signify the end of the program;it can also return the macros EXIT_SUCCESS or EXIT_FAILURE.For instance consider the code below.

int main( )
{

return EXIT_SUCCESS ; ///returns exit success code
}

int main( )
{

return EXIT_FAILURE ; ///return exit failure code
}

int main( )
{
return 0 ; //returns 0; can be also any other integer value
}

The macro EXIT_SUCCESS or EXIT_FAILURE is some integral value and these values are machine dependent.If the main() returns EXIT_SUCCESS or EXIT_FAILURE or some other value then the OS will accept the value.We can use this value to carry out some other operations.In Windows one of these operations is calling many programs consecutively.





To use the value returned by main() function we have to get the value from the OS by calling some other function and then we can do whatever we desire with the value.But note such process cannot be carried out in C++ IDE or using C++,in Windows there is a .NET framework known as ‘Windows Powershell‘ .And using this framework and it’s scripting language we can call many programs consecutively without having to run the program directly by us.

Of course I’ll be showing here how to use the Windows Powershell and also run three(you can add more) C++ .exe file consecutively.But before we start using the Windows Powershell let’s get some things ready first.

First::Getting the .exe files.

To call a program we will need the .exe file so let us create three .exe file first.Open your C++ IDE ,create a new project and name it pro1.Copy and paste the code given below in your main.cpp file.

int main( )
{
cout<< “pro1.exe called \n” ;

return 100 ;
}

This program will return 100 as the existing code(returned value).Run the program and copy the prog1.exe found in “../bin/debug” into another directory say “C:\proRet_valueTest” ;this directory will be useful later.

For the second program name the project as ‘prog2’ and copy and paste the code shown below.

int main( )
{
cout<< “Second program prog2.exe called \n” ;

return 50;
}

 
Run this program and copy the prog2.exe and paste it into the directory “C:\proRet_valueTest”.
 
Name the third program as prog3 and copy and paste the code given below.
 

int main( )
{
cout<< “Last program pro3.exe called \n” ;

return 0;
}

 
Run the program and copy and paste the prog3.exe file in the directory “C:\proRet_valueTest”.Now the first step is complete.In the second step we will write a simple program using Windows Powershell scripting language -language which the Powershell can understand- to call the three .exe files consecutively.


 


Stop wasting time,earn money($$$) from your website-Join Now!

Steps 2::Using Windows Powershell calling the three .exe files
consecutively.

Powershell scripting language also consist of if() and else() statement with a meaning similar to the C++ language if() and else() statement.So you can easily grasp the meaning of the code which will be shown here.Before writing the full code however let’s see some important entity that will help in writing the working code.

i)To run any program with Powershell we will use ‘&’ character and the directory of the .exe file with the name of the .exe file included.This means in our case to run prog1.exe we will write,

   & C:\proRet_valueTest\porg1.exe

for the second program to execute we will write

  & C:\proRet_valueTest\porg2.exe

and for the third program,

  & C:\proRet_valueTest\porg3.exe

 
ii)To get the value returned by each program we will use ‘$LastExitCode‘.This element will store the returned value of the program,so to get the returned value simply write it after running the program .For instance,

‘Note ‘->’ is part of the Powershell command so do not include it’

 ->& C:\proRet_valueTest\porg1.exe
 ->$LastExitCode

to this the Powershell will output,

->prog1.exe called
->100 &nbsp  ///100 becasue prog1.exe returns 100

iii)The element ‘-eq’ is same as ‘==’ operator of C++.That means -eq in Powershell compares two values for equalness.
 
iv)The entity ‘write-host’ is same as ‘cout’ of C++.This means ‘write-host’ will output whatever follows(string or number) under ” “(double quotation) on the Powershell screen.
 
After knowing these five elements we can write the required program.The program and each line of code explanation is given below.
 
if( 1 ){ & C:\function\prog1.exe
/*we call if() and the argument 1 is always true so the next line is
executed that means prog1.exe is executed */

if( $LastExitCode -eq 100 ) {
/* The value returned by porg1.exe is stored in $LastExitCode and we compare it with 100 ,if true the next line is executed else the program will exit because we have not given any else() statement case*/

& ‘C:\function\prog2.exe’
/* the prog2.exe is executed */

} if( $LastExitCode -eq 50 ) {
/*After prog2.exe is executed the value returned is stored in $LastExitCode and it is compared with the value 50,if it is true the prog3.exe is executed*/

& ‘C:\function\prog3.exe’

}Else { write-host “Last program not called” }
/*If $LastExitCode is not equal to 50 then the powershell will output “Last program not called” */

}
 
That’s the code required,isn’t it simple? Now open up your Windows Powershell and copy the code and paste it exactly as it is shown below in your powershell screen.
 

if( 1 ){ & C:\function\prog1.exe

if( $LastExitCode -eq 100 ) {

& ‘C:\function\prog2.exe’

} if( $LastExitCode -eq 50 )
{

& ‘C:\function\prog3.exe’

}Else { write-host “Last program not called” }

}

 
If everything goes well you will see the three strings,

prog1.exe called
Second program prog2.exe called
Last program prog3.exe called

being written on the screen after every short intermediate.However if you see any error message then check your code again.
 
The topic discussed in this post is the simplest use of the value that is returned by main() function.You can perform more advance operation using the Windows Powershell but for that you will neeed more knowledge of the scripting language of Windows Powershell.Well Google is always there to help you out and also if you need any help comment below or mail me.
 

Related Link

->Preprocessor directives of C and C++ :#include ,#if ,#ifndef ,#define ,#endif ,#else ,#undefine, #error, #line and #pragma

->Using Ellipsis (…) and initialization_list