.NETRotor源码研究1补遗 - 解决无法检测操作系统版本的错误

    技术2022-05-11  39

    最近不少朋友反映在编译Rotor 的时候出现无法检测操作系统版本的错误,具体现象为执行env.bat 的时候报错:  

    Could not get platform OS version

        出现该错误的原因是env.bat 会调用env.core.pl 设置环境,而env.core.pl 在检测操作系统版本的时候会使用到ver 命令的输出结果:

                if (Windows()) {

                                # The output of ver looks like:

                                #

                                # Microsoft Windows XP [Version 5.1.2600]

                                #

                                # (including the empty line before)

     

                                $platform_os_version = `ver`;

                                # we keep the first two fields of the version number

                                if (! ($platform_os_version =~ s//nMicrosoft.*/[Version +([0-9]+/.[0-9]+)/..*/]/$1/g)) {

                                                $platform_os_version = "";

                                }  

                                if ($platform_os_version =~ /^5/..*/) {

                                                $platform_os_version = "5.1";

                                }             } else {

                                $platform_os_version = `uname -r`;

                                # we keep all fields of the version number

                                if (! ($platform_os_version =~ s/^[^0-9]*([0-9.]+).*$/$1/s)) {

                                                $platform_os_version = "";

                                }             }            

                chomp ($platform_os);

                chomp ($platform_os_version);

                if (! $platform_os_version ) {

                                CorFail ("Could not get platform OS version");

                }    

    可以看到env.core.pl使用正则表达式s//nMicrosoft.*/[Version +([0-9]+/.[0-9]+)/..*/]/$1/g来分析Ver的输出结果。如果操作系统为非中文版本或者安装了界面包的情况下,ver命令便有可能输出中文结果,导致脚本出错。虽然可以通过修改系统Locale的方式解决,不过更简单的解决方法显然是直接修改脚本,把$platform_version固定为某个值,比如5.1,修改后代码如下:

                if (Windows()) {

                                # assume 5.1 version because the original code cannot parse the output of ver command correctly in non-English OS

                                $platform_os_version = "5.1";

                } else {

                                $platform_os_version = `uname -r`;

                                # we keep all fields of the version number

                                if (! ($platform_os_version =~ s/^[^0-9]*([0-9.]+).*$/$1/s)) {

                                                $platform_os_version = "";

                                }             }            

                chomp ($platform_os);

                chomp ($platform_os_version);

                if (! $platform_os_version ) {

                                CorFail ("Could not get platform OS version");

                }    

    这里有一个已经修改好的env.core.pl (放在我的Windows Live Skydrive账户里面),有需要的朋友可以点击下载,然后覆盖SSCLI20目录下的env.core.pl即可。

     

    作者:      张羿(ATField) Blog:      http://blog.csdn.net/atfield                 http://blogs.msdn.com/yizhang 转载请注明出处

    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1836028


    最新回复(0)