Hi,
I am trying to call SetPixelFormat function to draw into a window created by another process. I have got the window handle and device context handle using FindWindow and GetDC functions. I have noticed that the window handle and device context handles are correct. However, SetPixelFormat function fails with error code 0xc0070006. Any idea why I am getting this error?
Is this possible at all to use the device context of window of another process to draw in that window using OpenGL?
Here is the source code:
// Source code to create a sample window in process 1
#include <windows.h>
// Global variable
HINSTANCE hinst;
// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
BOOL fGotMessage;
if (!InitApplication(hinstance))
return FALSE;
if (!InitInstance(hinstance, nCmdShow))
return FALSE;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASS wc;
wc.style = NULL;//CS_GLOBALCLASS;//CS_GLOBALCLASS;//CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = “MainWClass”;
if (!RegisterClass(&wc))
{
return NULL;
}
}
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
// Save the application-instance handle.
hinst = hinstance;
// Create the main window.
hwnd = CreateWindow("MainWClass", "Sample",
WS_VISIBLE,
0, 0,
240 + 10, 320 + 40,
NULL, NULL, hinstance, NULL);
if (!hwnd)
return FALSE;
UpdateWindow(hwnd);
return TRUE;
}
// Source code to find the window and use that window to draw something in another process 2
#include <windows.h>
// Global variable
HINSTANCE hinst;
// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
BOOL fGotMessage;
PWINDOWINFO pwi;
BOOL retCode;
COLORREF crColor,retCrColor;
//HDC hdc;
int iPixelFormat;
HWND hfoundWND=NULL;
HDC hdc;
DWORD errCode=0;
PIXELFORMATDESCRIPTOR pfd1 = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
hfoundWND=FindWindow("MainWClass","Sample");
if( NULL == hfoundWND)
{
errCode=GetLastError();
}
hdc= GetDC( hfoundWND );
if( NULL == hdc)
{
errCode=GetLastError();
}
// get the best available match of pixel format for the device context
iPixelFormat = ChoosePixelFormat(hdc, &pfd1);
if( 0 == iPixelFormat)
{
errCode=GetLastError();
}
errCode=SetPixelFormat(hdc, iPixelFormat, &pfd1);
if(0 == errCode)
{
errCode=GetLastError();
printf("SetPixelFormat failed, %d
", (UINT)errCode);
}
crColor=0x00ff0000;
retCrColor=SetBkColor(hdc, crColor);
if( CLR_INVALID == retCrColor)
{
errCode=GetLastError();
}
pwi=(PWINDOWINFO)malloc(sizeof( WINDOWINFO));
pwi->cbSize=sizeof(WINDOWINFO);
if(FALSE == GetWindowInfo(
hfoundWND,
pwi))
{
errCode=GetLastError();
}
retCode=ShowWindow(
hfoundWND,
SW_HIDE);
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
free(pwi);
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}