A Memory Viewer --- with a powerful class for any kind of text editor
Click here for larger image
Environment: VC6, NT4, Win2000, Win9x/ME
Abstract
The Memory Viewer displays memory contents starting at a specified address (0x00400000 by default), it shows memory contents as the HEX format in a binary editor.
With this viewer, programmer can watch where Windows runs an application, where is Windows data page, and even where is CMOS information (0x000FF5C0).
It shows the memory contents by bytes and explains bytes to ASCII as much as possible. If a memory page is not readable, it shows '?' instead.
A toolbar at the top of the window displays the starting address for the memory display. Edit the value in the toolbar and press the button 'Go' beside to change the starting address. Use the scrollbar at the side of the window to view other memory locations in the system's address space without changing the starting address of the display. Scrollbar can go to anywhere in any address without problem.
The treasure in this program is the class CSW_EditorBase. It is called 'EditorBase' because it can be a basic class for any kind of editor, such as an Information Window or a Binary Viewer. The class has all the necessary frame members for an editor control.
Editors are easily built by deriving just a few members from CSW_EditorBase, and they don't have to take care of flashing, selections, scrollbars, drawing part, and other boring works has to be done on each kind of editors job over and over.
If you are looking for a tiny text window, it would be a very nice startup.
In fact, the purpose of the Memory Viewer is just to test this idea and implement the class CSW_EditorBase. It proves well.
How a common text editor works
To draw texts line by line, you have to decide :
- The number of lines, GetTotalLines() in CSW_EditorBase;
- The height of a line, which is the maximum height of all characters in a line. GetLineH() in CSW_EditorBase;
- A line index from a client Y position on pixels. GetLineFromY() in CSW_EditorBase;
- The number of columns, it should be the maximum columns in all lines. It is used to calculate scrollbars range in most situations. GetTotalCols() in CSW_EditorBase;
- A width of a column. It is very important when the font is varied. GetColW() in CSW_EditorBase;
- A column index from a client X position on pixels. GetColFromX() in CSW_EditorBase;
- Get the string in a line. GetLineStr() in CSW_EditorBase;
- A method to draw a line. OnDrawItem() in CSW_EditorBase;
- Calculate scrollbar ranges. OnUpdateBars() in CSW_EditorBase;
- Override OnPaint(), MouseMove() and other CWnd methods.
- To remove some refresh problems, only draws the visible lines and moves the whole client area when scrolling.
- It may be other works such as how to save all text or draw an image inside.
Class CSW_EditorBase members
Abstract class CSW_EditorBase derives from CWnd and has the following member functions:
Line functions, a line can be a line of text and its alignment
// Get the number of total lines virtual UINT GetTotalLines (void) = 0; // Get the height of a line virtual UINT GetLineH (int nLine); // Get a line number from a position( in pixel) virtual int GetLineFromY (int &Y);
Column functions, a column can be a char and its alignment, or an image
// Get the maximum number of columns in all lines virtual UINT GetTotalCols (void) = 0; // Get the with of a column virtual UINT GetColW (int nCol); virtual int GetColFromX (int &X); // Cell functions // Get the line and column index from a point virtual BOOL GetLineColFromPt(CPoint &mapPt, int &iLine, int &iCol); // String functions // Get the string for a line virtual void GetLineStr (int nLine, CString& str) = 0; // Paint virtual BOOL OnDrawItem ( CDC *pDC, UINT nLine, LPCTSTR lpStr, int FirstSelectedChar = -1, int LastSelectedChar = -1); // Scroll bars, update scrollbars range void OnUpdateBars (void); // Don't derive again in subclass, normally. afx_msg BOOL OnEraseBkgnd (CDC *pDC); afx_msg void OnPaint (void); afx_msg void OnSize (UINT nType, int cx, int cy); afx_msg void OnHScroll ( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); afx_msg void OnVScroll ( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); afx_msg void OnLButtonDown (UINT nFlags, CPoint point); afx_msg void OnLButtonDblClk (UINT nFlags, CPoint point); afx_msg void OnMouseMove (UINT nFlags, CPoint point); afx_msg void OnLButtonUp (UINT nFlags, CPoint point); afx_msg BOOL OnMouseWheel (UINT nFlags, short zDelta, CPoint pt); ...
How to use class CSW_EditorBase
Derive a new class from CSW_EditorBase, overrides some members as necessary. GetTotalLines(),GetTotalCols() and GetLineStr() are essential. Creates an instance of the new class and puts it inside a view window.
See CSW_TextInfo as a simple example.
class CSW_TextInfo : public CSW_EditorBase
{
private:
// Save all lines here
CStringArray m_StrData;
// Keep the maximun number of columns
int m_MaxColNBR;
// public members
public:
CSW_TextInfo ();
// Add a line of string
void AddLine (LPCTSTR lpStr);
// Clean all information
void Clean (void);
// Members overrides from its parent class
protected:
virtual UINT GetTotalLines (void);
virtual UINT GetTotalCols (void);
virtual void GetLineStr (int nLine, CString& str);
};
See CSW_BDisplay in the source for a complicated example.
MemViewer History
v2.0 is on process
v1.0 (01/December/2001) First public release

Comments
Did not Work !
Posted by Legacy on 08/29/2003 12:00amOriginally posted by: marudhu
I tried with various address and the '?' is the all content I see.
ReplyI used win2k.
Tried with rebuilding all...and nothing works.
2 Questions
Posted by Legacy on 05/24/2003 12:00amOriginally posted by: Zhefu Zhang
1. May I say that the mem viewer is actually reading its own process virtual mem space instead of the physical mem, for all the code running in GUI user mode stuffs. More specific, it only read its memview.exe space.
2. Where I could find material stating 0x000FF5C points to the CMOS data, I am using a Win2k Server + SP3, in English. And I got all zero around this address.
Thx ahead and look forward to hearing from u soon. I have to say yr GUI is nice :=)
Have a nice day
Jeff
-
ReplyWhat I was looking for
Posted by ovaisreza on 09/04/2005 05:19pmThis project is not complete or author does not know what he is doing. You need kernal mode driver to accesss LDT and perhapse access other process memory, this would only access current process's memory (Which is it self)
ReplyNice work, Thanks.
Posted by Legacy on 12/07/2001 12:00amOriginally posted by: Jacky Leung
Nice work, Thanks.
ReplyIt runs well !
Posted by Legacy on 12/04/2001 12:00amOriginally posted by: joezhou
It runs well on my win2000.
ReplyDemo Project is not complete !
Posted by Legacy on 12/03/2001 12:00amOriginally posted by: Hans Wedemeyer
As per subject.
It is not possible to compile the demo project.
at least send a make file...
Otherwise this looks very interesting...
I would like to see the demo
Thanks
Replycompilaion error
Posted by Legacy on 12/03/2001 12:00amOriginally posted by: airman
MemView.exe - 1 error(s), 0 warning(s)
Reply\microsoft visual studio\vc98\include\oaidl.h(6827) : fatal error C1073: Internal error involving incremental compilation(compiler file '.\p0io.c', line 1128)
what is oaidl.h?