Jump to content











Photo
- - - - -

Unable to create device from C++

c++

Best Answer Olof Lagerkvist , 17 February 2015 - 08:47 PM

Hi,
 
Among the most obvious things I see is that you pass in a hard-coded device number 0 instead of the IMDISK_AUTO_DEVICE_NUMBER constant (not necessarily wrong though in case you know what you are doing) and that you try to create a ram disk without any specified size and without any image file to create it from. You either need to specify a disk size without an image file name to create an uninitialized ram disk with a specific size, or specify an image file name in combination with the IMDISK_TYPE_VM flag to create a ram disk automatically initialized with the size and contents of that image file.
 
I would recommend that you read the parameter descriptions in imdisk.h.
 

	/**
	This function creates a new ImDisk virtual disk device.

	hWndStatusText  A handle to a window that can display status message text.
	The function will send WM_SETTEXT messages to this window.
	If this parameter is NULL no WM_SETTEXT messages are sent
	and the function acts non-interactive.

	DeviceNumber    In: Device number for device to create. Device number must
	not be in use by an existing virtual disk. For automatic
	allocation of device number, use IMDISK_AUTO_DEVICE_NUMBER
	constant or specify a NULL pointer.

	Out: If DeviceNumber parameter is not NULL, device number
	for created device is returned in DWORD variable pointed to.

	DiskGeometry    The virtual geometry of the new virtual disk. Note that the
	Cylinders member does not specify the number of Cylinders
	but the total size in bytes of the new virtual disk. The
	actual number of cylinders are then automatically
	calculated and rounded down if necessary.

	The Cylinders member can be zero if the device is backed by
	an image file or a proxy device, but not if it is virtual
	memory only device.

	All or some of the other members of this structure can be
	zero in which case they are automatically filled in with
	most reasonable values by the driver.

	Flags           Bitwise or-ed combination of one of the IMDISK_TYPE_xxx
	flags, one of the IMDISK_DEVICE_TYPE_xxx flags and any
	number of IMDISK_OPTION_xxx flags. The flags can often be
	left zero and left to the driver to automatically select.
	For example, if a virtual disk size is specified to 1440 KB
	and an image file name is not specified, the driver
	automatically selects IMDISK_TYPE_VM|IMDISK_DEVICE_TYPE_FD
	for this parameter.

	FileName        Name of disk image file. In case IMDISK_TYPE_VM is
	specified in the Flags parameter, this file will be loaded
	into the virtual memory-backed disk when created.

	NativePath      Set to TRUE if the FileName parameter specifies an NT
	native path, such as \??\C:\imagefile.img or FALSE if it
	specifies a Win32/DOS-style path such as C:\imagefile.img.

	MountPoint      Drive letter to assign to the new virtual device. It can be
	specified on the form F: or F:\.
	*/
Hope this helps! Go to the full post


  • Please log in to reply
2 replies to this topic

#1 jazzycamel

jazzycamel
  • Members
  • 2 posts
  •  
    United Kingdom

Posted 17 February 2015 - 08:26 PM

Hi there,

 

I am currently trying to build a DLL using ImDisk so as to create a RAM disk for my python application. I've got everything installed and working and can create a disk from Control Panel etc., however I am unable to do so programatically via C++. The following is a potted example of what I am trying to do:

VFS_API int create(void)
{
	if ( ! ImDiskStartService(L"ImDisk"))
        wcout << "Failed to start service!" << dec << GetLastError() << endl;


	DISK_GEOMETRY dg = {0};
	DWORD device = 0;
	LARGE_INTEGER imgoff = {0};
	WCHAR letter [ 4 ] = { ImDiskFindFreeDriveLetter(), L':', L'\\', L'\0' };

	if ( ! ImDiskCreateDeviceEx ( NULL, & device, & dg, 0, 0, NULL, NULL, letter ) )
	{
		wcout << "GLE:" << dec << GetLastError() << endl;
		wcout << "Failed to mount: " << letter << endl;
		
		return -1;
	}
	wcout << "Mounted: " << letter << endl;
	return 0;
}

