I am just beginning to learn how to program DirectX 9 applications in C++, so I'm still not very good, and I'm messing it around quite a bit.
When I reopen the program after it crashes, the D3D Device fails to create with the result being D3DERR_INVALIDCALL. I am compiling with G++ in MinGW, using the DirectX August 2009 SDK. I'm guessing it's because I didnt release all the Devices and Textures etc when it crashes, and it still thinks I'm using them. It re-opens on a restart. Would someone be able to point me in the right direction as to how to sortof "reset" DirectX?
Here is what I am using to create the device:
D3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS D3DPP;
ZeroMemory(&D3DPP, sizeof(D3DPP));
D3DPP.Windowed = TRUE;
D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPP.hDeviceWindow = hWnd;
D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPP.BackBufferWidth = WIDTH;
D3DPP.BackBufferHeight = HEIGHT;
D3DDevice = 0; //Null pointer to make sure I can check it
HRESULT hr = D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPP, &D3DDevice);
if (!D3DDevice) {
MessageBox(NULL,"Device could not be created","error",0);
switch (hr) {
case D3DERR_DEVICELOST:
MessageBox(NULL,"1","error",0);
break;
case D3DERR_INVALIDCALL:
MessageBox(NULL,"2","error",0);
break;
case D3DERR_NOTAVAILABLE:
MessageBox(NULL,"3","error",0);
break;
case D3DERR_OUTOFVIDEOMEMORY:
MessageBox(NULL,"4","error",0);
break;
};
return 1;
};
return 0;
Well I have to ask. Do you have a valid hWnd before you make the call?
Make sure it's valid and good things might happen. :-)
It seems something in the MsgProc was causing the window to fail for the device, and my program to crash. Thanks anyway.
Related
So i wanna draw an overlay over another window, but im getting no real runtime error the visual Studio debugging tools tell me that the result of
HRESULT res = object->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWND, D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, NULL, &device);
is 0x8876086c. So here are the snippets of my code that are important and lead to this error(D3DERR_INVALIDCALL), which leads to the device being a nullpointer, which means i can't do anything with it.
I couldn't really figure out what led to this as i pretty much followed the documentation
int Paint::init(HWND hWND) {
if (FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &object))) {
exit(1);
}
ZeroMemory(¶ms, sizeof(params));
params.BackBufferWidth = width;
params.BackBufferHeight = height;
params.Windowed = true;
params.hDeviceWindow = hWND;
params.MultiSampleQuality = D3DMULTISAMPLE_NONE;
params.BackBufferFormat = D3DFMT_A8R8G8B8;
params.EnableAutoDepthStencil = TRUE;
params.AutoDepthStencilFormat = D3DFMT_D16;
HRESULT res = object->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWND, D3DCREATE_HARDWARE_VERTEXPROCESSING, ¶ms, NULL, &device);
and in the header file:
class Paint {
private:
IDirect3D9Ex* object = NULL;
IDirect3DDevice9Ex* device = NULL;
DWORD behaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
D3DPRESENT_PARAMETERS params;
ID3DXFont* font = 0;
HWND TargetHWND;
int width, height;
int init(HWND(hWND));
}
D3DPRESENT_PARAMETERS params = {};
// Use Win32 BOOL "TRUE" instead of C++ "true"
params.Windowed = TRUE;
params.hDeviceWindow = m_window;
// params.BackBufferWidth, BackBufferHeight are ignored for Windowed = TRUE
// For Windowed = TRUE, use params.BackBufferFormat = D3DFMT_UNKNOWN, which is zero.
// For params.BackBufferCount zero is assumed to be 1, but best practice
// would be to set it
params.BackBufferCount = 1;
// You used D3DMULTISAMPLE_NONE for the MultiSampleQuality instead of MultiSampleType.
// It's all zero anyhow.
params.EnableAutoDepthStencil = TRUE;
params.AutoDepthStencilFormat = D3DFMT_D16;
// --->>> This is the actual bug: there is no valid SwapEffect that has a value of zero <<<---
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
You are making the assumption that the Direct3D9 device supports D3DCREATE_HARDWARE_VERTEXPROCESSING, but you haven't validated it actually supports it. That said, D3DCREATE_SOFTWARE_VERTEXPROCESSING has known performance issues on Windows 10 so you should probably just require HW anyhow.
You should not be using legacy Direct3D9 or Direct3D9Ex for new projects. It's mostly emulated on newer versions of Windows, has lots of strange behaviors, and is almost 20 years old at this point. There's no support for the Direct3D 9 debug device on Windows 8.x or Windows 10. You should consider Direct3D 11 as a much better starting place for developers new to DirectX.
I want to modify a DirectX-Application's behavior (namely I want to implement a program similar to the Statman-Application by OrfeasZ [https://github.com/OrfeasZ/Statman/releases] as Onscreen-Info for Hitman 2) by injecting code (as DLL) into it and hooking the DirectX DeviceInterface VMT.
Since there are very limited resources on how to do this for DirectX11-Applications, I first wanted to learn how to do this in DX9 by creating a program that gets the DeviceInterface pointer of any DirectX9-Application. I wrote this code in the DllMain() function of my DLL (which is almost a 1:1 copy/paste of the third answer to this thread Hooking DirectX EndScene from an injected DLL):
HMODULE hDLL = GetModuleHandleA("d3d9");
LPDIRECT3D9(__stdcall*pDirect3DCreate9)(UINT) = (LPDIRECT3D9(__stdcall*)(UINT))GetProcAddress(hDLL, "Direct3DCreate9");
LPDIRECT3D9 pD3D = pDirect3DCreate9(D3D_SDK_VERSION);
D3DDISPLAYMODE d3ddm;
HRESULT hRes = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
//WNDPROC TempWndProc;
WNDCLASSEX wc = { sizeof(WNDCLASSEX),CS_CLASSDC, WndProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,TEXT("1"),NULL };
RegisterClassEx(&wc);
HWND hWnd = CreateWindow(TEXT("1"), NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL);
ShowWindow(hWnd, SW_SHOW);
IDirect3DDevice9 * ppReturnedDeviceInterface;
hRes = pD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &ppReturnedDeviceInterface);
pD3D->Release();
DestroyWindow(hWnd);
if (pD3D == NULL) {
//printf ("WARNING: D3D FAILED");
return false;
}
unsigned long* pInterface = (unsigned long*)*((unsigned long*)ppReturnedDeviceInterface);
When I inject the DLL into a DirectX9-Application (I've tried this with Civilization V and Total War: Shogun 2), it opens a window, so it actually is able to get the Direct3DCreate9 function from the d3d9.dll within the game, but pD3D->CreateDevice() always returns `D3DERR_INVALIDCALL. I don't really get what could be the reason for this, especially since the rest of this program works flawlessly. Does anybody have any idea what is missing/wrong?
D3DERR_INVALIDCALL
The method call is invalid. For example, a method's parameter may not
be a valid pointer.
Based on the error information this issue may caused by invalid parameter of IDirect3D9::CreateDevice method. You need initialize the pointer:
IDirect3DDevice9 *pReturnedDeviceInterface = NULL;
Also check if hWnd is a valid window handle and d3ddm.Format etc.
I've looked all over the internet for this and found nothing.
I know that Direct3D 9Ex works after Alt-Tabbing out of the program, unline Direct3D 9 (which just crashes). For that reason, I am trying to use Direct3D 9Ex. I want to make the program fullscreen. However, whenever I perform any operation with the device (IDirect3DDevice9Ex*) I get an access violation reading memory location 0x00000000. This is my code:
Direct3DCreate9Ex(D3D_SDK_VERSION, &pD3D);
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferHeight = iHeight;
d3dpp.BackBufferWidth = iWidth;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = false;
D3DDISPLAYMODEEX dm;
dm.Format = D3DFMT_X8R8G8B8;
dm.Height = iHeight;
dm.Size = sizeof(dm);
dm.RefreshRate = 60;
dm.ScanLineOrdering = D3DSCANLINEORDERING_PROGRESSIVE;
dm.Width = iWidth;
pD3D->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &dm, &pD3DDevice);
Any help would be appreciated.
d3dpp.FullScreen_RefreshRateInHz (0) differs from dm.RefreshRate (60).
They should both be set to the same rate, try D3DPRESENT_RATE_DEFAULT for both.
The HRESULT return from the CreateDeviceEx call should be checked using the SUCCEEDED or FAILED macro.
How can i take a screenshot of a full screen game in c++? I know it can be done with directx and i have written a simple code which can take a screenshot of a window but i don't know how to get a HWND of game.
Here is my code.
#include <d3dx9.h>
#include <d3dx9tex.h>
#include <stdio.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
int main()
{
IDirect3DSurface9 *surface;
IDirect3DDevice9 *g_pd3dDevice;
IDirect3D9 *g_pD3D;
D3DDISPLAYMODE d3ddm;
D3DPRESENT_PARAMETERS d3dpp;
HWND hWnd = GetConsoleWindow();
if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
printf("fail 1");
}
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
printf("fail 2");
}
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferHeight = d3ddm.Height;
d3dpp.BackBufferWidth = d3ddm.Width;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
{
printf("fail 3");
}
g_pd3dDevice->CreateOffscreenPlainSurface(800, 600, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
g_pd3dDevice->GetFrontBufferData(0, surface);
D3DXSaveSurfaceToFile("c:\\tmp\\output.jpg", D3DXIFF_JPG, surface, NULL, NULL);
surface->Release();
return 0;
}
I don't have any experience with Direct3D, but with OpenGL.
But both APIs provide almost the same functionality.
You should read back the back buffer (using Direct3D and not some Windows functionality) to host memory. Then simply save that bitmap to a file using an image library or something like that.
It's actually an extremely simple process.
These two links might be helpful to you:
http://www.mvps.org/directx/articles/screengrab.htm
How to save backbuffer to file in DirectX 10?
Is it possible to create device/directx application when pc uses vgasave mode?
This is my init function:
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = TRUE; // program windowed, not fullscreen
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
However when i call later
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
Program crashes, reporting about unhandled violation. Or maybe it isn't about vga, just I'm making something wrong?
CreateDevice returns D3DERR_NOTAVAILABLE
You've filled out way too few parameters in the D3DPRESENT_PARAMETERS. You've alomst certainly got bad settings for the back buffer and that kind of thing. If CreateDevice returns D3DERR_NOTAVAILABLE, then the d3ddev pointer is NULL, resulting in the access violation when you try to clear the back buffer- since there is no device.
D3DDeviceParameters.Windowed = true;
D3DDeviceParameters.BackBufferHeight = 0;
D3DDeviceParameters.BackBufferWidth = 0;
D3DDeviceParameters.BackBufferCount = 1;
D3DDeviceParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DDeviceParameters.MultiSampleQuality = 0;
D3DDeviceParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
D3DDeviceParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DDeviceParameters.hDeviceWindow = OS->GetHWND();
D3DDeviceParameters.EnableAutoDepthStencil = true;
D3DDeviceParameters.AutoDepthStencilFormat = D3DFMT_D24S8;
D3DDeviceParameters.Flags = 0;
D3DDeviceParameters.FullScreen_RefreshRateInHz = 0;
D3DDeviceParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Vsync.
This is my D3DPRESENT_PARAMETERS.