CreateWindowEx不返回NULL但GetLastError返回126(错误:126)

时间:2013-07-17 10:20:02

标签: windows api winapi getlasterror createwindow

在调用CreateWindowEx(..)之前没有任何错误。并且CreateWindowEx不返回NULL。有趣的是,在调用ShowWindow之后会出现一个窗口。

在代码中,您可以看到有2个消息框可以写入错误代码。第一个写入126,另一个写入0。

(错误126表示: ERROR_MOD_NOT_FOUND 126(0x7E) 找不到指定的模块。)

创建窗口后,窗口无法正常工作,如图所示 如果我的指针位于创建窗口的区域,它处于加载位置等等,当我将鼠标光标移动到窗口时,它不会显示箭头而是显示调整大小的光标。

对不起我的英语并感谢您的帮助。

代码: WinDeneme.cpp

    // WinDeneme.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

const wchar_t *AppName = L"Example";
unsigned int ClassID=0;
wchar_t Error[100];

LRESULT CALLBACK Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    wchar_t *ClassName = (wchar_t*)malloc(sizeof(wchar_t) * 100);

    swprintf(ClassName,100,L"%s_%d",AppName,ClassID);
    ClassID++;

    WNDCLASS *Class = (WNDCLASS*)calloc(1,sizeof(WNDCLASS));

    Class->lpszClassName = ClassName;
    Class->hInstance = hInstance;
    Class->lpfnWndProc = (WNDPROC)Proc;

    RegisterClass(Class);

    HWND Win = CreateWindowEx(
        0,                              // Optional window styles.
        ClassName,                     // Window class
        AppName,    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    swprintf(Error,100,L"%d",GetLastError());
    MessageBox(0,Error,L"Error",MB_OK); //  126

    ShowWindow(Win,nCmdShow);

    swprintf(Error,100,L"%d",GetLastError());
    MessageBox(0,Error,L"Error",MB_OK); //  0

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


    return EXIT_SUCCESS;
}

LRESULT CALLBACK Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

stdafx.h中

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <tchar.h>

图片(我使用相机,因为光标不会出现在打印屏幕中):

Pointer is in loading position

Pointer is in resizing position

**编辑:我通过添加

解决了指针问题
Class->hCursor = LoadCursor(NULL, IDC_ARROW);

但我仍然在VS2012 Express中收到错误126。

2 个答案:

答案 0 :(得分:1)

GetLastError()仅在发生实际错误时才有意义,除非另有说明。

对于CreateWindow/Ex(),如果它返回非NULL句柄,则没有错误发生GetLastError()的值未定义(它仍然包含来自早期API函数调用的错误代码。)

您必须在API函数退出之后和调用任何其他API函数之前立即调用GetLastError(),并且当API函数失败并且错误时,除非API函数是特定的记录为在其他情况下返回有效的GetLastError()值(例如,当CreateMutex()返回非NULL句柄时,GetLastError()如果互斥锁已经存在则返回ERROR_ALREADY_EXISTS,否则它返回0)。

大多数API函数在执行工作之前不会重置GetLastError(),从而保留了早期的错误代码。如果没有错误发生,则只有使用GetLastError()报告成功扩展信息的API函数才会重置GetLastError()

答案 1 :(得分:0)

如果CreateWindow返回非NULL值,则不会失败。没有错误,因此无需拨打GetLastError