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

RGDiff - Visual Diff Tool

, 2 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Capture results from GNU Diff and display visually.

Sample Image - RGDiff.jpg


The RGDiff project parses the output from a standard command line Diff program and presents the results in a graphical manner. It re-uses the GNU Diff program by "shelling out" using the WIN32 CreateProcess() function.

The techniques used could easily be incorporated into other programs requiring Diff functionality.

This article explains how the above is done in more detail.

Running RGDiff

RGDiff needs the GNU WIN32 diff.exe program to be in the same directory as RGDiff.exe. The directory should be writeable as RGDiff needs to create some temporary files.

RGDiff is statically linked with MFC because it is a standalone tool and MFC70.dll is probably not as widely distributed as MFC42.dll. I know this is probably not true among CodeProject members, but just to be on the safe side...


Whenever I have used Microsoft's WinDiff, I could not help but be frustrated by its "Zebra Stripes". I have used other GUI Diff tools, and found that a side by side approach helps me understand the differences between the files much better. I had a look around the WWW and couldn't find anything that was free and did what I wanted, I therefore decided to have a go at writing my own.

My first decision was whether to write my own Diff algorithm or rip off somebody else's. I looked at Diff algorithms, the WinDiff code, and the GNU Diff code, and found them all impenetrable in the time I had available Frown | :-(

