2013-06-25 50 views
1

我正尝试端口下面的JavaScript代码来INNO-设置Pascal脚本:无法转换类型(未知)的变体进入式(调度)

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.AdminManager'); 
var appPoolsSection = adminManager.GetAdminSection('system.applicationHost/applicationPools', 'MACHINE/WEBROOT/APPHOST'); 
var appPoolsCollection = applicationPoolsSection.Collection; 
for (var i = 0; i < appPoolsCollection.Count; i++) 
{ 
    var appPool = appPoolsCollection.Item(i); 
    // doing someting with the application pool 
} 

此代码已被翻译成这样:

var AdminManager, AppPoolsSection, AppPoolsCollection, AppPool: Variant; 
    i: Integer; 
begin 
    AdminManager := CreateOleObject('Microsoft.ApplicationHost.AdminManager'); 
    AppPoolsSection := AdminManager.GetAdminSection('system.applicationHost/applicationPools', 'MACHINE/WEBROOT/APPHOST'); 
    AppPoolsCollection := AppPoolsSection.Collection; 
    for i := 0 to AppPoolsCollection.Count-1 do begin 
    AppPool := AppPoolsCollection.Item(i); 
    // doing someting with the application pool 
    end; 
end; 

但它是提高在线AppPoolsCollection := AppPoolsSection.Collection以下错误:

Exception: Could not convert variant of type (Unknown) into type (Dispatch). 

有任何事情我可以做的是通知pascal scritp AppPoolsSection对象是IDispach而不仅是IUnknown

+0

你['IAppHostAdminManager :: GetAdminSection'](http://msdn.microsoft.com/en-us/library/aa965186(V = VS.90)。 aspx)方法调用对我来说看起来很奇怪。它应该有3个参数,其中第三个参数是'AppPoolsSection'变量和'HRESULT'类型的结果。看起来你的代码在将'HRESULT'值传递给'AppPoolsSection'变量时失败了,假设它以后是一个集合。 – TLama

+0

正是从[这里]的JavaScript例的直接端口(http://www.iis.net/configreference/system.applicationhost/sites)。我将使用接口声明来处理它。谢谢。 –

回答

2

我发现,工作和比“进口”的定义的接口比较简单的解决方案。

此代码中使用的所有COM组件都实现了IDispatch(它需要在VBScript或JScript上使用),然后我导入VariantChangeType函数来将IUnknown引用强制转换为IDispatch引用(因为它似乎不受支持帕斯卡脚本)。

跟随最后的代码:

function VariantChangeType(out Dest: Variant; Source: Variant; Flags, vt: Word): HRESULT; external '[email protected] stdcall'; 

function VarToDisp(Source: Variant): Variant; 
begin 
    Result := Unassigned; 
    OleCheck(VariantChangeType(Result, Source, 0, varDispatch)); 
end; 

procedure EnumerateAppPools(AppPools: TStrings); 
var AdminManager, Section, Collection, Item, Properties: Variant; 
    i: Integer; 
begin 
    AdminManager := CreateOleObject('Microsoft.ApplicationHost.AdminManager'); 
    Section := VarToDisp(AdminManager.GetAdminSection('system.applicationHost/applicationPools', 'MACHINE/WEBROOT/APPHOST')); 
    Collection := VarToDisp(Section.Collection); 
    for i := 0 to Collection.Count-1 do begin 
    Item := VarToDisp(Collection.Item(i)); 
    Properties := VarToDisp(Item.Properties); 
    if (VarToDisp(Properties.Item('managedPipelineMode')).Value = 1) and 
     (VarToDisp(Properties.Item('managedRuntimeVersion')).Value = 'v4.0') then 
     AppPools.Add(VarToDisp(Properties.Item('name')).Value); 
    end; 
end; 
相关问题