Jump to content

Recommended Posts

Posted (edited)

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
Posted

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

Posted (edited)

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
Posted (edited)

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
Posted

_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.

Posted

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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...