• 0

[VB.NET] MSI Manipulation


Question

Hi Guys,

I'm trying to write an app that works with MSI files. What I'm wanting to do is be able to open up the MSI and pull out some key pieces of data such as the GUID, ProductName, ProductVersion etc.

Does anyone know of a class or library that can help me work with MSI files?

Regardz :)

Link to comment
https://www.neowin.net/forum/topic/968772-vbnet-msi-manipulation/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

There are a couple of ways to do this:

1) P/Invoke into the Windows Installer API functions.

2) Use COM

A good reason to move away from VB .NET is that pretty much nobody uses it, so if you switch to C# you'll find a lot more support in terms of these kinds of things.

Here's a start, but I'd probably use P/Invoke, primarily because I hate everything about COM:

Imports WindowsInstaller

Dim retVal As String = String.Empty
Dim classType As Type = Type.GetTypeFromProgID("WindowsInstaller.Installer")
Dim installerObj As Object = Activator.CreateInstance(classType)
Dim installer As Installer = installerObj

Dim database As Database = installer.OpenDatabase(PATH_TO_MSI, 0)
//If you just want ProductVersion, then use "SELECT * FROM Property WHERE Property='ProductVersion'"
Dim view As View = database.OpenView("SELECT * FROM Property")
view.Execute()

Dim record As Record = view.Fetch()

While Not record Is Nothing
	retVal += String.Format("{0}: {1}\n", record.StringData(1), record.StringData(2))
	record = view.Fetch()
End While

Console.WriteLine(retVal)

Here's a C# P/Invoke Version:

[DllImport("msi", CharSet = CharSet.Auto)]
extern static public int MsiOpenDatabase(string path, int persistMode, out IntPtr handle);

[DllImport("msi", CharSet = CharSet.Auto)]
extern static public int MsiDatabaseOpenView(IntPtr database, string query, out IntPtr view);

[DllImport("msi", CharSet = CharSet.Auto)]
extern static public int MsiViewExecute(IntPtr view, IntPtr record);

[DllImport("msi", CharSet = CharSet.Auto)]
extern static public int MsiViewFetch(IntPtr view, ref IntPtr record);

[DllImport("msi", CharSet = CharSet.Auto)]
extern static public int MsiViewClose(IntPtr view);

[DllImport("msi")]
extern static public int MsiCloseHandle(IntPtr handle);

[DllImport("msi", CharSet = CharSet.Auto)]
extern static public int MsiRecordGetString(IntPtr record, uint field, StringBuilder value, ref uint valueSize);

private void button1_Click(object sender, EventArgs e)
{
	IntPtr hDb = IntPtr.Zero;
	IntPtr hView = IntPtr.Zero;
	IntPtr hRecord = IntPtr.Zero;
	uint valueSize = 256;

	int err = MsiOpenDatabase(PATH_TO_MSI, 0, out hDb);
	err = MsiDatabaseOpenView(hDb, "SELECT * FROM Property"/* WHERE Property='ProductVersion'"*/, out hView);
	err = MsiViewExecute(hView, IntPtr.Zero);

	var name = new StringBuilder(256);
	var value = new StringBuilder(256);
	var final = new StringBuilder();

	while (MsiViewFetch(hView, ref hRecord) == 0)
	{
		err = MsiRecordGetString(hRecord, 1, name, ref valueSize);
		valueSize = 256;
		err = MsiRecordGetString(hRecord, 2, value, ref valueSize);
		valueSize = 256;
		if (err == 0)
			final.AppendLine(String.Format("{0}: {1}", name.ToString(), value.ToString()));
		else
			final.AppendLine("****");

		MsiCloseHandle(hRecord);
	}

	MsiViewClose(hView);
	MsiCloseHandle(hView);
	MsiCloseHandle(hDb);

	MessageBox.Show(final.ToString());
}

  • 0

@BoogerJones

What are you talking about? You can find the same resources for VB.NET and C# (and for the very few ones that aren't around you can find translators to deal with the language symantics). Since the languages are exactly the same when compiled...

Microsoft claims the usage of the two language is rougly 50/50 in the .NET developer base... That's why they merged the VB.NET Language Team and the C# Language Team into the new "Managed Language Team"... As the differences between the two languages are symantical in nature.

http://www.infoworld...g-languages-831

  • 0
  On 17/01/2011 at 19:42, Frazell Thomas said:

Microsoft claims the usage of the two language is rougly 50/50 in the .NET developer base...

OK, maybe my "nobody uses it" statement was out-of-bounds. Still, I have a very hard time believing that usage is 50/50, but even if it's true it's irrelevant because I'm speaking to the number of resources/tutorials/support/examples/etc, not the number of devs. For example, the number of C#-tagged questions on StackOverflow is 138,412 versus 2,521 for VB .NET. Or are VB devs so good that they never have questions for SO? I'll admit that MS did a good job by not ignoring VB on MSDN, which some people thought they were going to do.

