D3DXGetImageInfoFromFile function

D3DXGetImageInfoFromFile function

本文关键字:function D3DXGetImageInfoFromFile      更新时间:2023-10-16

使用D3DXGetImageInfoFromFile()函数给我这个:

Unhandled exception at 0x004114d4 in SAMPLE.exe: 0xC0000005: Access violation reading location 0x00000000.

下面是包含错误的代码:
// ...
WCHAR           *Path = L"./LIFE.bmp";
D3DXIMAGE_INFO      *Info;
IDirect3DSurface9   *Surface = NULL;
LPDIRECT3DDEVICE9   pd3dDevice;
// ...
D3DXGetImageInfoFromFile(Path, Info); // everything is fine here, unless i do the following:
pd3dDevice -> CreateOffscreenPlainSurface(Info->Width, Info->Height, Info->Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);

那么,这里发生了什么?当我输入数字而不是Info->...时,一切工作正常…

您传递未初始化的指针Info,当方法试图访问您得到异常。您需要的是:

D3DXIMAGE_INFO Info;
D3DXGetImageInfoFromFile(Path, &Info);
pd3dDevice->CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);

另外,我建议您处理返回HRESULT的任何函数的结果代码。如:

if (FAILED(D3DXGetImageInfoFromFile(Path, &Info))) {
  // print something, abort or whatever.
}

如果您使用DXUT.h,那么V()V_RESULT宏是您最好的朋友:

V(D3DXGetImageInfoFromFile(Path, &Info));

HRESULT hr;
V_RETURN(D3DXGetImageInfoFromFile(Path, &Info));
V_RETURN(pd3dDevice->CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL));
V_RETURN(...);
// ... lots of D3D calls.
return S_OK;

您可能需要转义到您的图像路径中的反斜杠:

std::wstring wsPath = L"C:\wood.bmp";