I therefore decided that the only realistic approach for me was to use an existing command line Diff utility as a spawned process, which is a common design pattern on *nix. The GNU Diff utility seemed like a good start, and with programmers like Richard Stallman behind it, I felt that it might take me a while to improve on their algorithms! (A man's gotta know his limitations).

The GNU WIN32 Project provides a compiled diff.exe (and a lot of other useful stuff), and I have included diff.exe as part of both downloads. The above page provides details of how to get the source code if the reader should require that.

Techniques Used

This project uses standard WIN32/MFC techniques as follows:

  • Spawn a process and capture the results
  • SDI application with multiple windows
  • Splitter windows
  • Owner draw list boxes
  • Specialized list boxes
  • Registry access

Comparing Files

This is where Diff is used to compare 2 files. The command and output will be something like the following:

C:\Code\DIFF>diff f1.cpp f2.cpp
< // program to add 2 numbers
< //
<       return x + y;
>       int res(x+y)
>       return res;
> //int main(int argc, char** argv)
> //{
> //    cout << "Result: " << Addit(1,2) << endl;
> //    return 1;
> //}

The lines we are interested in are the ones without < or > signs at the start, as these are simply to help a human see which lines come from which file. The other lines tell us what the differences between the files are, or put another way, what we need to do to either file to make it the same as the other. It is these lines that we parse out and use to build the visual display.

The results above are interpreted as follows:

  1. 1,2d0 means delete lines 1-2 of file 1; or, if changing file 2 into file 1, append lines 1-2 of file 1 after line 0 of file 2.
  2. 8c6,7 means change line 8 of file 1 to read as lines 6-7 of file 2; or, if changing file 2 into file 1, change lines 6-7 of file 2 to read as line 8 of file 1.
  3. 15a15,20 means append lines 15-20 of file 2 after line 15 of file 1; or, if changing file 2 into file 1, delete lines 15-20 of file 2.

The GNU Diff documentation fully describes the way the results should be interpreted.

Comparing Directories

Comparing 2 files is good, but being able to recurse sub-directories like WinDiff is better. Luckily diff.exe provides command line options to allow this. The command and output will be something like the following:

C:\Code\DIFF>diff --recursive --report-identical-files --brief dir1 dir2
Files dir1\file1.cpp and dir2\file1.cpp are identical
Files dir1\file2.cpp and dir2\file2.cpp differ
Only in dir1: file3.cpp
Only in dir2: file4.cpp

The command line options are mostly self explanatory, but --brief tells Diff to only summarize each file. If this was omitted, a full Diff report for each file would be produced, taking longer. The above command executes very quickly, although I have only tried it on medium size projects of 200 or so files, with 20 or so sub-directories.

The results are parsed to produce a summary report just like with WinDiff. Double clicking a file then invokes Diff again to compare those 2 files, and a detailed report is displayed the same, as if the user had just chosen to compare those 2 files.

Note that because file names can contain the sequence " and " , some care is required when parsing the results.

The Scrolling Views

I have chosen to implement each view as an owner-draw list box. This allows me to customize the display to a great extent and I think they look OK. One problem is that when 2 files are displayed side by side in detail mode, I want them to keep in step when either one is scrolled. I achieve this by capturing all messages and when a scroll message is received, I first send it to the other list box. This works fine but can look a little odd sometimes as one view moves slightly after the other.

Future Direction

I am fairly pleased that RGDiff provides a useful, yet minimal implementation without too much effort expended. However the following are a few things that I think could improve RGDiff:

  • Birds eye view in the left margin, like WinDiff
  • Merge functionality (one at a time, or en-masse)
  • Better synchronized scrolling between views, in detail view
  • Better remembering of recently used files, like CodeProject front end to WinDiff

History - 3rd March 2003 - Initial Version


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

Rob Groves
Web Developer
United Kingdom United Kingdom
Software developer using C/C++, ASP, .NET and SQL Server/Oracle relational databases.

Comments and Discussions

QuestionFeature Request - Unix diff file import & export? Pinmemberdaluu14-Jul-06 9:21 
GeneralProblem comparing binary files PinmemberYaronNir20-Mar-05 23:37 
GeneralRe: Problem comparing binary files PinmemberRob Groves21-Mar-05 9:27 
GeneralRe: Problem comparing binary files PinmemberYaronNir21-Mar-05 21:40 
Generalthis code doesn't work for me :( PinsussYaronNir13-Mar-05 4:56 
GeneralRe: this code doesn't work for me :( PinmemberRob Groves13-Mar-05 11:08 
Some ideas...
Is diff.exe on your path?
Have you tried dumping out the command line? Does it work if you paste it into a cmd window?
Your szParams looks illegal as it has embedded double quotes with no escape characters.
I hope this is of some help.

GeneralRe: this code doesn't work for me :( PinmemberYaronNir13-Mar-05 12:23 
GeneralPretty good tool Pinmembergal007hm9-Dec-04 8:04 
Generalgreat tool Pinmemberfvcs20-Sep-04 19:01 
GeneralMade some improvements Pinmembertnimble18-Jul-04 2:40 
GeneralRe: Made some improvements PinmemberRob Groves18-Jul-04 11:23 
GeneralRe: Made some improvements PinmemberGud2114-Mar-05 11:13 
GeneralRe: Made some improvements PinmemberRob Groves4-Mar-05 22:21 
GeneralRe: Made some improvements Pinmember_garista_26-Apr-06 5:36 
GeneralRe: Made some improvements PinmemberRob Groves26-Apr-06 8:41 
Generalhelp Pinmemberttxuefeng25-Mar-04 15:25 
GeneralRe: help PinmemberRob Groves26-Mar-04 8:31 
GeneralWinMerge Pinmemberralfoide13-Dec-03 11:58 
GeneralOLEACC.DLL Error Pinsussty078726-Oct-03 19:54 
GeneralRe: OLEACC.DLL Error PinmemberRob Groves27-Oct-03 11:23 
GeneralRe: OLEACC.DLL Error Pinmembersurreybc9-May-04 17:05 
QuestionPosition? Pinmemberchen26-May-03 3:19 
GeneralVery Practical Question PinmemberChopper4-Mar-03 7:43 
GeneralRe: Very Practical Question PinmemberJörgen Sigvardsson4-Mar-03 8:59 
GeneralRe: Very Practical Question PinmemberChopper5-Mar-03 5:31 
GeneralRe: Very Practical Question PinmemberRob Groves5-Mar-03 10:42 
GeneralRe: Very Practical Question Pinmembergebrudergrimm14-Oct-03 4:39 
GeneralNice... Pinmember.S.Rod.3-Mar-03 21:57 
GeneralRe: Nice... PinmemberPaul Selormey3-Mar-03 22:42 
GeneralRe: Nice... Pinmember.S.Rod.3-Mar-03 23:13 
GeneralRe: Nice... PinmemberPaul Selormey4-Mar-03 0:32 
GeneralRe: Nice... PinmemberRob Groves5-Mar-03 10:35 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 3 Mar 2003
Article Copyright 2003 by Rob Groves
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid