托管代码操作不会更改属性

时间:2018-03-29 19:52:05

标签: c# .net dll installer installshield

我正在尝试在InstallShield套件项目中使用托管代码操作。

我按照他们的榜样希望这不会有什么大不了的。

http://helpnet.flexerasoftware.com/installshield21helplib/helplibrary/SteActionManagedCd.htm

理想情况下,我想让我的.dll方法在执行操作时更改安装屏蔽属性。当我测试安装程序以查看属性是否更改时,我得到了我设置的默认值。

也许我做错了,任何建议都会非常感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Web.Administration;

namespace IISHelp
{
  [Guid("BAFAEAED-08C6-4679-B94E-487A4D89DE63")]
  [TypeLibType(4288)]
  public interface ISuiteExtension
  {
    [DispId(1)]
    string get_Attribute(string bstrName);
    [DispId(2)]
    void LogInfo(string bstr);
    [DispId(3)]
    string get_Property(string bstrName);
    [DispId(3)]
    void set_Property(string bstrName, string bstrValue);
    [DispId(4)]
    string FormatProperty(string bstrValue);
    [DispId(5)]
    string ResolveString(string bstrStringId);
  }

 public class Help
 {
    const UInt32 ERROR_INSTALL_FAILURE = 1603;
    const UInt32 ERROR_SUCCESS = 0; 

    ISuiteExtension GetExtensionFromDispatch(object pDispatch)
    {
        if (Marshal.IsComObject(pDispatch) == false)
            throw new ContextMarshalException("Invalid dispatch object passed to CLR method");
        return (ISuiteExtension)pDispatch;
    }

    public UInt32 IsUniqueName(object pDispatch)
    {
        try
        {
            List<string> currentVirtualDirectories = GetApplicationNames();

            ISuiteExtension suiteExtension = GetExtensionFromDispatch(pDispatch);

            var name = suiteExtension.get_Property("APPLICATION_NAME");

            suiteExtension.set_Property("IS_UNIQUE_APPLICATION_NAME", (!currentVirtualDirectories.Contains(name)).ToString());
        }
        catch (System.ContextMarshalException)
        {
            return ERROR_INSTALL_FAILURE;
        }
        return ERROR_SUCCESS;
    }

    public List<string> GetApplicationNames()
    {
        List<string> currentVirtualDirectories = new List<string>();
        ServerManager mgr = new ServerManager();

        foreach (Site s in mgr.Sites)
        {
            foreach (Application app in s.Applications)
            {
                currentVirtualDirectories.Add(app.Path.Replace('/', ' ').Trim());
            }
        }
        return currentVirtualDirectories.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
    }
  }
}

0 个答案:

没有答案