即使注册成功,CreateWindowEx也会因错误1407而失败?

Fat*_*sis 1 c++ winapi

当我使用d3d时,我一直在尝试在类中包装一些基本的win32功能,并且难以理解为什么CreateWindowEx函数在我使用RegisterClassEx创建一个有效类之后失败说一个类不存在,没有错误可言: \.我觉得我错过了一些愚蠢的小事,但我找不到它.以下是一些代码:

我有一个像这样扩展WNDCLASSEX的类,所以它有一个普通的std :: string作为类名和一个简化的构造函数:

#ifndef WINDOWCLASS_H
#define WINDOWCLASS_H

#include <Windows.h>
#include <string>
#include "WindowAbstract.h"
using namespace std;

class WindowClass : public WNDCLASSEX
{
public:
    WindowClass(string className, WindowAbstract * window);
    ~WindowClass();

    bool Register();
    string ClassName() {return m_className;}

    friend class WindowAbstract;

private:
    string m_className;
};

#endif
Run Code Online (Sandbox Code Playgroud)

这是该类的构造函数:

WindowClass::WindowClass(string className, WindowAbstract * window)
{

    cbSize        = sizeof(WNDCLASSEX);
    style         = 0;
    lpfnWndProc   = window->WndProc;
    cbClsExtra    = 0;
    cbWndExtra    = 0;
    hInstance     = hInstance;
    hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    hCursor       = LoadCursor(NULL, IDC_ARROW);
    hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    lpszMenuName  = NULL;
    lpszClassName = className.c_str();
    hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    m_className = className;
}
Run Code Online (Sandbox Code Playgroud)

这是在构造之后调用的寄存器函数:

bool WindowClass::Register()
{
    if(RegisterClassEx(this) == 0)
        return false;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

WindowAbstract类包含窗口过程并首先创建,以将指向它的函数的指针传递给WindowClass对象.

#ifndef WINDOWABSTRACT_H
#define WINDOWABSTRACT_H

#include <Windows.h>
#include <string>
using namespace std;

class WindowAbstract
{

public:
    WindowAbstract();
    ~WindowAbstract();

    bool Create(string windowTitle, string className, DWORD styles, DWORD extendedStyles, int top, int left, int bot, int right, HWND parent, HMENU id);
    void Show();

    friend class WindowClass;

private:
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    HWND m_hwnd;

};

#endif
Run Code Online (Sandbox Code Playgroud)

这是创建功能:

bool WindowAbstract::Create(string windowTitle, string className, DWORD styles, DWORD extendedStyles, int top, int left, int bot, int right, HWND parent, HMENU id)
{
    m_hwnd = CreateWindowEx(extendedStyles, className.c_str() , windowTitle.c_str(), styles, top, left, bot, right, parent, id, GetModuleHandle(NULL), NULL);
    if(!m_hwnd)
        return false;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

看到这一切后,这里是我测试的实际winmain:

#include "WindowAbstract.h"
#include "WindowClass.h"


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
    DWORD error;
    bool result;
    char buffer[100];

    WindowAbstract * window = new WindowAbstract();
    WindowClass * myClass = new WindowClass("myClass", window);

    result = myClass->Register();
    if(!result)
    {
        error = GetLastError();
        sprintf_s(buffer, "error: %i", error);
        MessageBox(NULL, buffer, "Registration Failed!", MB_OK);
    }

    result = window->Create("my Window", myClass->ClassName(), WS_OVERLAPPEDWINDOW, WS_EX_CLIENTEDGE, 20, 20, 200, 200, NULL, NULL);
    if(!result)
    {
        error = GetLastError();
        sprintf_s(buffer, "error: %i", error);
        MessageBox(NULL, buffer, "Window Creation Failed!", MB_OK);

    }

    window->Show();

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

    if(window)
    {
        delete window;
        window = 0;
    }

    if(myClass)
    {
        delete myClass;
        myClass = 0;
    }

    return msg.wParam;
}
Run Code Online (Sandbox Code Playgroud)

所以,总之,这是如此令人困惑,因为WindowClass对象的Register函数返回正常,但是创建函数在WindowAbstract对象中失败,因为没有有效的类(错误1407),具有该名称?Whuh?

arx*_*arx 5

我认为问题出lpszClassName = className.c_str()WindowClass构造函数中.通常,您不应该依赖于在c_str()任何时间长度内返回的值.

在这种情况下,您(有效地)获取本地变量的地址,该地址变量在您呼叫时可能已经不存在RegisterClassEx.所以RegisterClassEx是成功的,但谁知道它看到了什么名字?

lpszClassName = m_className.c_str()可能会工作(如果你m_className先分配)但它仍然是粗略的.最好在打电话c_str()之前立即打电话RegisterClassEx.

  • `string :: c_str()`返回的`char*`是有效的[直到字符串上的下一个非const方法调用(或字符串被破坏)](http://www.cplusplus.com/reference) /串/串/ c_str /). (2认同)