This article combines some of the relevant articles I found on the internet here to tidy up:
In the later version of VS, VC6 can get the name of the function in the function body through __funcsig__;
But to do it yourself in VC6, here's how I found it:
Method 1:
#pragma once
#include <windows.h>
typedef unsigned long ulong_ptr,*pulong_ptr;
#define __OUT_ECOUNT_OPT (x)
#include <dbghelp.h>
#include <stdio.h>
#pragma comment (lib, "Dbghelp.lib")
Static LPSTR Getsymbolsearchpath ()
{
char directory [MAX_PATH];
Char Drive [MAX_PATH];
HMODULE module;
LPSTR path = new char [MAX_PATH];
memset (Path,0,max_path * sizeof (path[0));
Oddly, the symbol handler ignores the "link to" PDB embedded in the
Executable image. So, we ' ll manually add the location of the executable
To the search path since this is often where the PDB would be located.
Path[0] = ' the ';
module = GetModuleHandle (NULL);
GetModuleFileNameA (module, path, MAX_PATH);
_splitpath (path, drive, directory, NULL, 0);
strcpy (path, drive);
strcat (path, directory);
Append the working directory.
strcat (Path, ";. \\");
return path;
}
Class mydbg
{
Public
mydbg (DWORD dwaddr)
{
memset (szfuctionname,0,sizeof (szfuctionname));
BYTE Symbolbuffer [sizeof (SYMBOL_INFO) + 256 * sizeof (CHAR)] = {0};
Psymbol_info Psymbol = (psymbol_info) &symbolBuffer;
psymbol->sizeofstruct = sizeof (Symbol_info);
Psymbol->maxnamelen = 256;
if (Symfromaddr (GetCurrentProcess (), dwaddr, 0, Psymbol))
{
strcpy (Szfuctionname, psymbol->name);
}
Else
{
printf ("Symfromaddr failed! %d\n ", GetLastError ());
}
}
CHAR szfuctionname[32];
};
#define INIT_SYM (finvadeprocess) \
LPSTR SymbolPath = Getsymbolsearchpath (); \
Syminitialize (GetCurrentProcess (), SymbolPath, finvadeprocess); \
Delete[] SymbolPath;
#define UNINIT_SYM () \
Symcleanup (GetCurrentProcess ());
Static DWORD Geteip ()
{
DWORD dwcalleraddr;
__asm
{
Push DWORD Ptr[ebp+4]
Pop DWORD Ptr[dwcalleraddr]
Sub DWORD ptr[dwcalleraddr],5//minus the length of call GETEIP ()
}
return dwcalleraddr;
}
#define __FUNC_FOR_VC6__ (mydbg (Geteip ()). Szfuctionname)
C + + code
Test.cpp #include "stdafx.h"//#include "stdlib.h" #include "dbghelpapi.h"//#pragma comment (lib, "Dbghelp.lib") I NT Main (int argc, char * argv[]) {init_sym (TRUE) printf ("This is <%s> fuction\n", __func_for_vc6__); Uninit_sym () return 0; }
Output
This is <main> fuction
Description
The "Generate Debug Information" option must be selected in the settings under release
Copy C:\Program the DbgHelp.Lib under Files\Microsoft Sdks\windows\v6.0a\lib to the current folder;
Method Two: Http://www.codeproject.com/KB/debug/extendedtrace.aspx
Method Three: Http://www.codeproject.com/KB/tips/xtrace.aspx
Methods Two and three basically belong to the same method, on my machine (win7 + VC6) did not get the expected;
Method Four: is to write a DLL in VC, this DLL can return the current function name (of course, using ready-made __funcsig__), through the VC6 call the function in this DLL to get the function name in VC6;