Sure, you could make an argument that intermediate code is the same for both or that the C# code above can easily be translated to VB with an online converter. But again, I'm talking about availability of support. And that makes life much easier when you're looking for help.

I didn't mean to turn this thread into a VB vs C# debate. I learned the little that I know starting out with VB (back then Excel VBA was one of the easiest ways to get started), so it's got a special place in my heart. But it's a huge downside (to me) to have such a harder time finding example code for it.

  • 0

For the record I converted the P/Invoke code to VB and am using that. Works great. Thanks for all the help guys.

I do plan on moving to C#. I've just got a serious case of cbf at the moment until I get the syntax into my head.

Regardz :)

  • 0
  On 18/01/2011 at 01:40, boogerjones said:

For example, the number of C#-tagged questions on StackOverflow is 138,412 versus 2,521 for VB .NET. Or are VB devs so good that they never have questions for SO? I'll admit that MS did a good job by not ignoring VB on MSDN, which some people thought they were going to do.

As someone who (involuntarily) writes VB.NET, I find that most of my VB support actually comes from C# questions on StackOverflow, and that I will often ask for VB help in C# since (IMO) the C# syntax is prettier and easier to follow. It could be argued as well that C# developers tend to be working on more... adventurous projects too, whereas VB developers in my experience are more often ASP.NET developers, which requires less p/Invoke and other extraordinary stuff that's likely to end up on SO.

