How to create instance of MsftDiscMaster2 in C#

Well... its kinda bittersweet when after 10 minutes composing a compelling post on a technical problem, you figure out the problem right before you hit submit. The tide turns on the bitter side when you spent most of your time formatting the post than composing the text. So... to precent wastage of "knowledge", I post my question, as well as the answer, in the hope that it will help somebody.

I was tinkering with the CD-burning API in the Windows Vista SDK, and one of the basic classes needed for that is MsftDiscMaster2. The MSDN example is in VBScript and does this to create an object of type MsftDiscMaster2:


Now, to port this to C-Sharp, the first thing that I tried was this:

MsftDiscMaster2 g_DiscMaster = (MsftDiscMaster2)

It showed me a runtime excption: Cannot create an instance of an interface.

However, when I try this round-about way, it works!

MsftDiscMaster2 g_DiscMaster = (MsftDiscMaster2)
Microsoft.VisualBasic.Interaction.CreateObject("IMAPI2.MsftDiscMaster2", "");

So, how do I create an object that inherits from MsftDiscMaster2 in C#? What magic is Visual Basic doing that allows it to create an object out of an interface?

Solution: Just create an instance of the MsftDiscMaster2Class class. That's it... it is a concrete implementation of the MsftDiscMaster2 interface. Simple, isn't it?


Popular posts from this blog

Trouble installing KB2881553

February 5, 2005 Kshitij Day