2

I'm learning C++. I wonder is any C++ application have HWND. Example bellow app, with no window created. If it have, how I can get its HWND? Thank you very much!

#include <windows.h>

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
    MSG msg;
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
user2289150
  • 31
  • 1
  • 4
  • Related: [If it's a console app you can get the host window's `HWND` thus](http://support.microsoft.com/kb/124103). – ta.speot.is Apr 21 '13 at 05:54
  • @ta.speot.is, I have a feeling that was written before `GetConsoleWindow()`. – chris Apr 21 '13 at 06:25
  • Learning C++ before Windows Programming is a good idea. Or if you want to learn using WinAPI probably c will be enough if you don't need C++ (OOP) features. – Atiq Rahman Apr 21 '13 at 06:33

2 Answers2

4

"I'm learning C++. I wonder is any C++ application have HWND." The shortest answer is no. HWND is a defined type in a library used to write Windows applications. C++ is a language that can be used to do that as long as you have library that gives you functions (including HWND type.)

You can write programs for CMD prompt or for Unix which have nothing to do with Windows. Try this C style program. Copy text below to a.cpp file, and compile it to generate a.exe:

#include <stdio.h>
int main()
{
    printf( "Hello world\n" ) ;
    return 0 ;
}

When you run cmd, change directory to where a.exe is, and run a.exe then you will see:

Hello world

If you plan on learning C++ you don't need to write Windows applications. You can write CMD or Linux programs. Find a good book on the C++ subject. Good luck!

Grzegorz
  • 3,207
  • 3
  • 20
  • 43
  • Under Windows, even though you might not be aware of it, this program WILL have a `HWND` associated with it. It's showing the output somewhere, that "somewhere" is a window, and that window has a HWND. – MSalters Apr 21 '13 at 23:54
3

You need to create one.

check out CreateWindowEx and ShowWindow

yngccc
  • 5,594
  • 2
  • 23
  • 33
  • That mean if I dont create Window, my app can't have HWND? – user2289150 Apr 21 '13 at 05:53
  • 1
    @user2289150: There are 2 possibilities. Either your Windows application is a GUI app, in which case there can be 0, 1 or many HWND's - since you create them -, or it's a console app, in which case you generally get the HWND from CMD.EXE. – MSalters Apr 21 '13 at 23:56