I would agree that the ratio is probably not 60/40 (at least not with VB.NET alone, perhaps with VB.NET and VB classic), but I think it tends to be misrepresented.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • This is a liability problem. They aren't simply going to trust that you aren't the driver. I think if they really wanted to they could do something similar to key fobs where they only work if they are in the proximity of the driver's seat. As already pointed out by a Random Stranger, simply having your passenger hit the play button doesn't make it any less distracting for the driver.
    • Windows 11 gets improved app defaults settings and Windows Share in build 22631.5545 by Taras Buria Windows 11 build 22631.5545 is now available for download in the Release Preview Channel of the Windows Insider Program. The update is a pretty minor one, but it still packs some important changes, such as improvements for app defaults in the Settings app, Windows Share enhancements, and a few fixes here and there. With build 22631.5545, Microsoft is giving users in the EEA region more control over default apps in Windows 11, particularly for browser defaults. Now, browser defaults support additional file and link types. Your default browser now pins itself to the taskbar (you can turn this option off), plus you can change your typical PDF viewer with one click (if the browser of choice supports PDF handling). As for Windows Share improvements, the sharing window now includes a preview of the link that you are about to send to someone. The rest of the changelog includes various fixes: [Audit] Fixed: An issue with auditing privilege use created too many security event logs. These logs filled up the system drive and prevented users from signing in. [Authentication] Fixed: This update fixes an issue where domain-joined machines running Windows 11 22H2 or 23H2 couldn’t update their account passwords on Windows Server 2025 domain controllers, which led to trust relationship issues. [Country and Operator Settings Asset (COSA)] Fixed: This update brings profiles up to date for certain mobile operators. [Display Kernel] Fixed: An issue that prevented Remote Desktop Protocol (RDP) connections until you restarted your device. [Network file sharing] Fixed: This update fixes an issue where workstations and servers might stop responding when connecting to resources located on Server Message Block (SMB) shares. [Performance] Fixed: This update addresses an issue that prevented the complete removal of unused language packs and Feature on Demand packages, which previously led to unnecessary storage use and longer Windows Update installation times. [Shell] Fixed: This update resolves an issue where kiosk devices might stop responding after being locked and unlocked by an administrator. [Windows Hello] Fixed: This update fixes an issue that prevented the automatic renewal of expiring certificates in Windows Hello for Business. [Windows Search] Fixed: Windows Search responds very slowly—the Search Box can take over 10 seconds to load before you can use it. You can find the announcement post here.
    • Father's Day is coming, so give your dad some great gifts by Steven Parker Mashup from Depositphotos.com (1) (2) Father's Day is quickly approaching on Sunday, June 15. If you haven't gotten your dad a gift for the occasion, don't sweat it. There are lots of affordable gifts you can buy for Father's Day on Amazon, and if you order one or more of them right now, you can get them shipped to you in time to give them to your dad. Below we have put together some Apple deals, and we'll keep expanding the list as we come across more interesting deals, so be sure to check back. iPad Deals Apple iPad 11" 128GB A16 Tablet $299 -14% now $299 (was $349) Apple iPad Air 11" 128GB M3 Chip Tablet -17% now $499 (was $559) Apple iPad Air 13" 128GB M3 Tablet -12% now $699 (was $799) 2024 iPad Mini A17 Pro 128GB 8.3" Tablet -20% now $399 (was $499) 2024 iPad Pro 11" 256GB M4 OLED Tablet -10% now $899 (was $999) 2024 iPad Pro 13" 256GB M4 OLED Tablet -15% $1099 (was $1299) Apple Pencil (3rd Gen, For Select iPads) -13% now $69 (was $79) Apple Pencil Pro (For Select iPad Pro & Air) -23% now $99 (was $129) AirPods deals Apple AirPods Pro 2 Wireless Earbuds -32% now $169 (was $249) Apple AirPods 4 Spatial Audio Wireless Earbuds -23% now $99 (was $129) Apple AirPods 4 Active Noise Canceling Wireless Earbuds -17% now $149 (was $179) Apple Watch Deals Series 10 GPS 42mm (Sport Band) -25% now $299 (was $399) Series 10 GPS 42mm (Sport Loop) -25% now $299 (was $429) Series 10 GPS 42mm (Sport Loop) -23% now $329 (was $429) Series 10 GPS 46mm (Sport Band) -23% now $329 (was $429) Apple Watch Ultra 2 49mm GPS Smartwatch -8% from $739 (reg $799) Apple Watch SE (2nd Gen) Smartwatch -32% from $169 (was $249) MacBook Deals 2025 MacBook Air 13.6" M4 Chip Laptop (16GB/256GB) -15% now $849 (was $999) 2025 MacBook Air 15.3" M4 Chip Laptop (16GB/256GB) -13% now $1049 (was $1199) 2024 MacBook Pro M4 14.2" Laptops -11% from $1429 (was $1599) 2024 MacBook Pro M4 16" Laptops -10% from $2249 (was $2499) Mac Deals Mac Mini M4 10-Core CPU 10-Core GPU -8% now $546 (was $599) iMac M4 24" 8-Core CPU/GPU (16GB/256GB) -8% now $1193 (was $1299) iMac M4 24" 10-Core CPU/GPU (16GB/256GB) -7% now $1349 (was $1499) Kindle deals 16GB Kindle Scribe + Premium Pen -25% now $299.99 (was $399.99) 32GB Kindle Scribe + Premium Pen -24% now $320 (was $420) 64GB Kindle Scribe + Premium Pen -22% now $350 (was $450) Samsung 49" Odyssey OLED G9 (G95SC) 240Hz Curved Gaming Monitor -$800 now $999.99 (was $1799) Samsung Galaxy Buds FE True Wireless Bluetooth Earbuds -35% now $64.99 (was $99.99) Samsung Galaxy Tab S10+ -$120 now $879.99 (was $999.99) Samsung Galaxy Watch Ultra 47mm -31% now $449.99 (was $649.99) SAMSUNG Galaxy S25+ -12% now $879.99 (was $999.99) These are just a small selection of the discounts on offer; for more great deals, go to Amazon's Deals page. As an Amazon Associate, we earn from qualifying purchases.
    • HOLY MOTHER, did not see that one coming. if they can get Rick Moranis to participate, this will be me: https://tenor.com/5P2L.gif
    • Micron announced a $200 billion investment plan in the US by Hamid Ganji Memory chipmaker Micron Technology has announced a $200 billion investment plan in the US to bolster its chip manufacturing operations. The investment focuses on Idaho, New York, and Virginia as production and R&D hubs. According to Micron's statement, the company's new investment plan in the US includes $150 billion in domestic memory manufacturing and $50 billion in R&D operations, which can finally create around 90,000 direct and indirect jobs for Americans. "These investments are designed to allow Micron to meet expected market demand, maintain share and support Micron's goal of producing 40% of its DRAM in the U.S.," Micron said. In addition to prior investments, Micron also said it plans to spend $30 billion on building a second leading-edge memory fab in Boise, Idaho, and modernizing its existing manufacturing facility in Manassas, Virginia. The memory chipmaker firm also has an ongoing plan to build a mega fab in New York. Micron's investment brings advanced packaging capabilities to the US, enabling long-term growth in High-Bandwidth Memory (HBM), a key component in AI data centers. "This approximately $200 billion investment will reinforce America's technological leadership, create tens of thousands of American jobs across the semiconductor ecosystem, and secure a domestic supply of semiconductors—critical to economic and national security," Micron CEO Sanjay Mehrotra said. The timeframe of Micron's investment in the US has yet to be determined. Still, it could be accomplished over the next four years as the company hailed Trump's administration for facilitating the investment. Meanwhile, Micron is not the only chip maker to unveil plans to invest hundreds of billions of dollars in the US under the Trump administration. In April, NVIDIA also announced a whopping $500 billion investment in the US to produce AI servers and other related gear. Other tech giants like TSMC, Apple, OpenAI, and SoftBank have also announced massive investment plans under the Trump administration.
  • Recent Achievements

    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • Week One Done
      serfegyed earned a badge
      Week One Done
    • Week One Done
      fashionuae earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      ATLien_0
      261
    3. 3
      +FloatingFatMan
      202
    4. 4
      +Edouard
      168
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!