Enhanced Meta File of other windows

I want to get the write the data of another app's window to an EMF file (bit-blitting wont work, I need all the data, not just the viewable data).  The code below seems close.  It creates the EMF and writes "Draft".  The message gets posted to the separate app.  The separate app receives it and processes the WM_PRINT message, but nothing get's written to the DC.

What do I need to do?
HDC hdcRef, hdcMeta;
RECT rBounds;

hdcRef = GetDC(hWnd);

//Retrieve the coordinates of the
//client rectangle in pixels.
GetClientRect(hWnd, &rBounds);

//Create the metafile DC
hdcMeta = CreateEnhMetaFile(hdcRef,
          (LPSTR)"My Company Name\0MyApp\0\0");

//Verify hdcMeta is good
TextOut(hdcMeta, 0, 0, "Draft", 5);

//Fake out the app to draw to our DC
//Since hWnd is in another thread, we
//need to use PostMessage
//Since we use PostMessage, we need to
//be sure that hWnd has time
//to process and print to m_hdcMeta

PostMessage(hWnd, WM_PRINT, (WPARAM)hdcMeta, PRF_OWNED);

henMF = CloseEnhMetaFile(m_hdcMeta);

// Release the reference DC.
ReleaseDC(hWnd, hdcRef);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Before use EMF, you have to close it first.

SDK says:

<<An application can use the enhanced-metafile handle returned by the CloseEnhMetaFile function to perform the following tasks:

Display a picture stored in an enhanced metafile >>
Sorry, I misunderstood your question.

1) You have to use SendMessage instead of PostMessage, because EMF may be closed before abother app receives your message. It is better than Sleep().
But it is not very important, because

2) HDC is only valid in the context of the YOUR application, and not in the context of another application. If I'm not wrong.

If another app is your app, then you have to pass the name of the metafile to another app so it can send WM_PAINT message to itself.

If another app is not written by you, there are several ways.
Easiest one is to install, say, WH_GETMESSAGE hook to another app's thread. Hook proc will be called in the address space of that another app, so inside this proc you can create the metafile and send WM_PRINT message to the app window.

Ask me if you need additional details.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

HDC valid not only in one app.
AndersonAdrianAuthor Commented:
-Sounds like I need to implement a WH_GETMESSAGE hook.  How do I do that?  
<<HDC valid not only in one app>>


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndersonAdrianAuthor Commented:
Does it work?
It must be something like this:


UINT msg1=RegisterWindowMessage("Your_program_get_metafile")

UINT msg2=RegisterWindowMessage("Your_program_metafile_created")

DWORD threadId=GetWindowThreadProcessId(


// Post, not Send.

// Waiting for msg2 message. Once received, metafile can be used and the hook can be uninstalled.



UINT msg1=RegisterWindowMessage("Your_program_get_metafile")

UINT msg2=RegisterWindowMessage("Your_program_metafile_created")

void setHook(DWORD threadId)

.... HookProc(.....)
  if(code>0) {
     LPMSG msg=(LPMSG)lParam;
     if(msg->message=msg1) {

           HDC dc=GetDC(msg->hwnd);
           // All your code here.
           // Send, not Post
           // Notify your app that the job is done
           // PostMessage(yourWindow,msg2,...)


  retrun CallNextHookEx(...)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.