Get function name from operinter function?

I have a pointer to such functions.

TTestEvent = function(): Boolean; procedure ExecuteTest(aTest: TTestEvent; aType: String); begin if aTest then NotifyLog(aType + ' success') else TestError(aType + ' failed'); end; // Call the test procedure TestAll; begin ExecuteTest(LoadParcels, 'LoadParcel'); end; 

But it would be even better to extract the function name from the ainter function.

So instead

 aType + ' success' 

I need something like

 ExtractName(aTest) + ' success' 

Can this be done in Delphi 2007?

+4
source share
3 answers

You cannot do this with built-in functions. To get the function name from the address, you need to know the map of the executable file. This is not part of the executable unless you take steps to add it.

Debugging tools like JclDebug and madExcept offer the functionality you are looking for.

+4
source

If you use some of our Open Source classes, you can find the name of any character.

You need to create a .map file when creating your executable file by setting the "Detailed map" option in your project.

You can then send the .map using .exe or compress the .map into our own .mab format, which you can add to the .exe . The .mab format .mab much more efficient than .zip or another for this task: it is about 10 times smaller than the original .map file (that is, much smaller than the JCLDebug or MaxExpect suggestions, and much smaller than using the standard implementation of the Remote Debugging Symbol implementation project ").

Then you can use the TSynMapFile class to get debugging information from a .map file or information embedded in .exe :

 function ExtractName(aSymbolAddress: pointer): string; var i: integer; begin with TSynMapFile.Create do // no name supplied -> will read from .exe try i := FindSymbol(aSymbolAddress); if i>=0 then result := Symbols[i].Name else result := ''; finally Free; end; end; 

It will work for function names, as well as any other characters, such as methods or global variables.

See this blog post about the class. Please note that even if it is used by our mORMot framework or its logging features, you just don’t need to use the whole structure (just SynCommons.pas and SynLZ.pas ). See Map2Mab.dpr in the SQLite3 \ Samples \ 11 - Exception logging subdirectory to insert the contents of the .map file into the .exe file.

+6
source

You can implement Dictionary based registration engine with

  • function pointer like Key and
  • function name as Value .

ExtractName will be a dictionary method.

Remember to make it thread safe if required.

0
source

Source: https://habr.com/ru/post/1390784/


All Articles