Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-03-2006, 07:01 PM   #1 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
windows console app

Is there a way a console app can query if it was run from inside a DOS box as opposed to being double clicked?

The reason is that I'm tired of putting getch() at the end of all my quick test programs to avoid the printed answers from not being seen. If I open a DOS box and run the app from there, no problems. But being lazy, if I double click to run it, it runs, prints out info, and closes. It would be nice to be able to detect and only put in the getch() if the app was double clicked.

Thanks!
QUantumAnenome is offline   Reply With Quote
Old 01-28-2006, 03:44 PM   #2 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
I think it would be more trouble than its worth

Just get a good IDE and run your programs from inside it.
fp_unit is offline   Reply With Quote
Old 02-01-2006, 05:03 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
the compiled binary has 2 sections in the beginning.
MZ and PE the issue is that the PE code is the windows executable and MZ the dos.

If you run the app thru dos only the MZ gets executed and requests for windows "This program cannot be run in DOS mode."

There's only one way to execute PE code and that is thru the internal START function.
Both the cmd and gui use this to execute a program but it does make a difference in some cases of the gui where you can check which handle executed it.
DJMaze is offline   Reply With Quote
Old 03-05-2006, 06:33 PM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
Update: On Windows NT however you can see if the application is called from a console window or the GUI.

Code:
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                             LPSTR lpCmdLine, int nCmdShow) {

    LPWSTR *szArglist;
    int nArgs;
    int i;

    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    if (szArglist != NULL)
    {
        for (i=0; i<nArgs; i++) {
            if (szArglist[i] == "WIN") {
                print("We have a gui\n");
                LocalFree(szArglist);
                return 0;
            }
        }
    }
    print("We have a console\n");

    // Free memory allocated for CommandLineToArgvW arguments.
    LocalFree(szArglist);

    return 0;
}
DJMaze is offline   Reply With Quote
Old 03-05-2006, 07:10 PM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
here's another way to go in Windows
Code:
int WINAPI WinMain (HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine,
                     int nCmdShow)
{
// GUI app
}

int main(int argc, char** argv)
{
// console app
}
DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
console app full screen HELP! Manan Windows 2 09-25-2005 08:48 PM
Pirated software used to create help content in Microsoft's Windows XP sde Code Newbie News 3 11-17-2004 03:14 PM
The Windows of two minds creed Windows 4 11-10-2004 08:37 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 01:41 AM
c++.net console app ? sde Standard C, C++ 5 03-13-2003 07:07 AM


All times are GMT -8. The time now is 12:23 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting