Detect if a MSI component is installed

A C# program for those who don’t know MSI SDK or C++. C++ programmers can find the API inside the msi.h  file in Windows SDK.

class Program
{
static int Main(string[] args)
{
uint pathSize = 0;
try
{
foreach (string componentId in args)
{
MsiInstallState state = MsiLocateComponent(
componentId, null, ref pathSize);
if (state != MsiInstallState.Local)
{
return ERROR_UNKNOWN_COMPONENT;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return ERROR_MOD_NOT_FOUND;
}
return 0;
}
[DllImport(“msi.dll”, CharSet = CharSet.Auto)]
extern static internal MsiInstallState MsiLocateComponent(string component, string path, ref uint pathSize);
///

Enumeration of MSI install states.
internal enum MsiInstallState : int
{
Local = 3
}
const int ERROR_MOD_NOT_FOUND = 126;
const int ERROR_UNKNOWN_COMPONENT = 1607;

About Sheng Jiang 蒋晟

Microsoft MVP in Visual C , 2004- Forum moderator of the Visual C and .Net forums on CSDN Forum moderator of Chinese forums on Microsoft's MSDN forums
This entry was posted in Microsoft and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s