Click here to Skip to main content
11,641,211 members (62,702 online)
Click here to Skip to main content
Add your own
alternative version

Shared memory

, 28 Sep 2002 156.1K 4.6K 80
Share variables in the shared memory across processes
#include "stdafx.h"
#include "helpers.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BOOL GetErrorDescription( DWORD error_code, CString& error_string )
{
	BOOL rc = TRUE;

	error_string.Empty();

	LPVOID message_buffer = NULL;

	::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
					NULL,
					error_code,
					MAKELANGID( LANG_NEUTRAL, SUBLANG_SYS_DEFAULT ),
					(LPTSTR) &message_buffer,
					0,
					NULL );

	error_string = (LPCTSTR) message_buffer;

	::LocalFree( message_buffer );
	message_buffer = NULL;

	if ( error_string.GetLength() == 0 ) {
		CString frm = _T("Unknown error.");
		error_string.Format(frm, error_code );
		rc = FALSE;
	}

	return rc;
}

void DumpErrorCode(DWORD dwErrCode, DWORD dwLine , char *szFileName)
{
#ifdef _UNICODE

	DWORD dwFileName = MultiByteToWideChar(CP_ACP, 0, szFileName, -1, NULL, 0);
	WCHAR *wszFileName = new WCHAR[dwFileName];
	MultiByteToWideChar(CP_ACP, 0, szFileName, -1, wszFileName, dwFileName);

	CString str;
	GetErrorDescription(dwErrCode,str);
	TRACE(_T("%s(%d): 0x%.8x: %s"),wszFileName,dwLine,dwErrCode,str);

	delete [] wszFileName;
#else
	CString str;
	GetErrorDescription(dwErrCode,str);
	TRACE(_T("%s(%d): 0x%.8x: %s"),szFileName,dwLine,dwErrCode,str);
#endif
}

void UsrDump(TCHAR *msg,BYTE *pData, int len)
{
	try {
		va_list argList;
		va_start(argList, len);
		CString trmsg;

		if (pData == NULL) return;

		trmsg.Format(msg,argList);

		TRACE(_T("%s%s"),trmsg,_T("\n"));
		//TRACE(_T("\n"));
		TRACE(_T("Address:\t0x%.8x\nOffset\t\tData\n"),pData);
		//TRACE(_T("Offset\t\tData\n"));

		CString str;

		int c = len / 16;

		for (int i = 0; i < c; i++) {
			str.Format(_T("0x%.8x\t%.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n"),
				i*16,
				pData[0],pData[1],pData[2],pData[3],pData[4],pData[5],pData[6],pData[7],
				pData[8],pData[9],pData[10],pData[11],pData[12],pData[13],pData[14],pData[15],
				GetPrintCharOrDot(pData[0]),GetPrintCharOrDot(pData[1]),GetPrintCharOrDot(pData[2]),GetPrintCharOrDot(pData[3]),GetPrintCharOrDot(pData[4]),GetPrintCharOrDot(pData[5]),GetPrintCharOrDot(pData[6]),GetPrintCharOrDot(pData[7]),
				GetPrintCharOrDot(pData[8]),GetPrintCharOrDot(pData[9]),GetPrintCharOrDot(pData[10]),GetPrintCharOrDot(pData[11]),GetPrintCharOrDot(pData[12]),GetPrintCharOrDot(pData[13]),GetPrintCharOrDot(pData[14]),GetPrintCharOrDot(pData[15]));
			TRACE(str);
			pData += 16;
		}

		c = len % 16;

		CString numbers;
		if (c != 0) {
			str.Format(_T("0x%.8x\t"),i*16);
			numbers += str;
			//TRACE(str);
		}

		for (int j = 0; j < c; j++) {
			str.Format(_T("%.2x "),pData[j]);
			numbers += str;
			//TRACE(str);
		}
		//TRACE(numbers);

		CString spaces;
		if (c != 0) {
			int k = c*3;
			for (j = k; j < 47; j++) {
				//TRACE(_T(" "));
				spaces += _T(" ");
			}
			//TRACE(_T(" "));
			spaces += _T(" ");
		}
		//TRACE(numbers+spaces);

		CString chars;
		for (j = 0; j < c; j++) {
			str.Format(_T("%c"),GetPrintCharOrDot(pData[j]));
			//TRACE(str);
			chars += str;
		}
		//TRACE(numbers + spaces + chars);

		if (c != 0) {
			TRACE(numbers + spaces + chars + _T("\n"));
		}

		va_end(argList);

	} catch (...) {
		TRACE(_T("LINE %d: Unknown exception.\n"),__LINE__);
		::SetLastError((unsigned long)NTE_FAIL);
	}
}

CString GetDumpString(BYTE *pData, int len)
{
	CString dumped;

	try {
		CString str;

		int c = len / 16;

		for (int i = 0; i < c; i++) {
			str.Format(_T("0x%.8x     %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"),
				i*16,
				pData[0],pData[1],pData[2],pData[3],pData[4],pData[5],pData[6],pData[7],
				pData[8],pData[9],pData[10],pData[11],pData[12],pData[13],pData[14],pData[15],
				GetPrintCharOrDot(pData[0]),GetPrintCharOrDot(pData[1]),GetPrintCharOrDot(pData[2]),GetPrintCharOrDot(pData[3]),GetPrintCharOrDot(pData[4]),GetPrintCharOrDot(pData[5]),GetPrintCharOrDot(pData[6]),GetPrintCharOrDot(pData[7]),
				GetPrintCharOrDot(pData[8]),GetPrintCharOrDot(pData[9]),GetPrintCharOrDot(pData[10]),GetPrintCharOrDot(pData[11]),GetPrintCharOrDot(pData[12]),GetPrintCharOrDot(pData[13]),GetPrintCharOrDot(pData[14]),GetPrintCharOrDot(pData[15]));
			if (len > 16) {
				str += (TCHAR)13;
				str += (TCHAR)10;
			}
			dumped += str;
			pData += 16;
		}

		c = len % 16;

		CString numbers;
		if (c != 0) {
			str.Format(_T("0x%.8x     "),i*16);
			numbers += str;
		}

		for (int j = 0; j < c; j++) {
			str.Format(_T("%.2x "),pData[j]);
			numbers += str;
		}

		CString spaces;
		if (c != 0) {
			int k = c*3;
			for (j = k; j < 47; j++) {
				spaces += _T(" ");
			}
			spaces += _T("  ");
		}

		CString chars;
		for (j = 0; j < c; j++) {
			str.Format(_T("%c"),GetPrintCharOrDot(pData[j]));
			chars += str;
		}

		if (c != 0) {
			dumped += numbers + spaces + chars + (TCHAR)13;
			dumped += (TCHAR)10;
		}

	} catch (...) {
		TRACE(_T("LINE %d: Unknown exception.\n"),__LINE__);
		::SetLastError((unsigned long)NTE_FAIL);
	}

	return dumped;
}

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 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

Share

About the Author

Martin Ziacek
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 29 Sep 2002
Article Copyright 2001 by Martin Ziacek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid