Ejecting CD Drive in Native App

Hi

I am writing a native mode application that can eject the CD ROM drive.
Below is my code

BOOL bRet = FALSE;
UNICODE_STRING strVolumeName;

OBJECT_ATTRIBUTES objAttrsVolume;

PWCHAR pBufVolume;
WCHAR wVolumePath[MAX_PATH];

IO_STATUS_BLOCK ioStatusVolume;
NTSTATUS ntStatVolume;

pBufVolume = RtlAllocateHeap(g_hHeap, 0, MAX_PATH);

swprintf(wVolumePath, L"\??\E:");

RtlInitUnicodeString(&strVolumeName, wVolumePath);

objAttrsVolume.Length = sizeof(OBJECT_ATTRIBUTES);
objAttrsVolume.RootDirectory = NULL;
objAttrsVolume.ObjectName = &strVolumeName;
objAttrsVolume.Attributes = OBJ_CASE_INSENSITIVE;
objAttrsVolume.SecurityDescriptor = NULL;
objAttrsVolume.SecurityQualityOfService = NULL;
ntStatVolume = NtCreateFile(&g_hVolume,
GENERIC_READ,
&objAttrsVolume,
&ioStatusVolume,
0,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
FILE_NON_DIRECTORY_FILE,
NULL,
0);

if(NT_SUCCESS(ntStatVolume))
{
ntStatus = NtDeviceIoControlFile( g_hVolume,
NULL,
NULL,
NULL,
NULL,
IOCTL_STORAGE_EJECT_MEDIA,
NULL,
0,
NULL,
0);
if(NT_SUCCESS(ntStatus))
{
//Display open success
}
}

Though I get the “Open Success” message, the drive is not opening. Could
anyone help me with this.

Thanks
Ramananda

I am getting Access Violation (0xC0000005) when I call
NtDeviceIoControlFile. Please let me know if I am doing something wrong.

Thanks
Ramananda

“Ramananda” wrote in message
news:xxxxx@ntdev…
> Hi
>
> I am writing a native mode application that can eject the CD ROM drive.
> Below is my code
>
> BOOL bRet = FALSE;
> UNICODE_STRING strVolumeName;
>
> OBJECT_ATTRIBUTES objAttrsVolume;
>
> PWCHAR pBufVolume;
> WCHAR wVolumePath[MAX_PATH];
>
> IO_STATUS_BLOCK ioStatusVolume;
> NTSTATUS ntStatVolume;
>
> pBufVolume = RtlAllocateHeap(g_hHeap, 0, MAX_PATH);
>
> swprintf(wVolumePath, L"\??\E:");
>
> RtlInitUnicodeString(&strVolumeName, wVolumePath);
>
> objAttrsVolume.Length = sizeof(OBJECT_ATTRIBUTES);
> objAttrsVolume.RootDirectory = NULL;
> objAttrsVolume.ObjectName = &strVolumeName;
> objAttrsVolume.Attributes = OBJ_CASE_INSENSITIVE;
> objAttrsVolume.SecurityDescriptor = NULL;
> objAttrsVolume.SecurityQualityOfService = NULL;
> ntStatVolume = NtCreateFile(&g_hVolume,
> GENERIC_READ,
> &objAttrsVolume,
> &ioStatusVolume,
> 0,
> FILE_ATTRIBUTE_NORMAL,
> FILE_SHARE_READ,
> FILE_OPEN,
> FILE_NON_DIRECTORY_FILE,
> NULL,
> 0);
>
> if(NT_SUCCESS(ntStatVolume))
> {
> ntStatus = NtDeviceIoControlFile( g_hVolume,
> NULL,
> NULL,
> NULL,
> NULL,
> IOCTL_STORAGE_EJECT_MEDIA,
> NULL,
> 0,
> NULL,
> 0);
> if(NT_SUCCESS(ntStatus))
> {
> //Display open success
> }
> }
>
> Though I get the “Open Success” message, the drive is not opening. Could
> anyone help me with this.
>
> Thanks
> Ramananda
>
>

Provide the IO status block to NtDeviceIoControlFile


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

“Ramananda” wrote in message news:xxxxx@ntdev…
> I am getting Access Violation (0xC0000005) when I call
> NtDeviceIoControlFile. Please let me know if I am doing something wrong.
>
> Thanks
> Ramananda
>
> “Ramananda” wrote in message
> news:xxxxx@ntdev…
> > Hi
> >
> > I am writing a native mode application that can eject the CD ROM drive.
> > Below is my code
> >
> > BOOL bRet = FALSE;
> > UNICODE_STRING strVolumeName;
> >
> > OBJECT_ATTRIBUTES objAttrsVolume;
> >
> > PWCHAR pBufVolume;
> > WCHAR wVolumePath[MAX_PATH];
> >
> > IO_STATUS_BLOCK ioStatusVolume;
> > NTSTATUS ntStatVolume;
> >
> > pBufVolume = RtlAllocateHeap(g_hHeap, 0, MAX_PATH);
> >
> > swprintf(wVolumePath, L"\??\E:");
> >
> > RtlInitUnicodeString(&strVolumeName, wVolumePath);
> >
> > objAttrsVolume.Length = sizeof(OBJECT_ATTRIBUTES);
> > objAttrsVolume.RootDirectory = NULL;
> > objAttrsVolume.ObjectName = &strVolumeName;
> > objAttrsVolume.Attributes = OBJ_CASE_INSENSITIVE;
> > objAttrsVolume.SecurityDescriptor = NULL;
> > objAttrsVolume.SecurityQualityOfService = NULL;
> > ntStatVolume = NtCreateFile(&g_hVolume,
> > GENERIC_READ,
> > &objAttrsVolume,
> > &ioStatusVolume,
> > 0,
> > FILE_ATTRIBUTE_NORMAL,
> > FILE_SHARE_READ,
> > FILE_OPEN,
> > FILE_NON_DIRECTORY_FILE,
> > NULL,
> > 0);
> >
> > if(NT_SUCCESS(ntStatVolume))
> > {
> > ntStatus = NtDeviceIoControlFile( g_hVolume,
> > NULL,
> > NULL,
> > NULL,
> > NULL,
> > IOCTL_STORAGE_EJECT_MEDIA,
> > NULL,
> > 0,
> > NULL,
> > 0);
> > if(NT_SUCCESS(ntStatus))
> > {
> > //Display open success
> > }
> > }
> >
> > Though I get the “Open Success” message, the drive is not opening. Could
> > anyone help me with this.
> >
> > Thanks
> > Ramananda
> >
> >
>
>
>