使用C ++ Builder时,exe中是否有一个字符串表示文件是否以调试模式构建

时间:2014-02-21 14:07:57

标签: c++builder

我正在使用Embarcaredo的C ++ Builder XE3。

exe文件中是否有任何信息,最好是我可以搜索的字符串,显示文件是在调试模式还是在发布模式下生成的?

另外,有什么办法可以在编译时检测调试模式。换句话说,有没有办法可以做这样的事情,IDE会自动为我定义DEBUG_MODE。

#if defined ( DEBUG_MODE )
// some extra code
#endif

1 个答案:

答案 0 :(得分:2)

在编译时,预定义的宏是:

  • 在调试模式下:_DEBUG
  • 发布模式
  • :NDEBUG

在运行时你可以使用这样的东西(代码可能需要一些调整......):

bool debugBuild()
{
  bool dbg(false);

  // ParamStr(0) holds the complete path to the application
  char *thisFile(AnsiString(ParamStr(0)).c_str());

  // How big the VersionInfo buffer needs to be    
  DWORD unused;
  DWORD verSize = GetFileVersionInfoSize(thisFile, &unused);

  try
  {
    TCHAR *verInfoBuffer = new TCHAR[verSize + 1];

    // Get the sort-of handle we'll use in further VerQueryValue call
    GetFileVersionInfo(thisFile, 0, verSize, verInfoBuffer);

    // Special case. If you pass in \, you get this useful
    // structure passed back.
    unsigned len;
    ::VS_FIXEDFILEINFO *ffi;
    VerQueryValue(verInfoBuffer, "\\", &(void*)ffi, &len);

    dbg = ffi->dwFileFlags & VS_FF_DEBUG;
  }
  __finally
  {
    delete [] p;
  }

  return dbg;
}