Help Beginner

</h1>Windows Programming Beginner</h1>

I started learning about <windows.h> some days ago.I followed a youtube tutorial which explained how to create an empty window.

This is the code.

#include<windows.h>

LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInst , HINSTANCE hPrevInst ,LPSTR args , int ncmdshow)
{
WNDCLASSW wc = {0};

wc.hbrBackground = (HBRUSH)COLOR_WINDOW ;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"myWindowClass";
wc.lpfnWndProc = WindowProcedure ;
wc.style = CS_HREDRAW;

if(RegisterClassW(&wc))
return -1;

CreateWindowW(L"myWindowClass",L"My Window",WS_EX_OVERLAPPEDWINDOW | WS_VISIBLE,100,100,500,500,NULL,NULL,NULL,NULL);
MSG msg = {0};
while(GetMessage(&msg,NULL,NULL,NULL))
{

TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0 ;
}

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
switch(msg)
{
case WM_DESTROY :
PostQuitMessage(0);
break;

default:
return DefWindowProcW(hWnd,msg,wp,lp);
}

}


THe code reurns -1. The Window fails to register. I googled it and i followed the suggestion to add wc.style.

It still returned -1. Please help

PS> Suggest Better resources to learn :) thanks
Hi!
Which IDE do you have installed currently? MS Visual Studio or CodeBlocks should have such a properly working template by default.
My suggestion: Download one of them and work with the template. With CodeBlocks, my cross-platform favourite IDE, it's "Win32 Application". (Download link with the GCC compiler included: http://sourceforge.net/projects/codeblocks/files/Binaries/16.01/Windows/codeblocks-16.01mingw-setup.exe)

Good luck!
I have not seen the Code::Blocks template but I would not use the VS template. It's too complicated for beginners. You can try this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>


void ShowLastWinError()
{
  DWORD err = GetLastError();
  TCHAR *msg;
  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, err, 0, (LPTSTR)&msg, 1024, NULL);
  MessageBox(NULL, msg, _T("Windows Error"), MB_OK|MB_ICONERROR);
  LocalFree((HLOCAL)msg);
}

LRESULT CALLBACK WndProc (HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
  static HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;

  switch (msg)
  {
    case WM_CREATE:
    {
      return 0;
    }
    case WM_COMMAND:
    {
      return 0;
    }
    case WM_DESTROY:
    {
      PostQuitMessage (0);
      return 0;
    }
  }

  return (DefWindowProc (hWnd, msg, wParam, lParam));
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
  TCHAR szClassName[] = _T("Template");
  TCHAR szWindowName[] = _T("Template");
  WNDCLASSEX wc = { 0 };
  MSG messages;
  HWND hWnd;

  wc.lpszClassName = szClassName;
  wc.lpfnWndProc = WndProc;
  wc.cbSize = sizeof (WNDCLASSEX);
  wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
  wc.hInstance = hInstance;
  
  if (!RegisterClassEx(&wc))
  {
    ShowLastWinError();
    return -1;
  }
  
  hWnd = CreateWindowEx (0, szClassName, szWindowName, WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
      HWND_DESKTOP, 0, hInstance, 0);
      
  if (!IsWindow(hWnd))
  {
    ShowLastWinError();
    return -1;
  }
  
  ShowWindow (hWnd, iShow);
  while (GetMessage (&messages, NULL, 0, 0))
  {
    TranslateMessage (&messages);
    DispatchMessage (&messages);
  }

  return (int)messages.wParam;
}


If you want to learn windows programming get a good book like
https://www.amazon.com/Programming-Windows%C2%AE-Fifth-Developer-Reference/dp/157231995X/ref=la_B000APETXO_1_3?s=books&ie=UTF8&qid=1514369470&sr=1-3

I did not use a Template I wrote the whole thing from scratch as a Console Application in codeblocks

The 1st program i did was a messagebox program which worked.

EDIT: I followed this tutorial https://www.youtube.com/watch?v=8GCvZs55mEM
Last edited on
I'd suggest to use Thomas' or the "Win32 Application" Template from CodeBlocks.
1
2
if(RegisterClassW(&wc))
   return -1;


That looks like your error. RegisterClass() returns a non NULL ATOM if successful. So if you wish to test for failure and exit, you need to negate the return with '!'. See Thomas' example.
1
1
2
3
if(RegisterClassW(&wc))
   return -1;


That looks like your error. RegisterClass() returns a non NULL ATOM if successful. So if you wish to test for failure and exit, you need to negate the return with '!'. See Thomas' example.


That Worked Thanks!!
As a beginner I would recommend to code slowly and detect vulnerabilities among your code. That's how you ensure ""code security"". You can also use a app to do that. I know one called Checkmarx if you want to try.
Good luck!
Every post of this benhart guy seems to be a plug for Checkmarx, whatever that is.
@freddie1
...whatever that is.
is a provider of state-of-the-art application security solution: static code analysis software, seamlessly integrated into development process.


Finding this name might improve their google ranking, so it's a form of disguised advertising.
I wonder why they get away with it.
Registered users can post here. Sign in or register to post.