我们使用以下函数来获取当前引导配置指定的处理器数量.这个数字纯粹用于记录.
以下功能在XP,Vista,7,2003和2008上可以正常工作,但在Windows 2012 Server上失败.
- // -1 = not implemented or not allowed
- // 0 = not limited
- // >0 = number of processors in the {current} boot entry
- function Internal_GetBCDNumberOfProcessors: integer;
- var
- objBcdStore : OleVariant;
- objElement : OleVariant;
- objWBL : OleVariant;
- objWMIService: OleVariant;
- begin
- // for more info,see: https://stackoverflow.com/questions/7517965/accessing-bcdstore-from-delphi/7527164#7527164
- Result := -1;
- try
- objWMIService := GetObject('winmgmts:{(Backup,Restore)}\\.\root\wmi:BcdStore');
- if (not VarIsNull(objWMIService)) and
- boolean(objWMIService.OpenStore('',objBcdStore)) and
- (not VarIsNull(objBcdStore)) and
- boolean(objBcdStore.OpenObject('{fa926493-6f1c-4193-a414-58f0b2456d1e}',objWBL)) and
- (not VarIsNull(objWBL))
- then
- if objWBL.GetElement($25000061,objElement) and //<-- fails here on Server 2012
- (not VarIsNull(objElement))
- then
- Result := StrToIntDef(objElement.Integer,0)
- else
- Result := 0;
- except
- on E: EOleSysError do
- Result := -1;
- end;
- end;
如果我尝试在Win2012上运行它,objWBL.GetElement引发EOleSysError异常,文本OLE错误D0000225. Google找不到与此错误代码相关的任何有意义的内容:(
堆栈跟踪表示异常在System.Win.ComObj.DispatchInvokeError中被触发,由DispatchInvoke调用,由VarDispInvoke调用.
所有这些都是使用XE2进行复制的.我可以尝试重复XE3的问题,但我不相信Delphi RTL与它有任何关系.
有谁有任何想法可能的原因这种行为?
GetElement部分:
- if objWBL.GetElement($25000061,objElement) and //<-- fails here on Server 2012
- (not VarIsNull(objElement))
- then
- Result := StrToIntDef(objElement.Integer,0)
- else
- Result := 0;
可以用EnumerateElements替换:
- if objWBL.EnumerateElements(objArray) then try
- for i := VarArrayLowBound(objArray,1) to VarArrayHighBound(objArray,1) do begin
- objElement := objArray[i];
- if objElement.Type = $25000061 then
- Exit(objElement.Integer);
- end;
- finally VarClear(objArray); end;
这不会引发EOleException,但是遗憾的是还没有找到NumberOfProcessors元素.