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

2007年10月21日 20:46:00
最近不少朋友反映在编译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.youkuaiyun.com/atfield
http://blogs.msdn.com/yizhang
转载请注明出处



Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1836028


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值