D3DXGetImageInfoFromFile function
题
Using D3DXGetImageInfoFromFile()
function gives me this:
Unhandled exception at 0x004114d4 in SAMPLE.exe: 0xC0000005: Access violation reading location 0x00000000.
Here is the piece of code that contains error:
// ...
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);
So, what is happening here? And when I type in numbers instead of Info->...
, everything works fine...
解决方案
You pass uninitialized pointer Info
and when method tries to access you get the exception. What you need is the following:
D3DXIMAGE_INFO Info;
D3DXGetImageInfoFromFile(Path, &Info);
pd3dDevice->CreateOffscreenPlainSurface(Info.Width, Info.Height, Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL);
Also, I'd recommend you handling result codes of any function that returns HRESULT. Like:
if (FAILED(D3DXGetImageInfoFromFile(Path, &Info))) {
// print something, abort or whatever.
}
If you use DXUT.h then V()
or V_RESULT
macros are your best friends:
V(D3DXGetImageInfoFromFile(Path, &Info));
OR
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;
其他提示
You probably need to escape the backslash in the path to your image:
std::wstring wsPath = L"C:\\wood.bmp";
不隶属于 StackOverflow