Solved

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

Posted on 2007-11-27
17
268 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
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 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 100 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 100 total points
ID: 20358602
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 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: 20358792
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 100 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 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 100 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 300 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 100 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 300 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now