Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an .exe file that install .dll files to your machine. the conditions are only that if it success if prompt a message box saying .dll file has been installed correctly and if not it prompt a error message box.

But while i run this program from ECHO i do not want to prompt message box but i want to show a message in echo screen saying installed or error but again if it's run without echo it has to prompt message box, so there has to be something like switch which can be used to check if it's run directly or from echo and then able to prevent message box.

Cheers,
Mohit
Posted

Write your logic to check the parent process in your exe.if the parent process name is cmd.exe then you can put your code to write a message on console else show message box.
 
Share this answer
 
A command line argument seems the cleanest solution, you can use GetCommandLine[^] from your exe file.

e.g.
Starting the program like myprog.exe will show a messagebox, but
myprog.exe -console will use console output.

When opening myprog.exe via the explorer, no command line arguments are added so you'll get a messagebox if you start it manually.
 
Share this answer
 
Comments
mohit`12 1-Feb-11 9:32am    
okay i like this idead, but when i use getcommandline (like this m_lpCmdLine= GetCommandLineW();)
it returns the path of my exe file (like "C:\Documents and Settings\*****\Desktop\Setup_Csb\Debug\Setup.exe" )now when i run this from command line (cmd.exe) and put some arguments let's say setup.exe -console it still doesn't read the arguments .

if you can provide me better explanation or some little example that would be better.

Thank you
[no name] 1-Feb-11 11:34am    
The command line includes the file name of the program, it is exactly the text you type in the cmd-window.
To split a (unicode, since you use GetCommandLineW in your reply) command line to the separate arguments, you can use the CommandLineToArgvW function; http://msdn.microsoft.com/en-us/library/bb776391%28v=VS.85%29.aspx. There's an example under the link too.

edit; just saw you added an answer and make use of this function, so the problem seems solved :)
I assume what you mean is that if the console is active, then you want to show your messages there but if there is no console (as happens when they run your app directly from explorer), then you want to show a messagebox. You could call GetConsoleWindow and if it returns NULL, then you show your info/error messages through MessageBox, else you can use cout or printf.
 
Share this answer
 
Comments
[no name] 31-Jan-11 12:51pm    
As far as I know, GetConsoleWindow does not work with the cmd.exe that opened a program, but only with a console window associated with the program itself. In the case of a GUI program, there is no console allocated by default.
Nish Nishant 31-Jan-11 13:28pm    
Yeah, exactly. My guess is the OP has a console app (that also has a GUI). So when he runs it directly from the console, there is a console attached (and he can use it). But when launched from explorer, it's possible that the console portion opens and closes and the main dialog is brought up (possibly in a separate thread).
Do an AttachConsole(-1).

If run from a command prompt, it will succeed and attach to the command prompt.
If successful, call GetStdHandle(STD_OUTPUT_HANDLE) and use WriteFile on this handle to write to the console.
If AttachConsole failed, you can use MessageBox to display the message in a UI.
 
Share this answer
 
Solution:
LPWSTR *szArglist;
int nArgs =0;
wchar_t wText [MAX_PATH] = {0};

szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
for (int i = 0; i < nArgs; i++)
{
if(nArgs==2)// if there is any extra argument other than calling .exe file if not console will be false
console= TRUE; // CONSOLE APPLICATION ACTIVATED
}
// then in displaying messagebox
if(!console)
MessageBox(0,TEXT("success"),TEXT(" "),MB_OK);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900