2

I am a newbie when it comes to using WinAPI. I am following a tutorial where I found a code snippet. The snippet demonstrates a basic program. I am posting my full code below:

#include "a.h"
#include "windows.h"

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX WndClsEx;
    WndClsEx.cbSize      = sizeof(WNDCLASSEX);
    WndClsEx.style       = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc = WndProcedure;
    WndClsEx.cbClsExtra  = 0;
    WndClsEx.cbWndExtra  = 0;
    WndClsEx.hInstance   = hInstance;

    return 0;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
    default:
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}

I don't get any error while running the code from Qt Creator. However, when running it no windows appear but the output console shows:

"MyProgram.exe exited with code 0"

What might cause this?

3
  • 1
    This is not a QT program. It is not a native Win32 program either, too much code missing. You'll need to RTFM. Commented Mar 17, 2012 at 19:10
  • Did you by any chance mean "Qt Creator"? As in, the IDE that you use? Because Qt and "Qt Creator" are two different things. And as Hans says, the code you're showing has nothing to do with Qt. Commented Mar 17, 2012 at 19:12
  • Thanks for a quick reply... I don't know actually what should I do.. I am using Qt SDK, where Qt creator is an embedded part here. I want to do winapi programing using Qt. Isn't it possible ? Would you kindly suggest me something ? Commented Mar 17, 2012 at 19:20

2 Answers 2

3

I am posting my full code below:

Your code looks a lot like standard Win32 but it is missing a lot of code.

For example this very simple test.cpp file contains a full working Win32 application:

#define STRICT
#include <windows.h>

long PASCAL WndProc (HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpszCmdParam, int nCmdShow)
{
   static char szClassName[] = "Hello World";
   MSG         msg;
   WNDCLASS    wndclass;

   memset(&wndclass, '\0', sizeof(wndclass));

   wndclass.style          = CS_HREDRAW|CS_VREDRAW;
   wndclass.lpfnWndProc    = WndProc;
   wndclass.cbClsExtra     = 0;
   wndclass.cbWndExtra     = 0;
   wndclass.hInstance      = hInstance;
   wndclass.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
   wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
   wndclass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
   wndclass.lpszMenuName   = 0;
   wndclass.lpszClassName  = szClassName;
   RegisterClass (&wndclass);

   // create a new window 
   HWND hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                              szClassName,
                              "My Hello World Window",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);

   ShowWindow (hwnd, nCmdShow);

   while (GetMessage (&msg, NULL, 0, 0)) {
      TranslateMessage (&msg);
      DispatchMessage (&msg);
   }

   return msg.wParam;
}

long APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
       case WM_DESTROY:
          PostQuitMessage (0);
          return 0;
    }

    return DefWindowProc (hwnd, message, wParam, lParam);
}

It can be compiled and linked from the command line:

C:\TEMP>cl test.cpp user32.lib gdi32.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj
user32.lib
gdi32.lib

The resulting test.exe can be run and it will display a window:

C:\TEMP>test.exe
Sign up to request clarification or add additional context in comments.

Comments

1

As Hans Passant suggests in his comment, you're missing a lot of code. I don't know from which tutorial you copied this snippet, but surely there must be more code there.

For example, you have not registered it, nor created the actual window, you're not showing it and (as @rodrigo mentioned) you're missing the message loop. This example on MSDN illustrates what that all would look like.

And yes, you can perfectly develop applications in Qt Creator without actually using Qt for your user interface. I would however not dismiss it. Since you have all the tools at your disposal, have a look at Qt itself as well. You might just like it.

4 Comments

And most importantly, he does not have a message loop. Without it, even if he did all the other things, the program would terminate just after that, and the window would disappear.
@rodrigo Ah yep, implicit in the example I link to, but good to mention. I've added it.
Yes you are right. I have missed lots of code. Later i noticed some more are missing those are actually present in the tutorial. Thank you.
Great, glad that helped you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.