Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ winapi destroyWindow()立即退出我的程序_C++_Visual Studio 2010_Winapi - Fatal编程技术网

C++ winapi destroyWindow()立即退出我的程序

C++ winapi destroyWindow()立即退出我的程序,c++,visual-studio-2010,winapi,C++,Visual Studio 2010,Winapi,好的,我可以理解函数是否会返回错误或抛出异常,但由于某种原因,我对DestroyWindow的调用实际上会在该点退出程序。就像实际的exit()函数改变我的程序流一样。文档中没有提到类似的内容,我没有办法弄清楚到底发生了什么,因为我没有得到错误代码。有人遇到过这样的事情吗 我使用这个对象比使用winapi做的更多,所以忽略它的其余部分。这里还有什么问题吗 SYNC_WinSystem.h #ifndef SYNC_WINSYSTEM_H #define SYNC_WINSYSTEM_H #in

好的,我可以理解函数是否会返回错误或抛出异常,但由于某种原因,我对DestroyWindow的调用实际上会在该点退出程序。就像实际的exit()函数改变我的程序流一样。文档中没有提到类似的内容,我没有办法弄清楚到底发生了什么,因为我没有得到错误代码。有人遇到过这样的事情吗

我使用这个对象比使用winapi做的更多,所以忽略它的其余部分。这里还有什么问题吗

SYNC_WinSystem.h

#ifndef SYNC_WINSYSTEM_H
#define SYNC_WINSYSTEM_H

#include "SYNC_ISystem.h"

#include <Windows.h>
#include <array>
#include "SYNC_Winput.h"
#include "SYNC_IRenderer.h"
#include "SYNC_D3D11Renderer.h"

#define FULL_SCREEN true

// SYNC_WinSystem
class SYNC_WinSystem : public SYNC_ISystem
{   
public:
    class WindowsContext;

    SYNC_WinSystem();

    virtual long Initialize(InitializeContext *);
    virtual void Init_Loop();
    virtual void Shutdown();

    virtual long MakeDirectory(std::string);
    virtual bool CreateSkin(std::string, std::string, SYNC::ISkin *&);
    virtual ISound * CreateSound();

    LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

private:
    virtual long InitializeWindows();
    virtual bool Frame();
    virtual void ShutdownWindows();

private:
    SYNC_Winput m_Input;

private:
    std::shared_ptr<SYNC_IRenderer> m_Graphics;
    HINSTANCE m_hinstance;
    HWND m_hwnd;
    int m_screenWidth;
    int m_screenHeight;
    std::string m_WindowName;
};

// SYNC_WinSystem::WindowsContext
class SYNC_WinSystem::WindowsContext : public SYNC_ISystem::InitializeContext
{
public:
    WindowsContext();
    std::string Type();

    HINSTANCE m_hinstance;
    std::string m_WindowName;

private:
    const static std::string m_Identifier;
};


#endif
\ifndef SYNC\u WINSYSTEM\u H
#定义同步系统
#包括“SYNC_ISystem.h”
#包括
#包括
#包括“SYNC_Winput.h”
#包括“SYNC_IRenderer.h”
#包括“SYNC_D3D11Renderer.h”
#定义全屏为真
//同步系统
类同步WinSystem:公共同步WinSystem
{   
公众:
类上下文;
SYNC_WinSystem();
虚拟长初始化(InitializeContext*);
虚拟void Init_Loop();
虚拟无效关机();
虚拟长MakeDirectory(std::string);
虚拟bool-CreateSkin(std::string、std::string、SYNC::ISkin*&);
虚拟ISound*CreateSound();
LRESULT回调消息处理程序(HWND-HWND、UINT-msg、WPARAM-WPARAM、LPARAM-LPARAM);
私人:
虚拟长初始化窗口();
虚拟布尔帧();
虚拟空间关闭窗口();
私人:
同步Winput m_输入;
私人:
std::共享的ptr m_图形;
HINSTANCE m_HINSTANCE;
HWND m_HWND;
int m_屏幕宽度;
int m_屏幕高度;
std::字符串m_WindowName;
};
//同步WinSystem::WindowsContext
类同步\u WinSystem::WindowsContext:公共同步\u ISystem::InitializeContext
{
公众:
WindowsContext();
std::string Type();
HINSTANCE m_HINSTANCE;
std::字符串m_WindowName;
私人:
常量静态标准::字符串m_标识符;
};
#恩迪夫
SYNC_WinSystem.cpp

