In one of my projects I needed to be able to write data to a CD. I found a great article at code guru that helped me out:
http://www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c15201
However, I did find one bug in his code. On page three he declares his external functions using the DllImport, here is the bugged one:
public static extern bool
SHGetPathFromIDList(IntPtr ilPtr, string sPath);
The problem I encountered was that after calling the function, my path string was unchanged. The best explanation I am able to come up with is that strings behave like value types. I did not pass the string in as a ref or out parameter, so my instance of it doesn’t actually change when changed inside the function. To fix this I changed the string to a StringBuilder (which is a reference type):
public static extern bool SHGetPathFromIDList(
IntPtr ilPtr, StringBuilder sPath);
Before I called this function, I just created a StringBuilder with a large capacity and it fixed my problem.
My customer only needed this to work in Windows XP, thankfully, because the way Vista handles CD writing seems to mess up this method. When testing on a Vista machine either the drive was considered Not Ready or when it went to do the writing the OS thought the disk was full or write protected.
