有谁知道如果 TOSVersion.Name 仍然适用于 Windows 10?
我有一个 vcl 应用程序,它有一个表单显示事件,获取操作系统的详细信息,并使用来自 SysUtils 的 TOSVersion 记录在 TMemo 框中显示它们。
with mmoOSInfo.Lines do
begin
Clear;
Add(TOSVersion.ToString);
Add('');
Add('Architecture: ' + OSArchitectureToStr(TOSVersion.Architecture));
Add('Platform: ' + OSPlatformToStr(TOSVersion.Platform) +
IntToStr(PlatformFromPointer));
Add('Build: ' + IntToStr(TOSVersion.Build));
Add('Major: ' + IntToStr(TOSVersion.Major));
Add('Minor: ' + IntToStr(TOSVersion.Minor));
Add('Name: ' + TOSVersion.Name);
Add('Service Pack - Major: ' + IntToStr(TOSVersion.ServicePackMajor));
Add('Service Pack - Minor: ' + IntToStr(TOSVersion.ServicePackMinor));
end;
代码执行没有任何问题在 XP(是的,我们仍在使用它(挂在耻辱)),Vista,Windows 7,Windows 8.1,台式电脑,笔记本电脑和 Suce Pro 的,但不是安装在 Windows 10 上。
当我使用 paserver 进行调试时,TOSVersion.Name 返回为:= 'Windows 8'。我做错了什么还是我期望 TOSVersion 检测 Windows 10 太多?没有异常被触发。在我可以访问的 2 x Windows 10 计算机中,一个迁移路径来自 Windows 8.1,另一个来自 Windows 7。
非常感谢
有两件事阻止你的代码返回正确的版本:
您使用的 XE8 RTL 早于 Windows 10,因此不了解 Windows 10。
您的可执行文件不表现为支持 Windows 10,所以GetVersionEx
,其中TOSVersion
依赖,将谎言的版本。
我相信 XE8 update 1 会将版本检测更改为使用NetWkstaGetInfo
,而不受这个版本的影响。虽然对NetWkstaGetInfo
的调用确实会泄漏内存,但这可能并不重要,因为它只被调用一次。
与此主题相关的一些链接:
Operating system version changes in Windows 8.1 and Windows Server 2012 R2 Why Windows 8.1 Sometimes Tells You It Is Windows 8.0GetVersionEx
Targeting your application for Windows
还有更多…
如果您绝对必须向用户报告版本,那么您有多种选择:
将supportedOS
选项添加到您的清单中,并包含 Windows 10 的 GUID。这将阻止GetVersionEx
说谎。然后使用TOSVersion
的修改版本或其他方式获取版本。
使用 WMI 查询。
CallNetServerGetInfo
.
CallNetWkstaGetInfo
.
CallRtlGetVersion
.
在这个问题的更多细节:How to detect true Windows version?虽然请注意,接受的答案有过时的。
作为 WMI 方法的示例,您可以使用以下代码:
function OperatingSystemDisplayName: string;
function GetWMIObject(const objectName: string): IDispatch;
var
chEaten: Integer;
BindCtx: IBindCtx;
Moniker: IMoniker;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkPDisplayName(BindCtx, PChar(objectName), chEaten, Moniker));
OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;
function VarToString(const Value: OleVariant): string;
begin
if VarIsStr(Value) then begin
Result := Trim(Value);
end else begin
Result := '';
end;
end;
function FullVersionString(const Item: OleVariant): string;
var
Caption, ServicePack, Version, Architecture: string;
begin
Caption := VarToString(Item.Caption);
ServicePack := VarToString(Item.CSDVersion);
Version := VarToString(Item.Version);
Architecture := ArchitectureDisplayName(SystemArchitecture);
Result := Caption;
if ServicePack <> '' then begin
Result := Result + ' ' + ServicePack;
end;
Result := Result + ', version ' + Version + ', ' + Architecture;
end;
var
objWMIService: OleVariant;
colItems: OleVariant;
Item: OleVariant;
oEnum: IEnumvariant;
iValue: LongWord;
begin
Try
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT Caption, CSDVersion, Version FROM Win32_OperatingSystem', 'WQL', 0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, Item, iValue)=0 then begin
Result := FullVersionString(Item);
exit;
end;
Except
// yes, I know this is nasty, but come what may I want to use the fallback code below should the WMI code fail
End;
(* Fallback, relies on the deprecated function GetVersionEx, reports erroneous values
when manifest does not contain supportedOS matching the executing system *)
Result := TOSVersion.ToString;
end;
你试过使用自定义清单吗?
我使用 XE8,并且在使用针对 Windows 8.1 和 Windows 10 的清单文件时,TOSVersion 识别 Windows 10 没有问题。
我的自定义清单是为了让我的应用程序“Windows 10”意识到。
它是以下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
type="win32"
name="MrTheV Dev"
version="11.0.2804.9245"
processorArchitecture="*"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="asInvoker"
uiAccess="False"/>
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-80f6d0da78}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3f0}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
</application>
</compatibility>
</assembly>
我从这个链接:https://msdn.microsoft.com/fr-fr/library/windows/desktop/dn481241(v=vs.85).aspx
和 Windows 10 1607 上的 TOSVersion.Tostring 显示:
Windows (Version 10.0, Build 14393, 64-bit Edition)
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(88条)