#include "SYNC_WinSystem.h"

// SYNC_WinSystem definitions
SYNC_WinSystem * g_windows;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

SYNC_WinSystem::SYNC_WinSystem()
    : m_Graphics(new SYNC_D3D11Renderer)
{
    m_hinstance = nullptr;
    m_hwnd = nullptr;
    m_screenWidth = 0;
    m_screenHeight = 0;
}

long SYNC_WinSystem::Initialize(InitializeContext  * context)
{
    long result = 0;
    char errors[256];
    WindowsContext * cContext;
    if(context->Type() == "WindowsContext")
        cContext = static_cast<WindowsContext *>(context);
    else
        return false;

    m_hinstance = cContext->m_hinstance;
    m_WindowName = cContext->m_WindowName;
    g_windows = this;


    result = InitializeWindows();
    if(result)
    {
        sprintf_s(errors, "The Window could not initialize. Windows error code: %i", result);
        MessageBox(NULL, errors, "Error!", MB_OK); 
        return result;
    }

    std::array<std::string, 3> folderNames=
    {{
        "Compiled_Models",
        "Temp_Models",
        "Materials"
    }};

    for(int i = 0; i < (int) folderNames.size(); i++)
    {
        result = MakeDirectory(folderNames[i]);
        if( result && (result != ERROR_ALREADY_EXISTS))
        {
            sprintf_s(errors, "Error creating directory \" %s \" for system. Windows error code: %i", folderNames[i].c_str(), result);
            MessageBox(NULL, errors, "Error!", MB_OK);
            return result;
        }
        result = 0;
    }

    SYNC_D3D11Renderer::D3D11Context graphicsContext;
    graphicsContext.fullscreen = true;
    graphicsContext.hwnd = m_hwnd;
    graphicsContext.screenDepth = 1000.0f;
    graphicsContext.screenNear = 0.1f;
    graphicsContext.screenWidth = m_screenWidth;
    graphicsContext.screenHeight = m_screenHeight;

    if(!m_Graphics->Initialize(&graphicsContext))
        return false;

    return result;
}

void SYNC_WinSystem::Init_Loop()
{

    MSG msg;
    bool done, result;

    ZeroMemory(&msg, sizeof(MSG));
    done = false;

    while(!done)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(m_Input.IsKeyPressed(VK_ESCAPE))
        {

            done = true;
        }
        else
        {
            result = Frame();
            if(!result)
            {
                done = true;
            }
        }

    }
}

void SYNC_WinSystem::Shutdown()
{
    ShutdownWindows();
}

long SYNC_WinSystem::MakeDirectory(std::string dirName)
{
    DWORD result = 0;
    long returnValue = 0;

    dirName.insert(0, ".\\");
    result = CreateDirectory(dirName.c_str(), NULL);

    if(result == 0)
    {
        returnValue = GetLastError();
    }
    return returnValue;
}

bool SYNC_WinSystem::Frame()
{

    if(!m_Graphics->Frame())
        return false;

    return true;
}

