如何找到提升运行时版本 [英] How to find boost runtime version

查看:109
本文介绍了如何找到提升运行时版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写++库,它使用提升的C值。

I am writing a C++ library that uses boost.

在这个图书馆我想包括有关用来编译我的图书馆的二进制版本增压版本信息。
我可以使用宏 BOOST_VERSION ,这是好的。

In this library I want to include information about the boost version that was used to compile the binary version of my library. I can use the macro BOOST_VERSION and this is fine.

我也想确定这是提升的运行时版本,所以我可以与用来编译我的图书馆增压版本进行比较。很明显,我无法使用宏,因为它会给我硬codeD的版本在编译的时候,而不是在运行时。

I also wanted to determine which is the runtime version of boost so I could compare with the boost version that was used to compile my library. Obviously I cannot use the macro because it will give me the hard coded version at compile time, not at runtime.

我需要的是提升功能(如的boost :: get_version())。有没有办法在升压做到这一点?

What I needed is a function in boost (like boost::get_version()). Is there a way to do this in boost?

推荐答案

您可以使用宏来创建一些code如下:

You can use the macro to create some code as follows:

std::cout << "Using Boost "     
          << BOOST_VERSION / 100000     << "."  // maj. version
          << BOOST_VERSION / 100 % 1000 << "."  // min. version
          << BOOST_VERSION % 100                // patch version
          << std::endl;

这适用于升压1.51.x向上。不知道这是你在找什么,虽然,我会继续下去,看看是否有一种方法可以从当前加载的DLL得到它在一个更优雅的方式。

This works on boost 1.51.x and upwards. Not sure if this is what you are looking for though, I will keep going and see if there is a way to get it from the currently loaded dll in a more elegant way.

要找到运行时版本:

看升压系统后,似乎是最简单的方式,你可以做你在找什么将是有它被用来在编译时做出不同版本的可执行的平台依赖性code

After looking at the Boost system, it seems that the simplest way you could do what you are looking for would be to have platform dependant code which is utilised at compiletime to make the different versions of executable.

您需要使用的 GetFileVersionInfoEx API GetFileVersionInfo API ,你还需要考虑到,如果操作系统是32位或64位,所以这里是一些code这可能是使用的:

You will need to query the DLL for its version using GetFileVersionInfoEx API or GetFileVersionInfo API, you also need to take into account if the OS is 32 bit or 64 bit, so here is some code which may be of use:

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS IsWow64ProcessFunc;

BOOL is64BitOS()
{
    BOOL retval= FALSE;
    // need to check if the OS is 64 bit, and we cant therefore indiscrimately call the 64 bit function for this, so we use the following:
    IsWow64ProcessFunc= (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if(IsWow64ProcessFunc != NULL)
    {
        if (!IsWow64ProcessFunc(GetCurrentProcess(),&bIsWow64))
        {
            // your error handling code in here
        }
    }
    return retval;
}

然后,我们可以利用code的直进位,以获得DLL的版本:

Then we can utilise a straight-forward bit of code to get the version of the DLL:

void myWindowsDLLVersionChecker()
{
    DWORD  versionHandle = NULL;
    UINT   size      = 0;
    LPBYTE buffer  = NULL;
    DWORD  versionSize   = GetFileVersionInfoSize( szVersionFile, &versionHandle);

    if (verSize != NULL)
    {
        LPSTR versionData = new char[versionSize];

        if (GetFileVersionInfo( szVersionFile, versionHandle, versionSize, versionData))
        {
            if (VerQueryValue(versionData,"\\",(VOID FAR* FAR*)&buffer,&size))
            {
                if (size)
                {
                    VS_FIXEDFILEINFO *versionInfo = (VS_FIXEDFILEINFO *)buffer;
                    if (versionInfo->dwSignature == 0xFEEF04BD)  // value from http://msdn.microsoft.com/en-us/library/windows/desktop/ms646997(v=vs.85).aspx
                    {
                        if(is64BitOS())    //  if it is true then its a 64 bit Windows OS
                        {
                                major =     (versionInfo->dwProductVersionMS >> 16) & 0xff;
                                minor =     (versionInfo->dwProductVersionMS >>  0) & 0xff;
                                revision =  (versionInfo->dwProductVersionLS >> 16) & 0xff;
                                build =     (versionInfo->dwProductVersionLS >>  0) & 0xff;
                        } 
                        else //  32 bit Windows OS
                        {
                                major =     HIWORD(versionInfo->dwProductVersionMS);
                                minor =     LOWORD(versionInfo->dwProductVersionMS);
                                revision =  HIWORD(versionInfo->dwProductVersionLS);
                                build =     LOWORD(versionInfo->dwProductVersionLS);
                        }
                    }
                }
            }
        }
        delete[] versionData;
    }
}

对于Linux:

可怕地不记得了code表示,在present,但是这里是从内存中的部分解决方案(我没有一台Linux机器手的那一刻,在后面详细将检查这一点,但应与顶多只有轻微的修改)工作。

For Linux:

Horribly cant remember the code for that at present, however here is a partial solution from memory (I don't have a Linux machine to hand at the moment, will check this in more detail later, but should work with only minor modifications at most).

void myLinuxDLLVersionChecker() 
{
    // call readelf as an external proces son libboost_system.so, 
    // and use the information returned to extract the version from.
    // or use something like
    setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
    FILE *ldd = popen("/lib/libboost_system.so");
    //  information from http://linux.die.net/man/8/ld.so
    // the above pretty much replicates ldd's output, so you can pick out the version
}

结合二:

要在多个操作系统同样的事情发生,你很可能不得不使用这样的:

Combining the two:

To make the same thing happen in multiple OSes you would probably have to use something like:

#ifdef __unix__                    /* __unix__ is usually defined by compilers targeting Unix systems */
void detectBoostDllVersion()  // does not have to be void, can be whatever you need
{
    myLinuxDLLVersionChecker();
}
#elif defined(_WIN32) || defined(WIN32)     /* _Win32 is usually defined by compilers targeting 32 or   64 bit Windows systems */
void detectBoostDllVersion()  // has to match the same return type as the previous version
{
    myWindowsDLLVersionChecker();
}
#endif

现在取决于哪个操作系统这是编译,正确的功能将在由 detectBoostDLLVersion()函数指出​​:)

Now dependant upon which OS this is compiled for, the right function will be pointed at by the detectBoostDLLVersion() function :)

这篇关于如何找到提升运行时版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