Duplicating GDI Objects

C

Christopher McKay

I hope that there is an answer for this. I have a process that contains
an HBITMAP. I want to share that object with another process. It
appears that I cannot use the DuplicateHandle API to make a copy of the
bitmap for my second process.

Is there a way where I can copy GDI objects from one process to another?
 
J

John Phillips

What makes you think that you can't use DuplicateHandle()? Do you get an
error when you do so? If so, can you show how you're calling it?

Haven't tried this myself, but it would seem that the 2nd last paragraph of
Q106386 suggests that DuplicateHandle() is the way to go.
 
C

Christopher McKay

Here's the code that I am currently using:

HANDLE hProc = ::OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE,
pid);
HBITMAP hBit;

// Request the bitmap handle from the second process
if( !::DuplicateHandle(hProc, (HANDLE)::SendMessage(hwnd, WM_USER + 10,
0, 0), ::GetCurrentProcess(), &hBit, 0, FALSE, DUPLICATE_SAME_ACCESS) ) {
TCHAR msg[256];
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
msg, 256, NULL);
::MessageBox(m_hWnd, msg, _T("Results"), MB_ICONERROR);
}
else
::CloseHandle(hBit);

::CloseHandle(hProc);

The problem I am always having is that DuplicateHandle fails with the
message, "The handle is invalid". The only thing I can think of is that
the DuplicateHandle routine will not duplicate the GDI handle being
returned by my message handler. That's why I'm not sure what to do next.

Thanks for your help.

Chris McKay
 
J

John Phillips

Hmm...can't seem to find where in MSDN it says you -=can't=- do this, but
the prevailing wisdom in microsoft.public.platoformsdk.gdi appears to be
that one cannot.

It would seem that the best workaround is to use CreateDIBSection() and use
file mapping.

--
John Phillips
MVP - Windows SDK



Christopher McKay said:
Here's the code that I am currently using:

HANDLE hProc = ::OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE,
pid);
HBITMAP hBit;

// Request the bitmap handle from the second process
if( !::DuplicateHandle(hProc, (HANDLE)::SendMessage(hwnd, WM_USER + 10,
0, 0), ::GetCurrentProcess(), &hBit, 0, FALSE, DUPLICATE_SAME_ACCESS) ) {
TCHAR msg[256];
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
msg, 256, NULL);
::MessageBox(m_hWnd, msg, _T("Results"), MB_ICONERROR);
}
else
::CloseHandle(hBit);

::CloseHandle(hProc);

The problem I am always having is that DuplicateHandle fails with the
message, "The handle is invalid". The only thing I can think of is that
the DuplicateHandle routine will not duplicate the GDI handle being
returned by my message handler. That's why I'm not sure what to do next.

Thanks for your help.

Chris McKay

John said:
What makes you think that you can't use DuplicateHandle()? Do you get an
error when you do so? If so, can you show how you're calling it?

Haven't tried this myself, but it would seem that the 2nd last paragraph of
Q106386 suggests that DuplicateHandle() is the way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top