Reputation: 1637
I have to use SHGetFolderPath function of Shell32.dll inside C# Custom Action. Basically what I am trying to do is get the value of Property passed to MSI as:
msiexec /i file.msi IPADDRESS="127.0.0.1"
and write the value in some config file given by the SHGetFolderPath
I tried following code:
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;
}
}
}
The code compiles successfully but when making a Wix Installer and installing it gives error
A DLL required for this install to complete could not be run.
How do I fix the issue?
Upvotes: 1
Views: 1133
Reputation: 15905
It looks like your [CustomAction]
attribute is in the wrong place; Shouldn't it be on WriteFileToDisk
instead of SHGetFolderPath
?
Better yet, read session["LocalAppDataFolder"]
instead to skip the P/Invoke.
Upvotes: 2