|
#if defined(_MSC_VER)
#include <string>
#include <windows.h>
#include "..\include\dbghelp.h"
using namespace std;
// Copyright (c) 1998 John Panzer. Permission is granted to
// use, copy, modify, distribute, and sell this source code as
// long as this copyright notice appears in all source files.
void DumpLastError()
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL );
MessageBox(0, (LPCTSTR)lpMsgBuf, "LastError",MB_OK);
LocalFree( lpMsgBuf );
}
// Copyright (c) 1998 John Panzer. Permission is granted to
// use, copy, modify, distribute, and sell this source code as
// long as this copyright notice appears in all source files.
void getFuncInfo(long addr,string &module,string &funcName)
{
HANDLE h = GetCurrentProcess();
if (!SymInitialize(h,NULL,FALSE))
DumpLastError();
TCHAR moduleName[MAX_PATH];
TCHAR modShortNameBuf[MAX_PATH];
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery((void*)addr,&mbi,sizeof(mbi));
GetModuleFileName((HMODULE)mbi.AllocationBase,
moduleName, MAX_PATH );
_splitpath(moduleName,NULL,NULL,modShortNameBuf,NULL);
BYTE symbolBuffer[ sizeof(IMAGEHLP_SYMBOL) + 1024 ];
PIMAGEHLP_SYMBOL pSymbol =
(PIMAGEHLP_SYMBOL)&symbolBuffer[0];
// Following not per docs, but per example...
pSymbol->SizeOfStruct = sizeof(symbolBuffer);
pSymbol->MaxNameLength = 1023;
pSymbol->Address = 0;
pSymbol->Flags = 0;
pSymbol->Size =0;
DWORD symDisplacement = 0;
if (!SymLoadModule(h,
NULL,
moduleName,
NULL,
(DWORD)mbi.AllocationBase,
0) )
DumpLastError();
SymSetOptions( SymGetOptions() & ~SYMOPT_UNDNAME );
char undName[1024];
if (! SymGetSymFromAddr(GetCurrentProcess(), addr,
&symDisplacement, pSymbol) )
{
DumpLastError();
// Couldn't retrieve symbol (no debug info?)
strcpy(undName,"<unknown symbol>");
}
else
{
// Unmangle name, throwing away decorations
// that don't affect uniqueness:
if ( 0 == UnDecorateSymbolName(
pSymbol->Name, undName,
sizeof(undName),
UNDNAME_NO_MS_KEYWORDS |
UNDNAME_NO_ACCESS_SPECIFIERS |
UNDNAME_NO_FUNCTION_RETURNS |
UNDNAME_NO_ALLOCATION_MODEL |
UNDNAME_NO_ALLOCATION_LANGUAGE |
UNDNAME_NO_MEMBER_TYPE))
strcpy(undName,pSymbol->Name);
}
SymUnloadModule(GetCurrentProcess(),
(DWORD)mbi.AllocationBase);
SymCleanup(GetCurrentProcess());
module = modShortNameBuf;
funcName = undName;
}
#endif
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.