views:

939

answers:

3

Is there any way to do so? I know its possible to programmatically eject/retract the cd drive SOMEHOW, cause Roxio does that when it prompts me to insert a disk.

Either c# or vb.net is preferable, but c and c++ are okay too as a last resort.

I am nearly positive there is some way to do this, I just don't know the methods to call.

I do understand this is a somewhat unusual request, as Google yielded absolutely nothing when I searched for the methods...

+3  A: 

http://www.geekpedia.com/tutorial174_Opening-and-closing-the-CD-tray-in-.NET.html

Robert Harvey
K, now how to do it in vb? Thanks for answering the c# part!!!!
Cyclone
You might try googling "vb.net eject cd".
Darin Dimitrov
Converted to VB, got it!!!!!!!!
Cyclone
You also might try looking up the definition of "either..or", as in "Either c# or vb is preferable..."
RBarryYoung
+3  A: 

Here is a Linux solution :-), complete with source code and practical application.

cdonner
-1. Relevance? This isn't even the same operating system, let alone the same language or framework.
Adam Robinson
If there ever is a day when humor becomes irrelevant to our profession, I will start looking for a new job!
cdonner
@cdonner: If you want to be funny, post a comment, not something labeled as an "answer". You may find it amusing, but if someone is looking to solve a problem then it's just in the way. I can't believe people have actually voted this up.
Adam Robinson
+4  A: 

Here's an alternative solution to the accepted one, converted from a VB.NET sample:

using System;
using System.IO;
using System.Runtime.InteropServices;

class Test
{
    const int OPEN_EXISTING = 3;
    const uint GENERIC_READ = 0x80000000;
    const uint GENERIC_WRITE = 0x40000000;
    const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;

    [DllImport("kernel32")]
    private static extern IntPtr CreateFile
        (string filename, uint desiredAccess, 
         uint shareMode, IntPtr securityAttributes,
         int creationDisposition, int flagsAndAttributes, 
         IntPtr templateFile);

    [DllImport("kernel32")]
    private static extern int DeviceIoControl
        (IntPtr deviceHandle, uint ioControlCode, 
         IntPtr inBuffer, int inBufferSize,
         IntPtr outBuffer, int outBufferSize, 
         ref int bytesReturned, IntPtr overlapped);

    [DllImport("kernel32")]
    private static extern int CloseHandle(IntPtr handle);

    static void EjectMedia(char driveLetter)
    {
        string path = "\\\\.\\" + driveLetter + ":";
        IntPtr handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, 
                                   IntPtr.Zero, OPEN_EXISTING, 0,
                                   IntPtr.Zero);
        if ((long) handle == -1)
        {
            throw new IOException("Unable to open drive " + driveLetter);
        }
        int dummy = 0;
        DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, 
                        IntPtr.Zero, 0, ref dummy, IntPtr.Zero);
        CloseHandle(handle);
    }

    static void Main()
    {
        EjectMedia('f');
    }
}
Jon Skeet
@Jon, That's the best idea. It assumes things on the hardware. What if your hardware has special driver for eject? You'd better use the Shell eject function, which will make your code shorter and more general.
Elazar Leibovich