D3D-GetSurfaceLevel未处理的异常

D3D - GetSurfaceLevel unhandled exception

本文关键字:异常 未处理 D3D-GetSurfaceLevel      更新时间:2023-10-16

我正在创建D3D光标,所以首先我制作了Surface&纹理在执行此操作时,我在GetSurfaceLevel()函数上遇到了错误。

0x5B494B11处出现未处理的异常0xC0000005:读取位置0x00000000时发生访问冲突。

代码:

D3DXCreateTextureFromFile( g_D3dDevice, L"cursor1.bmp", &g_cursortex );
g_cursortex->GetSurfaceLevel( 0, &g_surfcursor );
g_D3dDevice->SetCursorProperties( 0, 0, g_surfcursor );
g_D3dDevice->ShowCursor( TRUE );

我该怎么办?

Unhandled exception at 0x5B494B11 0xC0000005: Access violation reading location 0x00000000.

这种错误是由取消引用NULL指针引起的,请检查是否已成功创建纹理。在调用g_cursortex->GetSurfaceLevel( 0, &g_surfcursor ); 之前,请确保g_cursortex不为NULL

HRESULT hr = D3DXCreateTextureFromFile( g_D3dDevice, L"cursor1.bmp", &g_cursortex );
if (SUCCEEDED(hr))
{
    g_cursortex->GetSurfaceLevel( 0, &g_surfcursor );
    g_D3dDevice->SetCursorProperties( 0, 0, g_surfcursor );
}