Hi Again,
I am now using CreateFile and DeviceIoControl on my C++ Application.
I can successfully get a handle to "\\\\?\\ImDiskCtl" and I can successfully invoke DeviceIoControl with IOCTL_IMDISK_QUERY_VERSION.
However when I try the same with IOCTL_IMDISK_CREATE_DEVICE it fails, with GetLastError() == 5 (which is Access Denied)
Any Ideas what could stop my app from creating a disk ?
Thanks
How I get access to the driver :
hDevice = CreateFileW(L"\\\\?\\ImDiskCtl", // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
NULL, // file attributes
NULL);
The following works :
DWORD VersionCheck;
DWORD BytesReturned;
BOOL bCheckVersion = DeviceIoControl(hDevice,
IOCTL_IMDISK_QUERY_VERSION,
NULL, 0,
&VersionCheck, sizeof VersionCheck,
&BytesReturned, NULL);
The following fails with last error 5 (even when run as adminstrator)
wchar_t wcFileName[512] = L"C:\\TestDisk.dsk";
std::size_t szStructSize = sizeof(IMDISK_CREATE_DATA) + (wcslen(wcFileName) * 2) + 2;
IMDISK_CREATE_DATA* DiskSetup = reinterpret_cast<IMDISK_CREATE_DATA*>( malloc( szStructSize ) );
memset( DiskSetup, 0, szStructSize );
if( DiskSetup )
{
DiskSetup->DeviceNumber = IMDISK_AUTO_DEVICE_NUMBER;
DiskSetup->DiskGeometry.Cylinders.QuadPart = 1024 * 1024 * 2;
DiskSetup->Flags = IMDISK_TYPE_FILE | IMDISK_DEVICE_TYPE_HD;
DiskSetup->DriveLetter = L'N';
DiskSetup->FileNameLength = wcslen( wcFileName );
wcscpy( DiskSetup->FileName, wcFileName );
DWORD dwBytesReturned = 0;
BOOL bSuccess = DeviceIoControl(hDevice,
IOCTL_IMDISK_CREATE_DEVICE,
DiskSetup, szStructSize,
DiskSetup, szStructSize,
&dwBytesReturned, nullptr);
if( bSuccess )
{
std::cout << "SUCCESS" << std::endl;
}
else
{
std::cout << "FAILURE" << std::endl;
}
iLastError = GetLastError();
std::cout << "Last Error : " << iLastError << std::endl;
while(true)
{
Sleep( 50 );
}
}
Edited by Muggen, 26 October 2017 - 10:52 AM.