Implement __function__ in VC6 (VC6 to get function names in the body) __function

Source: Internet
Author: User

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;               

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.