Solved

How do I force another program to draw its window on my memory bitmap instead of drawing to the screen?

Posted on 2004-04-19
15
354 Views
Last Modified: 2013-11-20
How do I force another program to draw its window on my memory bitmap?
This is the scenario: I want one of the windows of another program to be hidden (or off-screen) but I need to force it to draw to a memory location I specify (could be a bitmap dc). My program analyzes by pixel the window contents. Previously, I used GetPixel and it works great. But now I want the target window to be invisible. GetPixel will not work with "invisible targets". So i'm wondering if theres a way to "redirect" the drawing of the other application to a memory bitmap i specify (which I can analyze without problem), instead of drawing to its window.
Remember, my goal is to keep the third-party window invisible (or offscreen) while its program draws into a memory location that I can analyze pixel by pixel (realtime drawing and analysis).
This is a hard question with 500 points attached.
I'll give another 500 bonus points to the one who can give the answer.
Thanks in advanced for those who might have an idea.
0
Comment
Question by:kbcware
  • 6
  • 6
  • 2
15 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
I don't think there is any way to do this as you want.

What happens if you move the window off screen (eg SetWindowPos(NULL, 5000, 5000, 0, 0, SWP_NOZORDER | SWP_NOSIZE)) and then use your GetPixel routine ?
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20956223.html

This is very long thread, but you can find an answer in it. Asker wants to do the same thing from C# client. The code supplied by Feng Yuan does this using global hook and writes result to Clipboard. I changed this code by such way that it writes resulting bitmap bits to memory mapped file. C++ of C# client may read these bits from memory mapped file.
You can find download link to original Feng Yuan's solution and changed code. My solution (based on Feng Yuan's code) is fully listed in this thread and contains:

hook.dll:

hook project, hook.cpp
hook project, hookpaint.cpp
hook project, hookpaint.h

hooktest console client:

hooktest.cpp

0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
By the way, it works also when destination window is minimized.
If your process exits before destination (hooked) process, destination process crashes. Hook code should be changed to make clean unhooking, and this is what you need to do yourself or contact to hook code author.
0
 

Author Comment

by:kbcware
Comment Utility
First of all, I'm grateful that the first two people responding to my thread are the top 2 mfc gurus according to EE. =)

Andy, GetPixel returns CLR_INVALID when I try to use it on an off-screen window. This is expected, because Windows excludes the off-screen part of the window from the painting region.

I've seen the long thread you suggested Alex. Honestly, I didn't understand some part of the codes (especially the asm).
But the codes show that it is really possible. Please modify and share code with consideration of the following:

1.) pure visual c++ codes only. (no c#)
2.) my target window for capturing is unmanaged (i have no control and cant modify it)
3.) capturing will be done in quick successions, so file-based bitmaps may not be fast enough. anyway, my target window is small (250x170).
4.) please provide the cleanup (unhook code) if possible.

i understand that the codes in the thread attack this problem by overriding the beginpaint and endpaint functions.
maybe in the custom beginpaint function, we could modify the dc information so that the target prog would:

1.) draw its window in a MEMORY bitmap of my program instead of the display dc, and;
2.) draw even if its window is offscreen, hidden, or minimized.

just like i said in my original question, i'll be giving additional 500 points (1000 points in total) to the one who can help me with this.

thanks a lot.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
Comment Utility
1) pure visual c++ codes only. (no c#)

http://www.fengyuan.com/article/capture.zip.

This is link to pure C++ projects (hook Dll and console client) which capture window image from another process. This is not my code, I didn't try to understand it. I helped to handle the result of the hook:
- instead of writing to Clipboard image is written to memory-mapped file;
- C# client reads this file and shows image on the screen.

Basically, all you need is in this project.

2.) my target window for capturing is unmanaged (i have no control and cant modify it)

It doesn't matter, hood Dll needs only target window handle

3.) capturing will be done in quick successions, so file-based bitmaps may not be fast enough. anyway, my target window is small (250x170).

Memory-mapped file is fastest possible way of inter-process communication. Hook Dll writes to it, your process reads from it. It's impossible to write from one process to another directly.

I suggest you to download this project and see how it works. If this is OK for you, apply my changes to it - write to MMF instead of Clipboard (full code is in the thread). After this implement client code which creates MMF and reads bitmap bits from it.

4.) please provide the cleanup (unhook code) if possible.

I don't know how to make clean unhook, this is not my code. You need to do this yourself or talk with author.

---

1.) draw its window in a MEMORY bitmap of my program instead of the display dc, and;

Get bitmap bits from MMF and do what you need in your project.

2.) draw even if its window is offscreen, hidden, or minimized.

I tested this with minimized window, it works.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Memory-mapped file is not file, this is memory region which can be accessed from different projects.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
I thought it probably wouldn't work.  But if it did then it would have been easy to implement.
Ah well.  Good luck with the code from AlexFM.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:kbcware
Comment Utility
I downloaded feng Yuan's code, successfully compiled, and tested with notepad and calculator. I was amazed that it even worked after hiding these windows (ShowWindow(SW_HIDE)).

The BIG problem is that i tested it in my target prog's window.
After i did the hooktest, I checked the clipboard, and it contains SOLID BLACK.
The dimensions of the bitmap in the clipboard was correct, however.

My target app is a 3d chat game called Oz world.
I analyzed its exe file dependencies. it looks like it was made in c++.
the exe file is linked to a dll, which seems to do the graphics functions.
the dll is not linked to directx (or directdraw/3d) as shown in the dependencies.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Try Print Screen with this program - what what do you see in Clipboard?
DirectX is implemented as COM library, you cannot see this in file dependencies.
0
 

Author Comment

by:kbcware
Comment Utility
I see the exact window contents on the clipboard when I use printscreen.
This is what I've been using from the very beginning when I need to get the exact color values of a snapshot from
my target window.
0
 

Author Comment

by:kbcware
Comment Utility
I also tried sending WM_PRINT using my own memdc as the dc parameter.
the result: a 1x1 pixel bitmap goes to the clipboard
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
If Print Screen works, maybe there is solution.
WM_PRINT - this is exactly the way implemented in hook.dll. But HDC should belong to the same process, this is a reason it is implemented in the hook. Try to play also with WM_PRINTCLIENT message and with various flags in these messages. But all this should be done using hook.dll, it's impossible to do this from another process. Learn also destination process windows structure with Spy++, maybe you give wrong window handle.
If all this doesn't help, I don't know what to do. Try to talk with hook dll author.
0
 

Author Comment

by:kbcware
Comment Utility
The new PrintWindow function in the latest Core SDK works in Windows XP. It can capture partially or fully blocked windows. However, it can't capture hidden or minimized windows so maybe i'll just set the window offscreen. The problem is that it won't work with other OS (only in WinXP).... But PrintWindow is really very easy to use unlock Feng Yuan's code (not to mention his code only works with WinNT based systems).. I hope there is still a way to implement this in Win98/ME...
0
 

Author Comment

by:kbcware
Comment Utility
guys sorry if it took a long time before i close the topic.
i was hoping that someone would still come across the topic and post an idea.

i'm giving the credits to alexfm.
thanks to you all. by the way, i'm using the new PrintWindow (for XP only)...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now