{"id":23,"date":"2008-02-21T10:21:00","date_gmt":"2008-02-21T10:21:00","guid":{"rendered":"http:\/\/localhost:10265\/?p=22"},"modified":"2008-02-21T10:21:00","modified_gmt":"2008-02-21T10:21:00","slug":"how-to-openclose-a-cd-tray-programatically-using-c","status":"publish","type":"post","link":"https:\/\/mehdi.biz\/blog\/2008\/02\/21\/how-to-openclose-a-cd-tray-programatically-using-c\/","title":{"rendered":"How to open\/close a CD tray programatically using C#?"},"content":{"rendered":"<!-- google_ad_section_start --><p>I was recently asked to present how to open\/close a CD tray programatically using C#. The Media Control Interface (MCI) is the way to go. According to MSDN, the Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind of multimedia device.&#8221;<\/p>\n<p>You can interact with the MCI devices using two different command types, say, Command Messages and Command Strings. The command message addresses the MCI device using constants and structures while the command string provides a textual version of the command messages.<\/p>\n<p>Using the Command Strings under .NET Framework is straight forward, since it doesn&#8217;t interact with the structures that have to be usually DllImported and\/or redefined under the CLR. Therefore, I&#8217;m going to use the Command Strings to get the job done. Here&#8217;s the steps we&#8217;ve to take:<\/p>\n<ul>\n<li>Open the handle to the cd-rom drive. (To make things easier, we&#8217;ll assign an alias to the cd-rom drive letter).<\/li>\n<li>Send the Open\/Close door command to the aliased cd-rom drive.<\/li>\n<li>Close the handle to the cd-rom drive.<\/li>\n<\/ul>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstring drive = &quot;g&quot;;\nstring openHandleCmd = string.Format(&quot;open {0}:\\ type cdaudio alias cd_drive&quot;, drive);\nstring closeHandleCmd = &quot;close cd_drive&quot;;\nstring openCmd = &quot;set cd_drive door open&quot;;\nstring closeCmd = &quot;set cd_drive door closed&quot;;\n\n\/\/Open the CD-ROM's door.\nmciSendString(openHandleCmd, null, 0, IntPtr.Zero);\nmciSendString(openCmd, null, 0, IntPtr.Zero);\nmciSendString(closeHandleCmd, null, 0, IntPtr.Zero);\n\n\/\/Close the CD-ROM's door.\nmciSendString(openHandleCmd, null, 0, IntPtr.Zero);\nmciSendString(closeCmd, null, 0, IntPtr.Zero);\nmciSendString(closeHandleCmd, null, 0, IntPtr.Zero);\n<\/pre>\n<p>Where drive variable indicates the g drive (the cd-rom drive). Then add the following mciSendString declaration somewhere in your class:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;DllImport(&quot;winmm.dll&quot;, EntryPoint = &quot;mciSendStringW&quot;, CharSet = CharSet.Unicode)]\nprotected static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback);\n<\/pre>\n<p>FYI: DllImport resides in the System.Runtime.InteropServices namespace.<\/p>\n<p>That&#8217;s all for now, folks!<\/p>\n<!-- google_ad_section_end -->","protected":false},"excerpt":{"rendered":"<p>I was recently asked to present how to open\/close a CD tray programatically using C#. The Media Control Interface (MCI) is the way to go. According to MSDN, the Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":0,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}