使用 Shell32.dll自定义操作 Wix 中的 SHGetFolderPath 函数

本文关键字:中的 SHGetFolderPath 函数 Wix 操作 Shell32 dll 自定义 使用 | 更新日期: 2023-09-27 18:33:20

我必须在C#自定义操作中使用Shell32的SHGetFolderPath函数.dll。 基本上,我要做的是将属性的值传递给MSI,如下所示:

msiexec /i file.msi IPADDRESS="127.0.0.1"

并将值写入 SHGetFolderPath 给出的某个配置文件中

我尝试了以下代码:

namespace SetupCA
{
    public class CustomActions
    {
        [CustomAction]
        [DllImport("shell32.dll")]
        public static extern Int32 SHGetFolderPath(
            IntPtr hwndOwner,           // Handle to an owner window.
            Int32 nFolder,              // A CSIDL value that identifies the folder whose path is to be retrieved.
            IntPtr hToken,              // An access token that can be used to represent a particular user.
            UInt32 dwFlags,             // Flags to specify which path is to be returned. It is used for cases where 
            // the folder associated with a CSIDL may be moved or renamed by the user. 
            StringBuilder pszPath); 
        public static ActionResult WriteFileToDisk(Session session)
        {
            session.Log("Begin WriteFileToDisk");
            const int CSIDL_LOCAL_APPDATA = 0x001c;
            StringBuilder path1 = new StringBuilder(256);
            SHGetFolderPath(IntPtr.Zero, CSIDL_LOCAL_APPDATA, IntPtr.Zero, 0, path1);
            session.Log("LOCAL APP_DATA PATH " + path1.ToString());
            string ipAddress = session["IPADDRESS"];
            //string port = session["PORT"];
            //string path = session["PATH"];  // PATH is of format C:''lpaa''
            string path = path1.Replace(@"'", @"''").ToString();
            path = path + @"''lpa''config''";
            session.Log("LOCAL APP_DATA PATH NOW MODIFIED " + path.ToString());
            string temp = @"
{{
 ""logpoint_ip"" : ""{0}"" 
}}";
            string config = string.Format(temp, ipAddress);
            session.Log("Config Generated was " + config);
            System.IO.Directory.CreateDirectory(path);
            try
            {
                System.IO.File.Delete(path + "lpa.config");
            }
            catch (Exception e)
            {
                session.Log(e.ToString());
            }
            System.IO.File.WriteAllText(path + "lpa.config", config);
            session.Log("Ending WriteFileToDisk");
            return ActionResult.Success;
        }
    }
}

代码编译成功,但在制作 Wix 安装程序并安装它时会出现错误

 A DLL required for this install to complete could not be run.

如何解决此问题?

使用 Shell32.dll自定义操作 Wix 中的 SHGetFolderPath 函数

看起来您的[CustomAction]属性在错误的位置;不应该在WriteFileToDisk而不是SHGetFolderPath吗?

更好的是,阅读session["LocalAppDataFolder"]以跳过P/Invoke。