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

x
?
Solved

no console window while running a program on windows -  making it a win32 program a must? how?

Posted on 2007-11-27
17
Medium Priority
?
319 Views
Last Modified: 2008-02-26
I have programs like this one


#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;




int main ()
{

      string line;
      ifstream file("lines.txt");
      getline(file, line);
      cout << line << endl;
      
      return 0;
}



but it pops  a console window while ran.

is making it a win32 program a solution?

how do I do it with generic c++ code , and compile it with mingw?
0
Comment
Question by:Troudeloup
[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
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 20358529
A console application is a valid Win32 application, just one that runs in a console. If you mean to create a Win32 GUI app, you need a 'WinMain()' instead, e.g.

#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;



int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{

      string line;
      ifstream file("lines.txt");
      getline(file, line);
      cout << line << endl;
     
      return 0;
}

However, that will run without displaying a window, since the GUI code is missing. The probably simplest skeleton for such an app would be



#include <windows.h>
 
#pragma comment(lib,"user32.lib")
 
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
  switch(Message)
  {
     case WM_CLOSE:
        DestroyWindow(hwnd);
     break;
     case WM_DESTROY:
        PostQuitMessage(0);
     break;
     default:
        return DefWindowProc(hwnd, Message, wParam, lParam);
  }
  return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASSEX WndClass;
  HWND hwnd;
  MSG Msg;
 
  g_hInst = hInstance;
 
  WndClass.cbSize        = sizeof(WNDCLASSEX);
  WndClass.style         = NULL;
  WndClass.lpfnWndProc   = WndProc;
  WndClass.cbClsExtra    = 0;
  WndClass.cbWndExtra    = 0;
  WndClass.hInstance     = g_hInst;
  WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  WndClass.lpszMenuName  = NULL;
  WndClass.lpszClassName = g_szClassName;
  WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 
  if(!RegisterClassEx(&WndClass))
  {
     MessageBox(0, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }
 
  hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE,
     g_szClassName,
     "The title of my window",
     WS_OVERLAPPEDWINDOW,
     CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
     NULL, NULL, g_hInst, NULL);
 
  if(hwnd == NULL)
  {
     MessageBox(0, "Window Creation Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }
 
  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);
 
  while(GetMessage(&Msg, NULL, 0, 0))
  {
     TranslateMessage(&Msg);
     DispatchMessage(&Msg);
  }
  return Msg.wParam;
}

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 20358533
You are streaming to standard out -- this will require a console windows!

If you don't want a console window you will need to create your project as a Win32 application.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 20358602
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 20358657
>>>> You are streaming to standard out -- this will require a console windows!
In debug mode you could use

  ostringstream oss;
  oss << line << endl;

  OutputDebugString(oss.str().c_str());

it would show the message in the output window of Visual Studio.

If not using the VS or if compiling with a different compiler, you can stream to a logfile rather than cout or

    ostringstream oss;
    oss << line << endl;
    MessageBox(NULL, oss.str().c_str(), "", MB_OK);

Regards, Alex


0
 

Author Comment

by:Troudeloup
ID: 20358809
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;



int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{

      string line;
      ifstream file("lines.txt");
      getline(file, line);
      cout << line << endl;
     
      return 0;
}









given this, is it possible to pop a msgbox?



also, does it create a tab on the start bar?

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 20358821
>> given this, is it possible to pop a msgbox?
MessageBox(NULL, line.c_str(), "", MB_OK);

>> also, does it create a tab on the start bar?
No (I don't think so anyway!)
0
 

Author Comment

by:Troudeloup
ID: 20358831
also, is it possible to display a small cute icon on the clock bar with my own alter alternating graphics?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1200 total points
ID: 20358832
>>given this, is it possible to pop a msgbox?

Sure, just use

#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;



int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{

      string line;
      ifstream file("lines.txt");
      getline(file, line);
      cout << line << endl;

      MessageBox(NULL,"Test","Hi there!",MB_OK);
     
      return 0;
}

>>does it create a tab on the start bar?


While the message box is showing, yes.
0
 

Author Comment

by:Troudeloup
ID: 20358837
is it because once I click the messagebox, the process will end?


because i'd like to have the tab showing permanently when the code is running.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 20358852
>> While the message box is showing, yes.
Oooh, yes you're right -- I've never noticed that before :)
0
 

Author Comment

by:Troudeloup
ID: 20358867
uh, are you being sarcastic?


or maybe you never pop any box?

but then what did write ?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1200 total points
ID: 20358875
>>is it possible to display a small cute icon on the clock bar

Yes, but that's a way more difficult task, that would be


#include <windows.h>
 
#pragma comment(lib,"user32.lib")
 
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
 
BOOL    TaskBarAddIcon  (   HWND    hwnd, 
                            UINT    uID,
                            HICON   hicon, 
                            LPSTR   lpszTip
                        ) 
{ 
    BOOL            bRC; 
    NOTIFYICONDATA  nid; 
 
    nid.cbSize              =   sizeof  (   NOTIFYICONDATA); 
    nid.hWnd                =   hwnd; 
    nid.uID                 =   uID; 
    nid.uFlags              =   NIF_MESSAGE | NIF_ICON | NIF_TIP; 
    nid.uCallbackMessage    =   WM_USER +   uID; 
    nid.hIcon               =   hicon; 
 
    if  (   lpszTip) 
            lstrcpyn    (   nid.szTip,  lpszTip,    sizeof  (   nid.szTip)); 
    else 
            nid.szTip[ 0]   =   '\0'; 
 
    bRC     =   Shell_NotifyIcon(   NIM_ADD,    &nid); 
 
    if  (   hicon) 
            DestroyIcon (   hicon); 
 
    return( bRC); 
}
 
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HICON hicon;
 
  switch(Message)
  {
     case WM_CREATE:
 
     hicon  =   LoadIcon    (   g_hInst,    MAKEINTRESOURCE (   1));
 
     TaskBarAddIcon (   g_hWnd, 1,  hicon,  "Test");
     
     break;
     
     case WM_ACTIVATE:
 
     OutputDebugString("WM_ACTIVATE!\n");
 
     break;
     case WM_CLOSE:
        DestroyWindow(hwnd);
     break;
     case WM_DESTROY:
        PostQuitMessage(0);
     break;
     default:
        return DefWindowProc(hwnd, Message, wParam, lParam);
  }
  return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASSEX WndClass;
  HWND hwnd;
  MSG Msg;
 
  g_hInst = hInstance;
 
  WndClass.cbSize        = sizeof(WNDCLASSEX);
  WndClass.style         = NULL;
  WndClass.lpfnWndProc   = WndProc;
  WndClass.cbClsExtra    = 0;
  WndClass.cbWndExtra    = 0;
  WndClass.hInstance     = g_hInst;
  WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  WndClass.lpszMenuName  = NULL;
  WndClass.lpszClassName = g_szClassName;
  WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 
  if(!RegisterClassEx(&WndClass))
  {
     MessageBox(0, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }
 
  hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE,
     g_szClassName,
     "The title of my window",
     WS_OVERLAPPEDWINDOW,
     CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
     NULL, NULL, g_hInst, NULL);
 
  if(hwnd == NULL)
  {
     MessageBox(0, "Window Creation Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }
 
  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);
 
  while(GetMessage(&Msg, NULL, 0, 0))
  {
     TranslateMessage(&Msg);
     DispatchMessage(&Msg);
  }
  return Msg.wParam;
}

Open in new window

0
 

Author Comment

by:Troudeloup
ID: 20358885
uhhhh how about just a boot-to-go tab in the main bar?

(i'll, like, take that for now ;p)
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 20358900
>> uh, are you being sarcastic?
I don't do much 'Windows' GUI programming -- I mostly develop x-platform embedded components, we don't go in much for pop-up windows :)
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1200 total points
ID: 20358907
A tab in the task bar still requires to use a minimalistic window like the code in http://www.experts-exchange.com/codeSnippetPopup.jsp?csid=4519 does.
0
 

Author Comment

by:Troudeloup
ID: 20358929
ok i have to ask a lot Qs about that snippet ;p


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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