Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: rain_storm on October 20, 2011
-
Here's a neat trick I found that allows you to draw directly to a console window (Only tested under XP). This gives you access to a window using only kernel32 and gdi32 no need to link to user32 at all. The trick is to set the console title to a unique string and then get the HWND to the console using FindWindow. I'm interested in seeing if this works on windows7 because win7 requires you to pump the message queue which adds extra function calls.
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "gdi32.lib")
#undef UNICODE
#undef _UNICODE
#include <windows.h>
const int resx = 256;
const int resy = 256;
int pixel[resx*resy];
int t;
HWND hwnd;
HDC hdc;
RECT rc;
void mainCRTStartup(void)
{
static BITMAPINFO bmi = { sizeof(BITMAPINFOHEADER),0,0,1,32 };
SetConsoleTitle("io");
hwnd = FindWindow(NULL, "io");
if (!hwnd) return;
hdc = GetDC(hwnd);
if (!hdc) return;
static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),
1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_GDI|PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,
32,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0 };
static int fmt = ChoosePixelFormat(hdc,&pfd);
if (!fmt) return;
if (!SetPixelFormat(hdc,fmt,&pfd)) return;
while (!GetAsyncKeyState(VK_ESCAPE)) {
for (int y = 0; y < resy; y++) {
for (int x = 0; x < resx; x++) {
pixel[x+y*resx] = (x^y)+t;
}
}
GetClientRect(hwnd, &rc);
bmi.bmiHeader.biWidth = resx;
bmi.bmiHeader.biHeight = resy;
StretchDIBits(hdc,rc.right/2-resx/2,rc.bottom/2-resy/2,resx,resy,
0,0,resx,resy,pixel,&bmi,0,SRCCOPY);
SwapBuffers(hdc);
t++;
}
ReleaseDC(hwnd,hdc);
DestroyWindow(hwnd);
ExitProcess(0);
return;
}
-edit my bad FindWindow requires user32 thought NODEFAULTLIBS was suppose to exclude it apparently it doesn't
-
I've tested it here on win7 x64. It fails 9 times out of 10 and I just get a quick flash of the console window before it exits (no drawing). When it does work, I see blue square patterns drawn in the console window.
Not sure why it's failing so often, maybe a timing issue with grabbing the handle before the it's set up??
-
Very strange. Also this doesn't seem to work when I try it in FASM. I wonder why that is
-
I get almost the same result as Raizor, it works about 1 time out of every 4 times it runs, it's really col when it works though!