Hey,
I'm trying to make a simple addon for an existing application which would take screenshot from inside a directx game by a keystroke.
First of all, I made one using the simple old method via bitblt, GetDC and it worked, but only for XP and below, in Vista it gets black screens therefore I'm trying to make one via directx with no luck.
The application is old, and written in pure C and most of the samples available are for C++ and I couldnt find nor make work of anything relevant.
(directx header version: dx9-nov2007)
Here is the relevant part of my code for my try
Code:
/*
* File : Snap.c
* Usage: Snapshots.
*/
#include <windows.h>
//DX9
#include "dx9/d3d9.h"
#include "dx9/d3dx9.h"
//headers
#include "Include/Snap.h"
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
//Vista API (dwm)
//#include "Include/dwmapi.h"
dword g_dwScreenWidth = 640;
dword g_dwScreenHeight = 480;
static IDirect3DDevice9 *init_d3d9(void)
{
static HMODULE d3d9_handle = 0;
IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
IDirect3D9 *d3d9_ptr = 0;
IDirect3DDevice9 *device_ptr = 0;
D3DPRESENT_PARAMETERS present_parameters;
HRESULT hr;
d3d9_handle = LoadLibraryA("d3d9.dll");
if (!d3d9_handle)
{
LOG("Could not load d3d9.dll, critical.\n");
}
d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
if (!d3d9_create)
LOG("Failed to get address of Direct3DCreate9");
d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
if (!d3d9_ptr)
LOG("Failed to create IDirect3D9 object");
ZeroMemory(&present_parameters, sizeof(present_parameters));
present_parameters.Windowed = FALSE;
present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
present_parameters.BackBufferWidth = 640;
present_parameters.BackBufferHeight = 480;
present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
//hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
//if(!hr)
// LOG("Direct3D_CreateDevice returned:");
return device_ptr;
}
void Snap()
{
HRESULT hr;
static HMODULE d3d9_handle = 0;
IDirect3DDevice9 *device_ptr;
D3DPRESENT_PARAMETERS present_parameters;
device_ptr = init_d3d9();
if (!device_ptr)
LOG("NO DEVICE %u", device_ptr);
hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device_ptr, 640, 480, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &present_parameters, NULL);
}
Few issues I see with the code:
- device_ptr in Snap() is NULL for some reason and it LOG's "NO DEVICE" in my log. (not sure what it should return)
- When it gets to the last "hr" it crashes the application with an access violation 0xC0000005.
I've searched all over for the past 7 days now with no luck, hope anyone came across this in a traditional C.
edit: my compiler is VC6 (again, old project)
Best Regards,
Oleg.