www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - HWND is NULL but GetLastError returns 0x00

reply Zarathustra <adam.chrapkowski gmail.com> writes:
I have the problem with the following code in D2:
CreateWindowEx returns NULL but I haven't got idea why?

module test;

import base;
static import user32;
static import kernel32;

void MsgBox(immutable char [] o_str){
  user32.messageBox(null, cast(str)o_str, cast(str)"msg", 0x0);
}

struct WndClassEx{
align:
  dword size              = 0x00000030;
  dword style             = 0x00000000;
  ptr   wndProc           = null      ;
  dword clsExtraBytes     = 0x00000000;
  dword wndExtraBytes     = 0x00000000;
  ptr   hInstance         = null      ;
  ptr   hIcon             = null      ;
  ptr   hCursor           = null      ;
  ptr   hbrBackground     = null      ;
  str   menuName          = null      ;
  str   className         = null      ;
  ptr   hIconSm           = null      ;
}

class Application{
  static const ptr  hInstance;
  static const str commandLine;

  static this(){
    hInstance   = kernel32.getModuleHandle(null);
    commandLine = kernel32.getCommandLine();
    assert(hInstance && commandLine, "Application initialization fail");
  }
}

extern (Windows) static dword wndProc(ptr o_hwnd, dword o_msg, dword o_wparam,
dword o_lparam){
  return 0;
}

class Window{
  const ptr handle;

  this(){
    WndClassEx wndClass;

    wndClass.size      = 0x00000030;
    wndClass.wndProc   = cast(ptr)&wndProc;
    wndClass.hInstance = kernel32.getModuleHandle(null);
    wndClass.className = cast(str)"clsname";

    
    if(!user32.registerClassEx(cast(ptr)&wndClass)){
      user32.messageBox(null,
user32.translateErrorCode(kernel32.getLastError()), cast(str)"error",
0x00000000);
      assert(false, "Window class registring failed");
    }

    handle = user32.createWindowEx(
      0,
      wndClass.className,
      cast(str)"<applicationcaption>",
      0x00CF0000,
      0,
      0,
      640,
      480,
      null,
      null,
      Application.hInstance,
      null
    );
    
    if(handle is null){
      // ERROR_SUCCESS
      user32.messageBox(null,
user32.translateErrorCode(kernel32.getLastError()), cast(str)"error",
0x00000000);
      assert(handle);
    }

    user32.showWindow(handle, 0x0000000A);
    user32.updateWindow(handle);
  }


}

void main(){
  try{
    Window wnd = new Window();
  }
  catch(Object o){
    MsgBox(o.toString);
  }
}
Oct 16 2009
parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Zarathustra wrote:

 I have the problem with the following code in D2:
 CreateWindowEx returns NULL but I haven't got idea why?
<snip> That's because your are not properly processing all of the messages that are involved in window creation. See: http://msdn.microsoft.com/en-us/library/ms632635(VS.85).aspx or better yet, have a look at my port of atlwin.h: http://www.sstk.co.uk/atlWinD.php all of this tedious shit has been done before. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFK2Pq7T9LetA9XoXwRAqHlAKDPIZwXCWSvjNNviUX4SEguGFA+awCgoV7j VHP6w/x+jpQ42lOhyYxN0/o= =xX0G -----END PGP SIGNATURE-----
Oct 16 2009
parent reply Zarathustra <adam.chrapkowski gmail.com> writes:
I filled out all fields of wndclassex and operands of createWindowEx exactly
the same like in identical (working) C program, and there are still the same
problem.
//________________________________
void main(){
  try{
    ptr handle;
    //Window wnd = new Window();
    WndClassEx wndClass;

    wndClass.size          = 0x00000030;
    wndClass.style         = 0x00000000;
    wndClass.wndProc       = cast(ptr)&wndProc;
    wndClass.clsExtraBytes = 0x00000000;
    wndClass.wndExtraBytes = 0x00000000;
    wndClass.hInstance     = kernel32.getModuleHandle(null);
    wndClass.hIcon         = user32.loadIcon(null, 0x00007F00);
    wndClass.hCursor       = user32.loadCursor(null, 0x00007F00);
    wndClass.hbrBackground = gdi32.getStockObject(0x00000000);
    wndClass.menuName      = null;
    wndClass.className     = cast(str)"clsname";
    wndClass.hIconSm       = user32.loadIcon(null, 0x00007F00);

    
    if(!user32.registerClassEx(cast(ptr)&wndClass)){
      user32.messageBox(null,
user32.translateErrorCode(kernel32.getLastError()), cast(str)"error",
0x00000000);
      assert(false, "Window class registring failed");
    }

    handle = user32.createWindowEx(
      0,
      wndClass.className,
      cast(str)"<applicationcaption>",
      0x00CF0000,
      0,
      0,
      640,
      480,
      null,
      null,
      kernel32.getModuleHandle(null),
      null
    );
    
    if(handle is null){
      // ERROR_SUCCESS
      user32.messageBox(null,
user32.translateErrorCode(kernel32.getLastError()), cast(str)"error",
0x00000000);
      assert(handle);
    }
  }
  catch(Object o){
    MsgBox(cast(char[])o.toString);
  }
}
//________________________________

div0 Wrote:

 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Zarathustra wrote:
 
 I have the problem with the following code in D2:
 CreateWindowEx returns NULL but I haven't got idea why?
<snip> That's because your are not properly processing all of the messages that are involved in window creation. See: http://msdn.microsoft.com/en-us/library/ms632635(VS.85).aspx or better yet, have a look at my port of atlwin.h: http://www.sstk.co.uk/atlWinD.php all of this tedious shit has been done before. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFK2Pq7T9LetA9XoXwRAqHlAKDPIZwXCWSvjNNviUX4SEguGFA+awCgoV7j VHP6w/x+jpQ42lOhyYxN0/o= =xX0G -----END PGP SIGNATURE-----
Oct 17 2009
parent Zarathustra <adam.chrapkowski gmail.com> writes:
Ok thanks, My fault ;p
Oct 17 2009