{"id":16,"date":"2009-04-20T21:36:34","date_gmt":"2009-04-20T21:36:34","guid":{"rendered":"\/blogs\/benbarefield\/archive\/2009\/04\/20\/writing-to-a-cd.aspx"},"modified":"2009-04-20T21:36:34","modified_gmt":"2009-04-20T21:36:34","slug":"writing-to-a-cd","status":"publish","type":"post","link":"https:\/\/www.benbarefield.com\/blog\/2009\/04\/20\/writing-to-a-cd\/","title":{"rendered":"Writing to a CD"},"content":{"rendered":"<p>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:<br \/><a title=\"http:\/\/www.codeguru.com\/csharp\/.net\/net_general\/tipstricks\/article.php\/c15201\" href=\"http:\/\/www.codeguru.com\/csharp\/.net\/net_general\/tipstricks\/article.php\/c15201\">http:\/\/www.codeguru.com\/csharp\/.net\/net_general\/tipstricks\/article.php\/c15201<\/a><\/p>\n<p>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:<\/p>\n<div style=\"border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4\">\n<div style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">static<\/span> <span style=\"color: #0000ff\">extern<\/span> <span style=\"color: #0000ff\">bool<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">   SHGetPathFromIDList(IntPtr ilPtr, <span style=\"color: #0000ff\">string<\/span> sPath);<\/pre>\n<\/div>\n<\/div>\n<p>The problem I encountered was that after calling the function, my path string was unchanged.&nbsp; 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&#8217;t actually change when changed inside the function.&nbsp; To fix this I changed the string to a StringBuilder (which is a reference type):<\/p>\n<div style=\"border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4\">\n<div style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">static<\/span> <span style=\"color: #0000ff\">extern<\/span> <span style=\"color: #0000ff\">bool<\/span> SHGetPathFromIDList(<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    IntPtr ilPtr, StringBuilder sPath);<\/pre>\n<\/div>\n<\/div>\n<p>Before I called this function, I just created a StringBuilder with a large capacity and it fixed my problem.<\/p>\n<p>My customer only needed this to work in Windows XP, thankfully, because the way Vista handles CD writing seems to mess up this method.&nbsp; 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.benbarefield.com\/blog\/2009\/04\/20\/writing-to-a-cd\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}