views:

89

answers:

3

In Windows, there seems to be at least two ways to get to the frame buffer: GDI and DirectX.

The problem is that in order to use GDI or DirectX, it seems that you must be running a GUI application, and then from this application you can call the appropriate GDI and DirectX functions. For example:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // Insert frame buffer reading via GDI here.
    return 0;
}

However, I am looking to create a module that will contain one function, which will read the content of the frame buffer and save it as an image. This module will be linked with a bigger application.

So, how would I create this module without having to run an actual GUI application?

+1  A: 

You don't need a WinMain() to use GDI - just link against the appropriate libraries and you're good to go.

You can create console apps which create windows. There's no specific need for WinMain(). If you need an HINSTANCE you can call GetModuleHandle(NULL).

Aaron
+1  A: 

Its really not as simple as that. For one GDI and DirectX use totally different ways of storing frame buffer information.

To intercept the DirectX buffer, for example, you'd need to intercept DirectX calls and then when present is called grab the frame-buffer to a file.

Goz
+1  A: 

If I'm reading your question correctly, it sounds like you're wanting to write a console app (non-GUI) that will perform a screen capture. If so, just google screen capture and you'll find all sorts of code examples, all of which perform pretty much the same steps, use GetDC(0) to get the HDC of the desktop, then use BitBlt to copy from that as a source DC into the DC of a memory bitmap you create, then write your bitmap to disk. Note that doing this in a console app is straightforward, doing it from an NT service is not. Also, the client area of some apps (videos, OpenGL, etc.) won't get captured without some extra work that gets very hairy.

An alternative method is to have your app use the SendKeys API function to simulate a Shift+PrintScreen, then retrieve the clipboard and write it to a file.

(If using C, use this instead of SendKeys)...

// Simulate press of "Shift+PrintScrn"... keybd_event(VK_SHIFT, 0, 0, 0); keybd_event(VK_SNAPSHOT, 0, 0, 0); // then release... keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0); keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0);

Brian D. Coryell