Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateWindow() returns NULL and ERROR_SUCCESS

Status
Not open for further replies.

lagaye

Programmer
Apr 2, 2002
7
FR
Hi, I'm new on this forum ...

I've got this annoying problem
and I hope someone will have some
clue on how to solve it ...

In a simple Win32 application
(Visual Studio template "simple
Win32 application" I'm using a
function that initializes
everything in my app, including
creating the window.

When I create my Window (using
CreateWindow()), the function
returns a handle which value is
NULL (telling the CreateWindow
function did not manage to create
the window ...) but, if I call
GetLastError it returns me
ERROR_SUCCESS (telling the function
ended successfully ...) so here's
my problem . It seems there's a
paradox somewhere ??? Or did
I miss something ?

I haven't found any information
about this in MSDN.

Grrrrr i hate Windows API ...

Thanks in advance
 
Well, if the handle to the window is null but you see an ERROR_SUCCESS, the only situtation I can think of off hand is it was successful because the window does not exist so the return of NULL, in the computers mind, is correct????

look into FindWindow and see if you can find it maybe???

Just a guess
Matt
 
I meant window does exist the find window call could be done before you create it to find out if it already exists and if it does you can show the window FindWindow returned.

Matt
 
Here's my code ...

In reply to Zyrenthian :

I don't think the window allready exists
because I only create it once (only one
call to Init()).
The MSDN tells about CreateWindo() :
If you get NULL in return, then an error
has occured, call GetLastError to know what
this error was ...

In reply to sflam :
Here's my initialization procedure (that's
the one that creates the window) I'm sorry
to write it in french but I think you'll
understand what it's all about anyway :))

<PRE>
HWND Init (HINSTANCE hInstance,int nCmdShow)
{ WNDCLASS wc;

// Définit et inscrit la classe de la fenetre //

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(DWORD);
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject BLACK_BRUSH);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName = szClass;
if(!RegisterClass(&wc))
{ char Buffer[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,Buffer,1024,NULL);
MessageBox(NULL,Buffer,&quot;Hotwarz Erreur&quot;,MB_OK);
return NULL;
}

// Obtient les dimensions d'affichage //

//int ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
//int ScreenHeight = GetSystemMetrics(SM_CYSCREEN);

// Crée une fenetre et génère l'affichage //

HWND hWnd;

hWnd = CreateWindow(szClass,szCaption,WS_VISIBLE | WS_POPUP,50,50,0,0,NULL,NULL,hInstance,NULL);

if(hWnd==NULL)
{ char Buffer[1024];
int LastErr = GetLastError();
if (LastErr==ERROR_SUCCESS) sprintf(Buffer,&quot;Succés \0&quot;);
else FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,Buffer,1024,NULL);
MessageBox(NULL,Buffer,&quot;Hotwarz Erreur&quot;,MB_OK);
return NULL;
}

ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

return hWnd;
}
</PRE>

 
Something not right with your creation: the width and hight are zero. I think you have reverse the (x, y) and (width, height) values.

Shyan
 
Nope that's not it ,
I'm really trying to create a window
with 0,0 size at 50,50 position.

I've got my code working in another
application ...

Any more clues ?

Lagaye
 
It's changed !!!

I've set the size to 10,10 and now
it tells me &quot;Invalid window handle&quot;
 
I don't have problem creating the window, be it zero size or (10, 10), with minor changes of your code (like declaring szClass and szCaption).

What system are you running on? Compiler? Version?

If you can send your project to me off the list (lam@ssicentral.com) , I'll take a look.

Shyan
 
Don't now how, but I solved the problem ...

I was preparing it to send it to you and
it then worked ok ...

I can't figure out what the error that I
made was, but well it works so thanks for
your help. It's nice to see people who help
each other...

See you later in Tek-tips forum ...

lagaye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top