Search code examples
c++visual-studio-2008visual-c++commentspragma

How can I retrieve #pragma comment data at run time?


I came across a section on MSDN this evening talking about #pragma options. Specifically, the #pragma comment definition.

Is it possible to pull that information back out at run time (to identify which machine some source code was generated on for example)? If so, how?

For example, how could I retrieve the compile date if I were to add the following #pragma:

pragma comment( user, "Compiled on " __DATE__ " at " __TIME__ ) 

Solution

  • The string "Compiled on " ... is actually in the binary, but it's not directly accessible. What you need to do is actually scan the executable image to find that string. This brings its own set of problems (i.e. how do you find the needle that is your string in the haystack that is the code) without some "marker" to tell you "aha! here it is!"

    Why not do something like this to make your life easier?

    static const char *sCompileInfo = "Compiled on " __DATE__ " at " __TIME__