1

When I type this, I don't get a blank window display on my computer:

#define WIN32_LEAN_AND_MEAN

#include<Windows.h>

bool InitMainWindow(HINSTANCE, int);
LRESULT CALLBACK MsgProc(HWND, UINT, WPARAM, LPARAM);
const int WIDTH=800;
const int HEIGHT=600;

HWND hwnd=NULL;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline,       int mCmdShow)
{
    if(!InitMainWindow(hInstance,mCmdShow))
        return 1;
    MSG msg={0};

    while(WM_QUIT!=msg.message)
    {
        if(PeekMessage(&msg,0,0,0,PM_REMOVE))

        {  
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return static_cast<int>(msg.wParam);

}

bool InitMainWindow( HINSTANCE hInstance, int mCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize=sizeof(wcex);
    wcex.style=CS_HREDRAW|CS_VREDRAW;
    wcex.cbClsExtra=0;
    wcex.cbWndExtra=0;
    wcex.lpfnWndProc=MsgProc;
    wcex.hInstance=hInstance;
    wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
    wcex.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszClassName = (LPCWSTR)"tutorialClass";

    wcex.lpszMenuName=NULL;
    wcex.hIconSm=LoadIcon(NULL,IDI_WINLOGO);

    if(!RegisterClassEx(&wcex))
        return false;

    hwnd=CreateWindow((LPCWSTR)"tutorial class",
            (LPCWSTR)"tutorial window",
            WS_OVERLAPPED | WS_SYSMENU |WS_CAPTION,
            GetSystemMetrics(SM_CXSCREEN)/2-WIDTH/2,
            GetSystemMetrics(SM_CYSCREEN)/2-HEIGHT/2,
            WIDTH,
            HEIGHT,
            (HWND)NULL,
            NULL,
            hInstance,
            (LPVOID*)NULL);

    if(!hwnd)
        return false;

    ShowWindow(hwnd, mCmdShow);

    return true;

}

LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_CHAR:
            switch(wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                    return 0;
            }
            return 0;
    }
    return DefWindowProc(hwnd, msg ,wParam,lParam);
}

In Visual Studio 2010 Express Edition, I get the following Errors and Warnings:

'WindowBlank.exe': Loaded 'C:\Users\ramapriya\Documents\Visual Studio 2010\Projects\WindowBlank\Debug\WindowBlank.exe', Symbols loaded.
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\ProgramData\Browser Manager\2.6.1125.80\{16cdff19-861d-48e3-a751-d99a27784753}\browsemngr.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\imagehlp.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\psapi.dll', Cannot find or open the PDB file
'WindowBlank.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file
The program '[7792] WindowBlank.exe: Native' has exited with code 1 (0x1).

Sometimes it takes a long time to compile in my computer because it is slow. I do not understand whats wrong in the code. I just started learning and I tried changing parameters, the order, retyped everything, creating a new project but a blank window just doesn't seem to appear. Why is that?

I don't know what is wrong with this code. Or are some libraries missing perhaps? I have windows 7 64 bit Home Edition, and I also have Directx 11 SDK installed.

Mat
  • 202,337
  • 40
  • 393
  • 406
LoveMeow
  • 3,858
  • 9
  • 44
  • 66
  • 1
    Try stepping through the code in the debugger to see whether any function calls fail. If they do, check the error code. (Also, casting a char* to LPCWSTR tends not to go well.) – Raymond Chen Apr 04 '13 at 18:51
  • If I dont give the (LPCWSTR) before the characters it gives an error.That's the reason I added it. Is there anything else I could do?it wont compiler without it. – LoveMeow Apr 04 '13 at 18:57
  • 1
    The compiler is telling you that you are passing the wrong type of string. Casting does not fix that. – Raymond Chen Apr 04 '13 at 19:15
  • [This question](http://stackoverflow.com/questions/2111368/creating-a-win32-window-app-with-english-title-bar-but-the-title-bar-becomes-ch) discusses the casting issue. – Raymond Chen Apr 04 '13 at 19:21
  • Look carefully at the return value from `CreateWindow`. Check the error code. That error code will tell you where your typo is. – Raymond Chen Apr 05 '13 at 19:50

2 Answers2

1

You can also use LPCTSTR to be on the safer side like:

wcex.lpszClassName = (LPCTSTR)_T("tutorialClass");

Do the same thing while calling CreateWindow() function also.

Also for a try just select Rebuild Solution once from Build menu in Visual Studio.

Or Select Clean Solution first and then Build Solution to see if this resolves your problem.

Another thing you can do is delete the .ncb, .sdf, .opt, .aps file if they are there in your solution directory and then again rebuild the solution and check.

Vishwanath Kamath
  • 340
  • 2
  • 3
  • 14
  • The cast is pointless here. If you use the `_T` or `TEXT` macros to declare string literals, you'll already have the right kind of string. Casting just hides bugs; nothing "safer" about that. – Cody Gray - on strike Apr 05 '13 at 07:35
-1

i got it your class name is different in wcex.lpszClassName = (LPCWSTR)"tutorialClass"; and hwnd=CreateWindow((LPCWSTR)"tutorial class",

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115