long SYNC_WinSystem::InitializeWindows()
{
    DWORD result = 0;
    WNDCLASSEX wc;
    DEVMODE dmScreenSettings;
    int posX, posY;

    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = &WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = m_hinstance;
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hIconSm = wc.hIcon;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = m_WindowName.c_str();
    wc.cbSize = sizeof(WNDCLASSEX);



    if(RegisterClassEx(&wc) == 0)
    {
        result = GetLastError();
        return result;
    } 

    m_screenWidth = GetSystemMetrics(SM_CXSCREEN);
    m_screenHeight = GetSystemMetrics(SM_CYSCREEN);

    if(FULL_SCREEN)
    {
        memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
        dmScreenSettings.dmSize = sizeof(dmScreenSettings);
        dmScreenSettings.dmPelsWidth = (unsigned long) m_screenWidth;
        dmScreenSettings.dmPelsHeight = (unsigned long) m_screenHeight;
        dmScreenSettings.dmBitsPerPel = 32;
        dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

        ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
        posX = posY = 0;

        m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, 
                             m_WindowName.c_str(), 
                             m_WindowName.c_str(),
                             WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
                             posX, posY, m_screenWidth, m_screenHeight,
                             NULL, NULL, m_hinstance, NULL);

    }
    else
    {
        m_screenWidth = 800;
        m_screenHeight = 600;

        posX = ((GetSystemMetrics(SM_CXSCREEN)/2) - (m_screenWidth/2));
        posY = ((GetSystemMetrics(SM_CYSCREEN)/2) - (m_screenHeight/2));

        m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, 
                             m_WindowName.c_str(), 
                             m_WindowName.c_str(),
                             WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
                             posX, posY, m_screenWidth, m_screenHeight,
                             NULL, NULL, m_hinstance, NULL);
    }



    if(!m_hwnd)
    {
        result = GetLastError();
        return result;
    }

    ShowWindow(m_hwnd, SW_SHOW);
    SetForegroundWindow(m_hwnd);
    SetFocus(m_hwnd);

    return result;
}

void SYNC_WinSystem::ShutdownWindows()
{
    ShowCursor(true);

    if(FULL_SCREEN)
    {
        ChangeDisplaySettings(NULL, 0);
    }


    if(DestroyWindow(m_hwnd) == 0)
    {
        char meh[256];
        sprintf(meh, "error: %i" , GetLastError());
        MessageBox(NULL, meh, "error!", MB_OK);
    }

    m_hwnd = NULL;

    UnregisterClass(m_WindowName.c_str(), m_hinstance);
    m_hinstance = NULL;

    g_windows = NULL;

    return;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch(msg)
    {
        case WM_DESTROY:
        {

            PostQuitMessage(0);
            return 0;
        }
        case WM_CLOSE:
        {
            PostQuitMessage(0);
            return 0;
        }
        default:
        {
            return g_windows->MessageHandler(hwnd, msg, wparam, lparam);
        }
    }
}

LRESULT CALLBACK SYNC_WinSystem::MessageHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{

    switch(msg)
    {
        case WM_KEYDOWN:
        {
            m_Input.KeyDown((unsigned int) wparam);
            return 0;
        }
        case WM_KEYUP:
        {
            m_Input.KeyDown((unsigned int) wparam);
            return 0;
        }
        default:
        {
            return DefWindowProc(hwnd, msg, wparam, lparam);
        }
    }


}

ISound * SYNC_WinSystem::CreateSound()
{
    return nullptr;
}

bool SYNC_WinSystem::CreateSkin(std::string filename, std::string shaderName, SYNC::ISkin *& skin)
{
    if(!m_Graphics->CreateSkin(filename, shaderName, skin))
        return false;

    return true;
}


// SYNC_WinSystem::WindowsContext definitions
const std::string SYNC_WinSystem::WindowsContext::m_Identifier = "WindowsContext";

SYNC_WinSystem::WindowsContext::WindowsContext()
{


}

