Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

WinHexView

4.71/5 (6 votes)
4 Jan 2017CPOL2 min read 13K  
A tool that I wrote some time ago called WinHexView which I used it for displaying file's content in different format like hexadecimal, decimal or octal

Introduction

This article, a short one, is about a tool that I wrote some time ago and I called it WinHexView. When I work at home, sometimes I like or I have to check the content of binary files. Coming from a Linux world where the terminal is your best friend, I was disappointed because I had to search for such a tool. And there are some great options!

So, I took a look at WinAPI and I started coding in C++. I cannot say that I created the best tool for inspecting file's content but it does its job. I'm using it successfully at home and at work and I'm really glad that I created it.

Using the Code

For implementing the tool, I used WinAPI (it was, somehow, an obsession to use it even if maybe it's not the best choice) and C++ as the programming language. At the base of the application, I tried to decouple the object that invokes the operation from the one that knows how to perform it. Basically I wanted to have a class ContentFormat...

C++
#pragma once
class ContentFormat
{
public:
	ContentFormat();
	virtual ~ContentFormat();
	void DefaultPrint();
	void AsciiPrint();
	void OneByteOctalDisplay();
	void CharacterDisplay();
	void DecimalDisplay();
	void Configure(byte * buffer, DWORD bufferSize);
	std::string GetFormattedOutput();

private:
	byte *gBuffer;
	DWORD gBufferSize;
	const byte gNoOfBytes = 8;
	const UINT32 gStartOffsetValue = 0x00000000;
	const byte gOffsetIncrement = 0x10;
	std::stringstream gContentStream;
};

...that has methods for different output formats like printing ASCII values, characters, decimal values, etc. and a class Command that calls one of those methods.

C++
#pragma once

class Command
{
public:
	Command(std::shared_ptr<ContentFormat> obj);
	virtual ~Command();
	void SetExecuteMethod(void(ContentFormat::*method)());
	void Execute();
private:
	std::shared_ptr<ContentFormat>gObj;
	void(ContentFormat::*gMethod)();

};

Basically, I implemented a sort of Command Design Pattern.

For reading the files, I created a class FileReader as a layer over the needed WinAPI calls.

C++
#pragma once
class FileManagementException :public std::exception
{
private:
	std::string g_msg;
public:
	FileManagementException(const std::string& msg) :g_msg(msg)
	{}

	FileManagementException() :g_msg("FileManagementException thrown!"){}
	const char *what() const
	{
		return g_msg.c_str();
	}
};
class FileReader
{
public:
	FileReader();
	virtual ~FileReader();
	void *LoadFileContent(DWORD *bufferSize) throw (std::exception);
	void SetInputFile(std::wstring filePath);
private:
	DWORD Read(HANDLE hFile, void *buffer, INT64 count)throw (std::exception);
	std::wstring gFilePath;
	HANDLE gFileHandle;
};

Points of Interest

As I probably mentioned, WinHexView is a command line tool for displaying a specified file's content in hexadecimal, decimal, octal format, etc. For me, it's all I needed for inspecting content of files in hex. I could write it in C# or CLI, but at that moment, I thought that it is more interesting to use C++ and WinAPI.

If someone wants to use it and finds some lines of codes that are considered a bad practice, or bugs, or has some ideas to improve this tool, please feel free to fill an issue on GitHub.

Also the repository contains a doc folder where I stored some markdown documents, a test report, release notes and also a document for review. Those are some of the basic documentations that I started to use in all my projects. If you find an issue, you can fill it or edit the review document.

History

  • Created article
  • Updated article
  • Added link to repository
  • Updated tags
  • Posted as a tip

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)