Showing results for 
Show  only  | Search instead for 
Did you mean: 
Level 4

MsiGetProperty doesnt work for long text

I've an basic MSI Project with a custom action scheduled in the UI Sequence.
This action invokes a function of an C# Dll file stored in the binary table.

Inside of this function I use MsiGetProperty to retrieve a path (basically the SourceDir which was copied into an own property before) of the msi package. This works fine as long as the MSI is installed from a directory with a short path e.g: "C:\Test\"
If the path gets a bit longer (for example you copy the msi to the Windows Desktop, resulting in a path like "C:\Users\Administrator\Desktop\ProductName\Installx64\" -> so just about 50 Characters) the MsiGetProperty seems not to return the appropiate value anymore.
At this point I can see in the MSILog my property was set before and contains the correct value - I just can't get it.

The code I use looks like this:
[DllImport("msi.dll", CharSet = CharSet.Ansi)]
private static extern int MsiGetProperty(IntPtr hInstall, string szName, [Out] StringBuilder szValueBuf, ref int pchValueBuf);

private static String GetPropertyStringValue(IntPtr hInstall, String PropertyName)
int BufferSize = 0;
int GetResult = 0;
StringBuilder sb = new StringBuilder();

GetResult = MsiGetProperty(hInstall, PropertyName, sb, ref BufferSize);

if (ERROR_MORE_DATA == GetResult)
var sb2 = new StringBuilder();
int FullSize = BufferSize + 1;
GetResult = MsiGetProperty(hInstall, PropertyName, sb2, ref FullSize);

sb = sb2;

return sb.ToString();
catch (Exception ex)
MsiSetProperty(hInstall, "ERRORMESSAGE", String.Format("{0}: {1}", PropertyName, "failed with ex"));
MsiSetProperty(hInstall, "ERRORMESSAGE", String.Format("{0}: {1}", PropertyName, ex.ToString()));
return string.Empty;

I tried a lot of things right now:
Using other charset, adding 2, 10, 20 instewaad of just 1 to the buffersize to ensure it is big enough.

I added some kind of "debug.Prints" into my ERRORMESSAGE property to check in the Msi Log if my function is called, check the returned Buffersize and so on.
I seems like any code below the 2nd call of MsiGetProperty is not executed (obviously I would expect an exception message in case of a runtime error), therefore I can't tell what's the returned result-value of that Api call.

The strange thing is this works fine until the property value is shorter than ~30 Characters.
Does anybody have an idea what I'm doing wrong or new to add here?
Labels (1)
0 Kudos
(1) Reply
Level 4

was finally able to figure this out by myself.
In my case the default size of the StringBuilder was to small to contain the full value.
0 Kudos