This code gets the product code directly from the MSI file. Thus, it allows you to read code without installing a file.
class MsiHandle : SafeHandleMinusOneIsInvalid { public MsiHandle() : base(true) { } protected override bool ReleaseHandle() { return NativeMethods.MsiCloseHandle(handle) == 0; } } class NativeMethods { const string MsiDll = "Msi.dll"; [DllImport(MsiDll, CharSet = CharSet.Unicode, ExactSpelling = true)] public extern static uint MsiOpenPackageW(string szPackagePath, out MsiHandle product); [DllImport(MsiDll, ExactSpelling=true)] public extern static uint MsiCloseHandle(IntPtr hAny); [DllImport(MsiDll, CharSet = CharSet.Unicode, ExactSpelling = true)] static extern uint MsiGetProductPropertyW(MsiHandle hProduct, string szProperty, StringBuilder value, ref int length); [DllImport(MsiDll, ExactSpelling = true)] public static extern int MsiSetInternalUI(int value, IntPtr hwnd); public static uint MsiGetProductProperty(MsiHandle hProduct, string szProperty, out string value) { StringBuilder sb = new StringBuilder(1024); int length = sb.Capacity; uint err; value = null; if(0 == (err = MsiGetProductPropertyW(hProduct, szProperty, sb, ref length))) { sb.Length = length; value = sb.ToString(); return 0; } return err; } } static class Program {
Full example in Subversion at http://ankhsvn.open.collab.net/svn/ankhsvn/trunk/src/tools/Ankh.Chocolatey/ Use the username "guest" and password.
source share