المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Preventing the user from Ejecting the CD during crucial Process.



C# Programming
08-27-2009, 09:04 PM
Hi everyone, I am currently looking for a solution to prevent a user from opening the CD drive during a cruicial burning process. I think I'm close, like real close but the last hurdle is keeping me back.

I've found the right api for the job, but it's unmanaged in C++ and porting to C# is real tricky. So essentially that is the answer I am after.

I've figured out that at some point my solution involves: IOCTL_STORAGE_EJECT_CONTROL and PREVENT_MEDIA_REMOVAL, being used with the function DeviceIoControl()

I have these snippets which I have edited into my code.


/// DeviceIoControl uses IOCTL_STORAGE_EJECT_CONTROL
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
ref long InBuffer,
int nInBufferSize,
ref long OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
[In] ref NativeOverlapped lpOverlapped);

/// AIDS THE USE OF IOCTL_STORAGE_EJECT_CONTROL
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr CreateFile(
string fileName,
[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
int flags,
IntPtr template);

private void SomethingMethod()
{
IntPtr tHandle = CreateFile(@"\\.\" + destVolume, FileAccess.Read, FileShare.ReadWrite, IntPtr.Zero,
FileMode.Open, 0, IntPtr.Zero);

Bool success = DeviceIoControl(something, IOCTL_STORAGE_EJECT_CONTROL, something, something,
something, and so on);
}

I'm sure most of you will recognise that I am importing the dll libraries but the issue I am having is the Structure or Enums (I'm not quite which which is which) as I believe that IOCTL_STORAGE_EJECT_CONTROL and PREVENT_MEDIA_REMOVAL are stored as 'structs' somewhere.

So in brief, all I need to know are what parameters I need to pass to DeviceIoControl to stop the drive from ejecting.

Many Thanks.