HWND is NULL but GetLastError returns 0x00

Zarathustra adam.chrapkowski at gmail.com
Fri Oct 16 08:49:15 PDT 2009


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);
  }
}


More information about the Digitalmars-d-learn mailing list