Jump to content
Sign in to follow this  
devrandom

CreateWindow not working

Recommended Posts

devrandom

Hello,

I created this simple code, but it always returns 1.

Where's the problem?

#include <stdio.h>
#include <windows.h>

int main(void)
{
    HINSTANCE instance;
    HWND handle;

    handle = CreateWindow (
                                "MainWClass",  // Class name
                                "Window!!",    // Title
                                WS_MINIMIZEBOX|// \
                                WS_CAPTION|    //  \__> style
                                WS_POPUP|      //  /
                                WS_SYSMENU,    // /
                                CW_USEDEFAULT, // Left
                                CW_USEDEFAULT, // Top
                                250,           // Width
                                250,           // Height
                                (HWND) NULL,
                                (HMENU) NULL,
                                instance,
                                NULL
                             );

    if (!handle)
    {
        return 1;
    }

    ShowWindow(handle, SW_SHOW);

    while (1)
    {
        UpdateWindow(handle);
    }

    return 0;
}

Note: it is C, not C++... Is this the problem?

I know only C, how can I create windows in C with Windows APIs?

Thank you :)

Edited by devrandom

Share this post


Link to post
Share on other sites
Mat

You aren't initialising instance for a start. How about setting up a simple error catching mechanism. If you just use return GetLastError(); then look up the code on msdn then we would be a bit closer to finding out, saying that it just failed is not really enough.

Mat

Share this post


Link to post
Share on other sites
Richard Robertson

It's fine to use C. The problem is that you are attempting to use "MainWClass" but I don't see where you registered this class anywhere.

Share this post


Link to post
Share on other sites
devrandom

You aren't initialising instance for a start. How about setting up a simple error catching mechanism. If you just use return GetLastError(); then look up the code on msdn then we would be a bit closer to finding out, saying that it just failed is not really enough.

Mat

Thank you :)

It's fine to use C. The problem is that you are attempting to use "MainWClass" but I don't see where you registered this class anywhere.

Yes, I think this is the problem, I just modified a bit the MSDN example that evidently isn't working :S

How I can create a class? I looked at RegisterClass but being at the beginning with the Windows APIs, I have no idea on how to create the structure to pass as parameter for that function...

Is there any example on how to do this?

Sorry for my bad english :)

Edited by devrandom

Share this post


Link to post
Share on other sites
danielkza

How I can create a class? I looked at RegisterClass but being at the beginning with the Windows APIs, I have no idea on how to create the structure to pass as parameter for that function...

Something like this should work (untested):

WNDCLASS class = {0};
class.style = 0;
class.hInstance = GetModuleHandle(NULL);
class.lpfnWndProc = (WNDPROC) myWindowProcedure;
class.lpszClassName = (LPCTSTR) _T("MyWindowClass");
// etc
Edited by danielkza

Share this post


Link to post
Share on other sites
Richard Robertson

_T is a macro that will create a string that is either ANSI or Unicode depending on your platform.

I'm surprised anyone still uses it and doesn't just move to Unicode.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites
danielkza

_T is a macro that will create a string that is either ANSI or Unicode depending on your platform.

I'm surprised anyone still uses it and doesn't just move to Unicode.

I usually use Unicode exclusively, but for the example I thought I'd stick to the definition on MSDN.

Share this post


Link to post
Share on other sites
devrandom

Mhhh I think there is a problem: when I use _T() my compiler says that isn't declared :|

Share this post


Link to post
Share on other sites
wraithdu

Do I have to be the one to say it? Go buy a book and learn C. You clearly know next to nothing and are going to ask 100 more questions each time one is answered.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.