Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C++

XPEInfo - a non-MFC class to get info from PE file

Rate me:
Please Sign up or sign in to vote.
4.95/5 (21 votes)
11 Dec 2008CPOL4 min read 44.4K   988   53  
The XPEInfo APIs allow you to extract information from a PE file. Included in the download is a Windows demo app, and a console app that tests whether a PE file is valid, whether it is 64-bit, contains debug info, is a .Net executable, or is signed. Sample cmd files are provided.
// HDPEInfo.cpp  Version 1.0 - see article at CodeProject.com
//
// Author:  Hans Dietrich
//          hdietrich@gmail.com
//
// Description:
//     HDPEInfo implements a command-line utility to extract information from
//     a PE file.
//
// Usage:
//     HDPEINFO [switch] filename
//     /6   Check if 64-bit PE file
//     /d   Check if PE file contains debug info
//     /h   Display usage
//     /n   Check if .Net PE file
//     /s   Check if signed
//     /v   Check if valid PE file (default if no switches specified)
//     Note:  only one switch may be used at a time
//
//     HDPEINFO returns -1 if there is an error (bad switch or no filename)
//                       0 if the condition being tested for is false
//                       1 if the condition being tested for is true
//                       2 usage displayed
//
// History
//     Version 1.0 - 2008 December 11
//     - Initial public release
//
// License:
//     This software is released under the Code Project Open License (CPOL),
//     which may be found here:  http://www.codeproject.com/info/eula.aspx
//     You are free to use this software in any way you like, except that you 
//     may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this 
//     software may cause.
//
///////////////////////////////////////////////////////////////////////////////

//#include "stdafx.h"
#include "XGetopt.h"
#include "XTrace.h"
#include "..\..\demo app\src\XPEInfo.h"
#include <io.h>

#pragma warning(disable : 4996)	// disable bogus deprecation warning

BOOL b64bit = FALSE;
BOOL bDebug = FALSE;
BOOL bDotNet = FALSE;
BOOL bSigned = FALSE;
TCHAR *pszFile = NULL;


//=============================================================================
void usage()
//=============================================================================
{
	_tprintf(_T("HDPEINFO v1.0 - get info from PE file - (c) 2008 Hans Dietrich\n\n"));
	_tprintf(_T("Usage:  HDPEINFO [switch] filename\n"));
	_tprintf(_T("        /6   Check if 64-bit PE file\n"));
	_tprintf(_T("        /d   Check if PE file contains debug info\n"));
	_tprintf(_T("        /h   Display usage\n"));
	_tprintf(_T("        /n   Check if .Net PE file\n"));
	_tprintf(_T("        /s   Check if signed\n"));
	_tprintf(_T("        /v   Check if valid PE file (default if no switches specified)\n"));
	_tprintf(_T("        Note:  only one switch may be used at a time\n"));
	_tprintf(_T("\n"));
	_tprintf(_T("   HDPEINFO returns -1 if there is an error (bad switch or no filename)\n"));
	_tprintf(_T("                     0 if the condition being tested for is false\n"));
	_tprintf(_T("                     1 if the condition being tested for is true\n"));
	_tprintf(_T("                     2 usage displayed\n"));
	_tprintf(_T("\n"));
	_tprintf(_T("For more help visit http://www.codeproject.com/KB/files/xpeinfo.aspx\n"));
	_tprintf(_T("\n"));
	exit(2);
}

//=============================================================================
BOOL ProcessCommandLine(int argc, TCHAR *argv[])
//=============================================================================
{
	TRACE(_T("in ProcessCommandLine\n"));

	b64bit = FALSE;
	bDebug = FALSE;
	bDotNet = FALSE;
	bSigned = FALSE;
	pszFile = NULL;

	int c;
	TCHAR szMessage[1000];
	
	while ((c = getopt(argc, argv, _T("6dhnsv"))) != EOF)
	{
		switch (c)
		{
		case _T('6'):
			TRACE(_T("option 6\n"));
			b64bit = TRUE;
			break;

		case _T('d'):
			TRACE(_T("option d\n"));
			bDebug = TRUE;
			break;

		case _T('h'):
			TRACE(_T("option h\n"));
			usage();
			break;

		case _T('n'):
			TRACE(_T("option n\n"));
			bDotNet = TRUE;
			break;

		case _T('s'):
			TRACE(_T("option s\n"));
			bSigned = TRUE;
			break;

		case _T('v'):
			TRACE(_T("option v\n"));
			break;

		case _T('?'):
			_stprintf(szMessage, _T("%s: ERROR: illegal option %s"), 
				argv[0], argv[optind-1]);
			_tprintf(_T("%s\n"), szMessage);
			//::MessageBox(0, szMessage, _T("HDPEInfo"), MB_OK|MB_ICONWARNING);
			TRACE(_T("%s\n"), szMessage);
			return FALSE;
			break;

		default:
			TRACE(_T("WARNING: no handler for option %c\n"), c);
			return FALSE;
			break;
		}
	}

	if (optarg)
	{
		TRACE(_T("optarg = %s\n"), optarg);
		pszFile = optarg;
	}
	return TRUE;
}

//=============================================================================
void _tmain(int argc, TCHAR *argv[])
//=============================================================================
{
	int rc = -1;
	//_tprintf(_T("argc=%d\n"), argc);
	TCHAR szMessage[1000];
	if (argc < 2)
	{
		usage();
	}
	else
	{
		if (ProcessCommandLine(argc, argv))
		{
			if ((pszFile == NULL) || (_taccess(pszFile, 00) == -1))
			{
				_stprintf(szMessage, 
					_T("%s: ERROR: missing file name or file not accessible"), 
					argv[0]);
				_tprintf(_T("%s\n"), szMessage);
				//::MessageBox(0, szMessage, _T("HDPEInfo"), MB_OK|MB_ICONWARNING);
				TRACE(_T("%s\n"), szMessage);
			}
			else if (pszFile)
			{
				CXPEInfo info(pszFile);

				if (b64bit)
					rc = info.Is64Bit() ? 1 : 0;
				else if (bDebug)
					rc = info.IsDebug() ? 1 : 0;
				else if (bDotNet)
					rc = info.IsDotNet() ? 1 : 0;
				else if (bSigned)
					rc = info.IsSigned() ? 1 : 0;
				else
					rc = info.IsValid() ? 1 : 0;
			}
		}
	}
	exit(rc);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions