Click here to Skip to main content
Click here to Skip to main content

Remote Debugging in MSVC++ 6.0 - Step by step in the OnPaint message handler

By , 24 Jan 2002
 

Introduction

Most of the developers that work with graphic applications or work with applications that have full-screen window some times need to know exactly what is going on behind the OnPaint function or any other screen-drawing function. The problem is that when you set up a break point in the OnPaint message handler the focus of the window goes to the VC++ environment. When this happens, your on paint event is lost and you can't even see what's happening during the painting process. This situation is very common in directX programming too, where the program need to use fullscreen mode and doesn't allow you to switch to VC++.

MS VC++ has a workaround to this problem called remote debugger. The remote debugger enables you to execute your app in one machine while you step into the code in another machine.

To explain how this VC++ feature works, we are going to use the sample project provided with this article.

Setting up the environment

The first thing you need to do is install the remote debugger in the remote machine. If you don't have VC++ installed in the remote machine (it's not required :) ), you need to copy the following files to the system directory of the remote computer:

MFC42D.DLL<br>
MFCO42D.DLL<br>
MSVCRTD.DLL

After copying this files you need to establish a connection between the remote computer and the "host" computer (we'll call that computer that hosts VC++ "host" for now on). To do this, create a folder in the remote computer and share it so that the host computer can see it. You must do the same thing in the host computer. Create a folder called "Test" and share it so that the remote computer can access it.

The next step is to copy the remote debugger files to the remote machine. To do this, in the host computer, map the drive that you shared early in the remote computer. After mapping the drive, copy the remote debugger files. This files can be found in the Visual Studio Folder at the subfolder Common\MsDev98\Bin. This files are:

DM.DLL<br>
MSDIS110.DLL<br>
MSVCMON.EXE<br>
MSVCP60.DLL<br>
PSAPI.DLL<br>
TLN0T.DLL<br>

When the files are copied, in the remote computer, go to the folder where the files were copied. Find the MSVCMON.EXE file and start it. When you start it, you'll see the following screen.

When you see this screen, hit the Connect button, so that your machine starts to listen for any connection to the remote debugger. The following screen will be showed.

Setting up the VC++ project

We have the remote computer up and running. Now we are going to use the sample project to test the remote debugger. First copy the sample project files to the shared "Test" folder in the host computer. Open the sample project and locate the OnPaint message handler function. The function must contain this code:

CPaintDC dc(this); // device context for painting

if (IsIconic())
{

	SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

	// Center icon in client rectangle
	int cxIcon = GetSystemMetrics(SM_CXICON);
	int cyIcon = GetSystemMetrics(SM_CYICON);
	CRect rect;
	GetClientRect(&rect);
	int x = (rect.Width() - cxIcon + 1) / 2;
	int y = (rect.Height() - cyIcon + 1) / 2;

	// Draw the icon
	dc.DrawIcon(x, y, m_hIcon);
}
else
{
	CDialog::OnPaint();
	for(int i=0;i<640;i+=50)
	{
		dc.TextOut(200,i, "THIS IS DRAWED STEP BY STEP!");
	}
}

Set a break point at the line contain the "for" statement, so that we can step into the code while the window is being drawn.

The next step is to set the project to run using the remote debugger. Go to the Project Menu and select the Settings menu item. The project settings window should appear. Select the Debug Tab. In this window we will see 4 text boxes. In the first check box, you need to type the complete path of the application executable file as the host computer see it. (Ex.: My project is running under C:\Test in the host computer, so you need to type C:\Test\TestRemoteDebug.exe).

The second and third text box can be just blank. In the forth text box you need to type the path of the application as the remote computer see it. If you have copied the sample project to the "Test" folder, you need to inform the path of the mapped drive in the remote computer (Ex.: Let's say that you have mapped out "Test" folder of the host computer in the F: drive letter of the remote computer. In this case you need to inform F:\TestRemoteDebug.exe in the text box). After this step you can close the project settings window.

We are almost ready! Now you need to inform to the project which computer will be the remote debugger. To do this, go to the Build Menu and select the "Debugger Remote Connection..." option. Within this window, select the TCP/IP protocol and click on settings. In the settings window inform the name or IP address of the remote computer. After that you can close this window hitting the ok button.

Ok! We are ready! Start your project by hitting the F5 key (or Run menu item). The first time you execute you'll probably see a screen like this:

This happens because VC++ needs to find a local reference of each DLL that the project uses in the local machine. The first time this message boxes appear, just inform the correct path of the local DLLs.

Now just step into the code in the host computer and see what happens in the remote machine! Have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mauricio Ritter
Web Developer
Brazil Brazil
Member
Mauricio Ritter lives in Brazil, in the city of Porto Alegre. He is working with software development for about 8 years, and most of his work was done at a bank, within a home and office banking system.
Mauricio also holds MCSD, MCSE, MCDBA, MCAD and MCT Microsoft certifications and work as a trainer/consultant in some MS CTEC in his city.
Mauricio also works in his own programming site, aimed to Brazilian Developers: http://www.dotnetmaniacs.com.br
 
In his spare time he studys korean language...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhat DLLs do you use?memberpHRO gHO16 Dec '09 - 5:05 
I have only one MFC42D.DLL on my development machine. I copied it over to the remote device. When the debug session starts, I get an error ...
 
Ordinal not found.
 
The Ordinal 5084 could not be located in the dynamic link library MFC42D.DLL.
 
Obviously, the one and only MFC42D.DLL that I have isn't the right DLL. Where do I get the right DLL?
QuestionHow to remote debugging a program running on a PC with a private IP address?memberehaerim18 Sep '08 - 10:41 
It is well known to remote-debugging a program running on a PC with a public IP address, but it is not possible for a private IP address.
 
Simply, MSVCMON.exe running on the remote target PC acts as a server and the host machine running VC Debugger acts as a client. That is VC Debugger connects to the server and the server accepts it and finally remote debugging starts.
 
However when we need to debug a program running on a PC having a private IP, the above server/client mechanism does NOT work because server's ip is private and unknown to the client.
 
When a potential bug appears on a certain client's PC only, it is necessary to debug a program running on that specific client's PC, and it is likely that the PC has a private IP address (for example, 192.1.2.3 or 10.1.2.3, and so on).
 

Somehow it is necessary to make MSVCMON.exe run in client mode and VC Debugger run in server mode.
 

Does anyone have a good way of debugging in this case?
 
HaeRim
GeneralDifferent OSmemberMasthanRao15 May '07 - 7:38 
Hi,
My host machine is Windows XP & Target machine is Windows 2000 Professional. The windows directory in Windows XP box is C:\Windows and the same in 2K Professional box is C:\Winnt. When I started debugging the host machine is reporting error saying ntdll.dll is not found on the target machine. How to resolve the issue? Can any one please help me.
Regards
Masthan
GeneralNeed UrgentmemberUrukundappaGudise7 Nov '06 - 23:57 
Hello Guys,
 
I need to debug the code which is located on a remote server. The code is in ASP and VB.
 
Can you please suggest me how do it?Smile | :)
GeneralRemote DebuggingmemberProgrammer Anju15 Jun '06 - 22:44 
I want to Debug an exe which is supposed to start at Runtime on remote system. I tried to stop the service on remote system and then restart the same so that I may start debugging at the beginning of the code line by line, but all I can see is some memory addresses. When I started the debugger by pressing F5 I got some messages to confirm some paths wherein some incompatibility was found between host and remote computers exe. The exe on the remote computer is stored under a folder in Program Files and not in a shared folder. Can anyone give any suggestions about how to go about it?
GeneralRemote debuggingmemberRaj241115 May '06 - 20:49 
I am facing problem during remote debugging. If i start the process from host machine ( the one having Visual Studio) by pressing F5, i can debug the process successfully. But if some other application is going to start my process on remote machine and i want to debug the same process then how do i do it? Can somehow debugger window started on host machine when the process is running on remote machine and some error occurs on remote machine? Plz reply as soon as possible.
General.remotememberToby Opferman9 May '04 - 8:10 
You could sum this all up with:
 
cdb -p
.server tcp:port=8080
 
(locally)
 
cdb -remote tcp:port=8080,server=remotename
 

Generalsimplified Terminal Servermemberandré_k6 Dec '03 - 1:02 
Mauricio can you be a genious : need a method not only to debug, but, in general, to interact with a distant running program, developed with the possibility to receive commands from the net and to export screens either
GeneralRe: simplified Terminal ServermemberRobertoAguasGuerreiro25 Dec '03 - 23:47 
VNC is a program wich allows you to connect to the remote machine, like pc anywhere. I believe tightVNC is the free version of the program. Just install it on the host machine as CLIENT and on the remote machine as SERVER. You can set it up as a service or just when you wanna run it.
I suggest that you want the link too Smile | :) so here he is:
http://www.tightvnc.com/download.html
 
if you don't succeed in connecting to this site, try google with the search option : tightVNC
 
hope i've helped you
 

GeneralAttaching the debugger to a remote running processmembercvind24 Feb '03 - 23:29 
That's all very fine if you are able to start the remote process from the debugger, but what if the process is already started (by another application) on the remote machine?
Can I somehow attach the debugger to a running remote process?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 25 Jan 2002
Article Copyright 2002 by Mauricio Ritter
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid