1. 程式人生 > >如何檢查,可執行程式,是debug模式?還是release模式?

如何檢查,可執行程式,是debug模式?還是release模式?

Generally no.

There hasn't a reliable way to do this with a native DLL file.

Even you would be able to check what versions of system libraries a program is linked with and it's the debug version, chances are there that it was a debug build. However, one could do a release build and still link with debug libraries, you couldn't recognize it in this situation.

The ones you should check for are the ones with a d suffix, they should also come in a set. So for Visual Studio 2015 and 2017, you should be seeing MFC140d.dll or MFC140ud.dll for debug builds, you will also see vcruntime140d.dll and ucrtbased.dll linked too. These are for the debug configuration. If there isn't the d suffix, MFC140.dll or MFC140u.dll with vcruntime140.dll and ucrtbase.dll, then they were built for release.

One other thing that you could look out for is that if you built them with the debug symbols enabled then the debug directory will contain the path to the debug symbol file (even with release builds).

Then there is the final thing that the linker has the habit of merging sections with release builds. So if your executable only has 5 sections instead of 8 or if you built it for x64 then you need to add one or two (unless you added your own, then increase this count as necessary) then it is more likely to be the release configuration.

This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

if the binary was built with a VERSION resource.

read out the VERSIONINFO of the file. If in the fileflags the VS_FF_DEBUG is set, then it is a debug version.

int GetFileVersion(const char *filename, char *ver)
{
    DWORD dwHandle, sz = GetFileVersionInfoSizeA( filename, & dwHandle );
    if ( 0 == sz )
    {       
        return 1;
    }
    char *buf = new char[sz];
    if ( !GetFileVersionInfoA( filename, dwHandle, sz, & buf[ 0 ] ) )
    {
        delete buf;
        return 2;
    }
    VS_FIXEDFILEINFO * pvi;
    sz = sizeof( VS_FIXEDFILEINFO );
    if ( !VerQueryValueA( & buf[ 0 ], "\\", (LPVOID*)&pvi, (unsigned int*)&sz ) )
    {
        delete buf;
        return 3;
    }
    sprintf( ver, "%d.%d.%d.%d"
            , pvi->dwProductVersionMS >> 16
            , pvi->dwFileVersionMS & 0xFFFF
            , pvi->dwFileVersionLS >> 16
            , pvi->dwFileVersionLS & 0xFFFF
    );
    delete buf;
    return 0;
}