CreateWindowEx失败,错误1400

时间:2015-12-18 23:07:37

标签: winapi nasm

标题说明了一切。我只是尝试在程序集中使用CreateWindowEx创建一个窗口,并且调用失败并出现错误1400(错误窗口句柄)。我已经验证没有其他API调用失败,并且DefWindowProc为WM_CREATE返回0(传递)。进入CreateWindowEx的参数对我来说是正确的。

编辑:现在共享带有错误检查的代码,我之前删除了该代码以提高可读性。

extern __imp__GetModuleHandleA@4
extern __imp__RegisterClassA@4
extern __imp__ExitProcess@4
extern __imp__AdjustWindowRect@12
extern __imp__CreateWindowExA@48
extern __imp__GetSystemMetrics@4
extern __imp__GetLastError@0
extern __imp__DefWindowProcA@16

global _main

section main text align=1
_main:
    push    byte 0
    call    [__imp__GetModuleHandleA@4]
    test    eax, eax
    jz      OnError

    mov     dword [hInstance], eax
    push    dword wndclass
    call    [__imp__RegisterClassA@4]
    test    eax, eax
    jz      OnError

    ; WS_VISIBLE | WS_CAPTION | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU
    mov     ebx, 0x10000000 | 0x00C00000 | 0x04000000 | 0x02000000 | 0x00080000

    push    byte 0
    push    ebx
    push    rect
    call    [__imp__AdjustWindowRect@12]
    test    eax, eax
    jz      OnError

    push    byte 0
    push    dword [hInstance]
    push    byte 0
    push    byte 0

    ; rect.bottom - rect.top
    mov     eax, dword [rect.bottom]
    sub     eax, dword [rect.top]
    push    eax

    ; rect.right - rect.left
    mov     eax, dword [rect.right]
    sub     eax, dword [rect.left]
    push    eax

    ; ( GetSystemMetrics(SM_CYSCREEN) - rect.bottom + rect.top ) >> 1
    push    byte 1                      ; SM_CYSCREEN
    call    [__imp__GetSystemMetrics@4]
    sub     eax, dword [rect.bottom]
    add     eax, dword [rect.top]
    sar     eax, 1
    push    eax

    ; ( GetSystemMetrics(SM_CXSCREEN) - rect.right + rect.left ) >> 1
    push    byte 0                      ; SM_CXSCREEN
    call    [__imp__GetSystemMetrics@4]
    sub     eax, [rect.right]
    add     eax, [rect.left]
    sar     eax, 1
    push    eax

    push    ebx                         ; dwStyle
    push    byte 0                      ; lpWindowName
    push    classname                   ; lpClassName
    push    0x00040000 | 0x00000100     ; dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE
    call    [__imp__CreateWindowExA@48]
    test    eax, eax
    jz      OnError

    push    byte 0
    call    [__imp__ExitProcess@4]

OnError:
    call    [__imp__GetLastError@0]
    push    eax
    call    [__imp__ExitProcess@4]

section WinProc text align=1
WinProc:
    push    dword [esp + 20]
    push    dword [esp + 16]
    push    dword [esp + 12]
    push    dword [esp + 8]
    call    [__imp__DefWindowProcA@16]
    ret     16

section wndclass data align=1
wndclass:
    dd      0x0020 | 0x0002 | 0x0001    ; Style CS_OWNDC | CS_HREDRAW | CS_VREDRAW
    dd      WinProc                     ; lpfnWndProc
    dd      0                           ; cbClsExtra
    dd      0                           ; cbWndExtra
hInstance:
    dd      0                           ; hInstance
    dd      0                           ; hIcon
    dd      0                           ; hCursor
    dd      0                           ; hbrBackground
    dd      0                           ; lpszMenuName
    dd      classname                   ; lpszClassName
classname:
    db      '1337Class', 0

section rect data align=1
rect:
    .left:   dd      0
    .top:    dd      0
    .right:  dd      1366
    .bottom: dd      768

1 个答案:

答案 0 :(得分:0)

事实证明问题出在我的窗口程序上。它完全搞砸了。只调用DefWindowProc的工作窗口过程的一个很好的例子是

*Main> parse (chooseFrom ["head", "tail", "tales"]) "" "tail"
Right "tail"
*Main> parse (chooseFrom ["head", "tail", "tales"]) "" "tales"
Right "tales"
*Main> parse (chooseFrom ["head", "tail", "tales"]) "" "tafoo"
Left (line 1, column 3):
unexpected "f"
expecting "i" or "l"

经验教训:

  1. 在开始捣乱之前,始终将ESP复制到EBP。它使 代码难以维护。
  2. 始终注意调试器中堆栈的内容。
  3. 即使你把垃圾传递给它,DefWindowProcA也会返回零!