C++ 未处理的异常访问冲突2D DirectX呈现D2D1.dll

C++ 未处理的异常访问冲突2D DirectX呈现D2D1.dll,c++,C++,我正在Youtube上学习2D DirectX图形编程教程。该项目包含3个文件。它包含一个main.cpp文件,用于创建窗口、图形类和图形标题。当代码试图在graphics类中创建D2D1工厂时,我收到未处理的异常错误。我正在Windows Vista上使用Visual Studio 10。以下是Visual Studio 10的“输出”窗口中的3个文件和输出: main.cpp: #include <Windows.h> #include <d2d1.h> #inclu

我正在Youtube上学习2D DirectX图形编程教程。该项目包含3个文件。它包含一个main.cpp文件,用于创建窗口、图形类和图形标题。当代码试图在graphics类中创建D2D1工厂时,我收到未处理的异常错误。我正在Windows Vista上使用Visual Studio 10。以下是Visual Studio 10的“输出”窗口中的3个文件和输出:

main.cpp:

#include <Windows.h>
#include <d2d1.h>
#include "graphics.h"

Graphics *graphics;

LRESULT CALLBACK WindowProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
)
{
    if(uMsg == WM_DESTROY) { PostQuitMessage(0); return 0; }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);


    if(uMsg == WM_PAINT)
    {
        graphics->BeginDraw();

        graphics->ClearScrean(0.0f, 0.0f, 0.5f);

        graphics->DrawCircle(100, 100, 50, 1.0f, 0.0, 0.0, 1.0);

        graphics->EndDraw();
    }
}

int WINAPI wWinMain(
    HINSTANCE hInstance,
    HINSTANCE precInstance,
    LPWSTR cmd,
    int nCmdShow)
{
    WNDCLASSEX windowclass;
    ZeroMemory(&windowclass, sizeof(WNDCLASSEX));
    windowclass.cbSize = sizeof(WNDCLASSEX);
    windowclass.hbrBackground = (HBRUSH) COLOR_WINDOW;
    windowclass.hInstance = hInstance;
    windowclass.lpfnWndProc = WindowProc;
    windowclass.lpszClassName = "MainWindow";
    windowclass.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&windowclass);

    RECT rect = { 0, 0, 800, 600 };
    AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, false,
    WS

    HWND windowhandle = CreateWindowEx(
    WS_EX_OVERLAPPEDWINDOW,
    "MainWindow",
    "DirectX",
    WS_OVERLAPPEDWINDOW,
    100,
    100,
    rect.right - rect.left,
    rect.bottom - rect.top,
    NULL,
    NULL,
    hInstance,
    0);

    if(!windowhandle) { return -1; }


    if(!graphics->Init(windowhandle))
    {
         delete graphics;
         return -1;
    }

    ShowWindow(windowhandle, nCmdShow);

    MSG message;
    while(GetMessage(&message, NULL, 0, 0))
    {
        DispatchMessage(&message);
    }

    delete graphics;

    return 0;
 }
调试输出:

'project2.exe': Loaded
'C:\Users\David\Documents\VisualStudio2010\Projects
 \project2\Debug\project2.exe',
 Symbols loaded.
 'project2.exe': Loaded 'C:\Windows\System32\ntdll.dll',
 Cannot find or open the PDB file
 ' project2.exe': Loaded 'C:\Windows\System32\kernel32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\d2d1.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\msvcrt.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\user32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\gdi32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\advapi32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\rpcrt4.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\msvcr100d.dll',
 Symbols loaded.
 'project2.exe': Loaded 'C:\Windows\System32\imm32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\msctf.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\lpk.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\usp10.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\uxtheme.dll',
 Cannot find or open the PDB file
 First-chance exception at 0x698509bf in project2.exe: 0xC0000005:
 Access violation writing location 0x00000000.
 Unhandled exception at 0x698509bf in project2.exe: 0xC0000005:
 Access violation writing location 0x00000000.

您正在访问未初始化的指针:

Graphics *graphics;  
//...  
if(!graphics->Init(windowhandle))  // <-- CRASH  
{...}  
'project2.exe': Loaded
'C:\Users\David\Documents\VisualStudio2010\Projects
 \project2\Debug\project2.exe',
 Symbols loaded.
 'project2.exe': Loaded 'C:\Windows\System32\ntdll.dll',
 Cannot find or open the PDB file
 ' project2.exe': Loaded 'C:\Windows\System32\kernel32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\d2d1.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\msvcrt.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\user32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\gdi32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\advapi32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\rpcrt4.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\msvcr100d.dll',
 Symbols loaded.
 'project2.exe': Loaded 'C:\Windows\System32\imm32.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\msctf.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\lpk.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\usp10.dll',
 Cannot find or open the PDB file
 'project2.exe': Loaded 'C:\Windows\System32\uxtheme.dll',
 Cannot find or open the PDB file
 First-chance exception at 0x698509bf in project2.exe: 0xC0000005:
 Access violation writing location 0x00000000.
 Unhandled exception at 0x698509bf in project2.exe: 0xC0000005:
 Access violation writing location 0x00000000.
Graphics *graphics;  
//...  
if(!graphics->Init(windowhandle))  // <-- CRASH  
{...}  
Graphics *graphics;  
graphics = new Graphics;  
if(!graphics->Init(windowhandle))  
{...}