Introduction
I needed the ability to burn data to optical media in several VB.NET applications.
As a starting point, I reviewed Eric Haddan’s CodeProject article that provided a C# application to burn optical media. Please take a look at his article titled “Burning and Erasing CD/DVD/Blu-ray Media with C# and IMAPI2”. Eric’s article provided a great learning tool, thank you Eric. However it was not usable in a VB.NET project. From his work, I developed a plan with two goals:
- Convert Eric’s C# application to a VB.NET application.
- Refactor the application such that the optical recording code was encapsulated into its own project.
With two goals tasked for this, it was decided to create two separate versions of vbBurnMedia
. In version 1.0, Eric’s code would be converted to VB.NET without modification. Once this version was working correctly, a second version, 1.1, would implement the second goal of refactoring the project.
Conversion to VB.NET
The first step was to create a new VB.NET windows project and copy all of Eric’s code and windows layout to the new project. My focus was on the conversion of C# syntax to VB and not on the overall task of what I was doing. As the first step, I added a reference to Imapi2
and Imapi2FS
. Changing C# syntax to VB.NET went pretty quickly without any problems. With syntax converted, I began testing and debugging vbMediaBurn
. I started with the simple tasks such as DetectMedia
and FormatMedia
. Everything went smoothly until I got to the last piece of code to test, BurnMedia
. When I attempted to burn data to a CD, I received an exception:
Unable to cast object of type 'IMAPI2FS.FsiStreamClass' to type 'IMAPI2.IStream'.
I had fallen into the first problem that Eric had warned about in his article. After some thought, I realized I had two choices:
- Convert Interop.cs to a VB.NET file (Interop.vb)
- Use Interop.cs without modification in a separate C# project
After some review of what it would entail to convert Interop.cs to VB.NET class, I quickly decided that was a very bad choice. That left the option of placing Interop.cs in its own C# project. This looked fairly simple to implement. I removed the Imapi2
references and added a new C# project to my project for Interop.cs, I began testing and debugging the code and things looked good, the conversion had gone very smoothly. That was until I found that the _Update
handlers were never processing. After many, many, many hours of frustration trying to figure out the problem, I stumbled across a Google discussion that pointed out the problem (I lost the reference to this web page, but would like to thank the author anyway). You must check the “Make assembly COM visible” checkbox in the Interop project. Go to project Properties, select the Application tab, then select the Assembly Information button.
That fixed the problem. At this point, version 1.0 was operational and the first goal of the project was successfully completed. Total time was about 20 hours of effort which was much more effort than I expected, but still less than starting from scratch.
Points of Interest
I would like to thank Eric for his work, it provided a great starting point and saved an enormous amount of time. As payback to the community, my hope is that vbBurnMedia
will save others time as well.
History
- 2011.May.09; version 1.0 Completed conversion of C# application to VB.NET