When I call this function, the service starts as expected, I get a suitable free drive letter ("E:\" in this case) but I cannot, no matter what arguments I pass, get the ImDiskCreateDriveEx(...) call to succeed. All I get from GetLastError() is '87' which translates to invalid parameters, but I can't work out which ones or why.

 

I'm compiling this using Visual C++ 2008 on Windows 7 Enterprise 32-bit.

 

I would really appreciate any advice or examples that can point me in the right direction.

 

Thanks.



#2 Olof Lagerkvist

Olof Lagerkvist

    Gold Member

  • Developer
  • 1448 posts
  • Location:Borås, Sweden
  •  
    Sweden

Posted 17 February 2015 - 08:47 PM   Best Answer

Hi,
 
Among the most obvious things I see is that you pass in a hard-coded device number 0 instead of the IMDISK_AUTO_DEVICE_NUMBER constant (not necessarily wrong though in case you know what you are doing) and that you try to create a ram disk without any specified size and without any image file to create it from. You either need to specify a disk size without an image file name to create an uninitialized ram disk with a specific size, or specify an image file name in combination with the IMDISK_TYPE_VM flag to create a ram disk automatically initialized with the size and contents of that image file.
 
I would recommend that you read the parameter descriptions in imdisk.h.
 
	/**
	This function creates a new ImDisk virtual disk device.

	hWndStatusText  A handle to a window that can display status message text.
	The function will send WM_SETTEXT messages to this window.
	If this parameter is NULL no WM_SETTEXT messages are sent
	and the function acts non-interactive.

	DeviceNumber    In: Device number for device to create. Device number must
	not be in use by an existing virtual disk. For automatic
	allocation of device number, use IMDISK_AUTO_DEVICE_NUMBER
	constant or specify a NULL pointer.

	Out: If DeviceNumber parameter is not NULL, device number
	for created device is returned in DWORD variable pointed to.

	DiskGeometry    The virtual geometry of the new virtual disk. Note that the
	Cylinders member does not specify the number of Cylinders
	but the total size in bytes of the new virtual disk. The
	actual number of cylinders are then automatically
	calculated and rounded down if necessary.

	The Cylinders member can be zero if the device is backed by
	an image file or a proxy device, but not if it is virtual
	memory only device.

	All or some of the other members of this structure can be
	zero in which case they are automatically filled in with
	most reasonable values by the driver.

	Flags           Bitwise or-ed combination of one of the IMDISK_TYPE_xxx
	flags, one of the IMDISK_DEVICE_TYPE_xxx flags and any
	number of IMDISK_OPTION_xxx flags. The flags can often be
	left zero and left to the driver to automatically select.
	For example, if a virtual disk size is specified to 1440 KB
	and an image file name is not specified, the driver
	automatically selects IMDISK_TYPE_VM|IMDISK_DEVICE_TYPE_FD
	for this parameter.

	FileName        Name of disk image file. In case IMDISK_TYPE_VM is
	specified in the Flags parameter, this file will be loaded
	into the virtual memory-backed disk when created.

	NativePath      Set to TRUE if the FileName parameter specifies an NT
	native path, such as \??\C:\imagefile.img or FALSE if it
	specifies a Win32/DOS-style path such as C:\imagefile.img.

	MountPoint      Drive letter to assign to the new virtual device. It can be
	specified on the form F: or F:\.
	*/
Hope this helps!

#3 jazzycamel

jazzycamel
  • Members
  • 2 posts
  •  
    United Kingdom

Posted 18 February 2015 - 09:25 AM

Classic case of RTFM :blush:

 

Thanks very much for your help and very quick response, problem solved.

VFS_API int create(void)
{
	if (!ImDiskStartService(L"ImDisk"))
        wcout << "Failed to start service!" << dec << GetLastError() << endl;

	LARGE_INTEGER size={1024*1000*10};
	DISK_GEOMETRY dg={size, FixedMedia, 0, 0, 0};
	DWORD device = IMDISK_AUTO_DEVICE_NUMBER;
	LARGE_INTEGER imgoff={0};
	WCHAR letter[3]={ImDiskFindFreeDriveLetter(), L':', L'\0' };

	if (!ImDiskCreateDeviceEx(NULL, &device, &dg, 0, IMDISK_TYPE_VM, NULL, NULL, letter))
	{
		wcout << "GLE:" << dec << GetLastError() << endl;
		wcout << "Failed to mount: " << letter << endl;
		
		return -1;
	}
	wcout << "Mounted: " << letter << endl;
	return 0;
}

  • Olof Lagerkvist likes this





Also tagged with one or more of these keywords: c++

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users