std::string SYNC_WinSystem::WindowsContext::Type()
{
    return m_Identifier;
}
#包括“SYNC_WinSystem.h”
//同步系统定义
同步WinSystem*g_windows;
LRESULT回调WndProc(HWND HWND、UINT msg、WPARAM WPARAM、LPARAM LPARAM);
SYNC_WinSystem::SYNC_WinSystem()
:m_图形(新同步D3D11渲染器)
{
m_hinstance=nullptr;
m_hwnd=nullptr;
m_屏幕宽度=0;
m_屏幕高度=0;
}
长同步\u WinSystem::初始化(InitializeContext*上下文)
{
长结果=0;
字符错误[256];
WindowsContext*cContext;
如果(上下文->类型()=“WindowsContext”)
cContext=静态_cast(上下文);
其他的
返回false;
m_hinstance=cContext->m_hinstance;
m_WindowName=cContext->m_WindowName;
g_视窗=这个;
结果=初始化窗口();
如果(结果)
{
sprintf_s(错误,“窗口无法初始化。Windows错误代码:%i”,结果);
MessageBox(空,错误,“错误!”,MB_OK);
返回结果;
}
std::数组文件夹名=
{{
“已编译的模型”,
“临时模型”,
“材料”
}};
对于(int i=0;i<(int)folderNames.size();i++)
{
结果=MakeDirectory(folderNames[i]);
如果(结果&(结果!=错误已经存在))
{
sprintf_s(错误,“为系统创建目录\%s\”时出错。Windows错误代码:%i”,文件夹名[i]。c_str(),结果);
MessageBox(空,错误,“错误!”,MB_OK);
返回结果;
}
结果=0;
}
同步D3D11Renderer::D3D11上下文图形上下文;
graphicsContext.fullscreen=true;
graphicsContext.hwnd=m_hwnd;
graphicsContext.screenDepth=1000.0f;
graphicsContext.screenNear=0.1f;
graphicsContext.screenWidth=m_screenWidth;
graphicsContext.screenHeight=m_screenHeight;
如果(!m_Graphics->初始化(&graphicsContext))
返回false;
返回结果;
}
void SYNC_WinSystem::Init_Loop()
{
味精;
完成,结果;
零内存(&msg,sizeof(msg));
完成=错误;
而(!完成)
{
if(peek消息(&msg,NULL,0,0,PM_-REMOVE))
{
翻译信息(&msg);
发送消息(&msg);
}
如果(按下m_输入键(VK_退出))
{
完成=正确;
}
其他的
{
结果=帧();
如果(!结果)
{
完成=正确;
}
}
}
}
void SYNC_WinSystem::Shutdown()
{
关闭窗口();
}
长同步\u WinSystem::MakeDirectory(std::string目录名)
{
DWORD结果=0;
长返回值=0;
插入(0,“.\\”);
结果=CreateDirectory(dirName.c_str(),NULL);
如果(结果==0)
{
returnValue=GetLastError();
}
返回值;
}
bool SYNC_WinSystem::Frame()
{
如果(!m_Graphics->Frame())
返回false;
返回true;
}
长同步\u WinSystem::InitializeWindows()
{
DWORD结果=0;
WNDCLASSEX wc;
DEVMODE DMS筛选设置;
int-posX,posY;
wc.style=CS|HREDRAW | CS|VREDRAW | CS|OWNDC;
wc.lpfnWndProc=&WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=m_hInstance;
wc.hIcon=LoadIcon(空,IDI_WINLOGO);
wc.hIconSm=wc.hIcon;
wc.hCursor=LoadCursor(空,IDC_箭头);
wc.hbrBackground=(HBRUSH)GetStockObject(黑色画笔);
wc.lpszMenuName=NULL;
wc.lpszClassName=m_WindowName.c_str();
wc.cbSize=sizeof(WNDCLASSEX);
如果(寄存器类(&wc)==0)
{
结果=GetLastError();
返回结果;
} 
m_screenWidth=GetSystemMetrics(SM_CXSCREEN);
m_屏幕高度=GetSystemMetrics(SM_屏幕);
如果(全屏)
{
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth=(无符号长)m_屏幕宽度;
dmScreenSettings.dmPelsHeight=(无符号长)m_屏幕高度;
dmScreenSettings.dmBitsPerPel=32;
dmScreenSettings.dmFields=DM|u BITSPERPEL | DM|u PELSWIDTH | DM|PELSHEIGHT;
中国
#include <memory>
#include <Windows.h>

#include "Syncopate.h"
#include "SYNC_WinSystem.h"
using namespace std;

#include "SYNC_Winput.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR lpCmdLine, INT nCmdShow)
{
    //create a new instance of the engine
    std::shared_ptr<SYNC_WinSystem> system( new SYNC_WinSystem);

    // create an init context for this specific derivative of SYNC_ISystem
    SYNC_WinSystem::WindowsContext context;
    context.m_hinstance = hInstance;
    context.m_WindowName = "Syncopate";

    // Initialize the system object. if something goes wrong, return ERROR
    if(system->Initialize(&context))
        return 1;

    SYNC::ISkin * model;

    if(!system->CreateSkin("data.txt", "ColorShader", model))
        return 1;

    system->Init_Loop();

    system->Shutdown();

    return 0;
}
    case WM_CLOSE:
    {
        PostQuitMessage(0);
        return 0;
    }