CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    1

    Eject dvd rom in C#

    Hi. I'm pretty new to C# and programming at all. And i wonder how you exactly can eject/open your dvd reader when you press a button. And please be nice and tell me all code, I mean if there are any system. code and such.
    Happy and great full for serious answers Hi. I'm pretty new to C# and programming at all. And i wonder how you exactly can eject/open your dvd reader when you press a button. And please be nice and tell me all code, I mean if there are any system. code and such.
    Happy and great full for serious answers

  2. #2
    Join Date
    Apr 2004
    Posts
    102

    Re: Eject dvd rom in C#

    You'll have to use interop. Here is a link to everything you'll need.

    First, you'll have to call CreateFile to get a handle to the drive. Next, you'll have to call DeviceIOControl to eject the drive. And finally, you'll have to call CloseHandle to close the drive handle.

    Assuming that your eject class is named DVD and that your DVD is the E: drive, you would initiate the ejection process as follows:

    Code:
     DVD.Eject(@"\\.\E:");
    Also, you'll need the following constants:

    Code:
    const uint GENERICREAD = 0x80000000;
    const uint OPENEXISTING = 3;
    const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;
    const int INVALID_HANDLE = -1;
    This is also needed:

    Code:
    using System.Runtime.InteropServices;

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Eject dvd rom in C#

    Use the mciSendString. Here's a link: http://www.devx.com/tips/Tip/13324
    Code:
    Public Class Form1
    
    Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _
    
    lpstrCommand As String, ByVal lpstrReturnString As String, _
    
    ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    mciSendString("Set CDAudio Door Open Wait", 0&, 0&, 0&)
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    mciSendString("Set CDAudio Door Closed Wait", 0&, 0&, 0&)
    End Sub
    
    End Class
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Nov 2009
    Posts
    1

    Re: Eject dvd rom in C#

    Thank's all!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured