How to create a virtual drive on Windows in Rust? (Similar to what the subst command does on windows) I don't seem to find such an API in Rust.
3 Answers
The only seemingly relevant function I see being invoked from subst.exe
is DefineDosDevice(dwFlags, lpDeviceName, lpTargetPath)
:
Defines, redefines, or deletes MS-DOS device names.
[in] lpDeviceName
A pointer to an MS-DOS device name string specifying the device the function is defining, redefining, or deleting.
[in, optional] lpTargetPath
A pointer to a path string that will implement this device.
Use whichever Rust binding you like.

- 6,733
- 4
- 38
- 44
Use SetVolumeMountPointW
:
- Microsoft documentation: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setvolumemountpointw
SetVolumeMountPointW function (winbase.h)
Associates a volume with a drive letter or a directory on another volume.
Parameters
[in] lpszVolumeMountPoint
The user-mode path to be associated with the volume. This may be a drive letter (for example, "X:") or a directory on another volume (for example, "Y:\MountX"). The string must end with a trailing backslash ('').
[in] lpszVolumeName
A volume GUID path for the volume. This string must be of the form "\?\Volume{GUID}" where GUID is a GUID that identifies the volume. The "\?" turns off path parsing and is ignored as part of the path, as discussed in Naming a Volume.
Function winapi::um::winbase::SetVolumeMountPointW
pub unsafe extern "system" fn SetVolumeMountPointW( lpszVolumeMountPoint: LPCWSTR, lpszVolumeName: LPCWSTR ) -> BOOL
- windows-rs bindings: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Storage/FileSystem/fn.SetVolumeMountPointW.html
Function windows::Win32::Storage::FileSystem::SetVolumeMountPointW
pub unsafe fn SetVolumeMountPointW<P0, P1>( lpszvolumemountpoint: P0, lpszvolumename: P1 ) -> BOOL where P0: IntoParam<PCWSTR>, P1: IntoParam<PCWSTR>,
Required features: "Win32_Storage_FileSystem", "Win32_Foundation"
You probably also want to use QueryDosDeviceW
: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-querydosdevicew, https://docs.rs/winapi/latest/winapi/um/fileapi/fn.QueryDosDeviceW.html, https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Storage/FileSystem/fn.QueryDosDeviceW.html

- 5,724
- 3
- 24
- 45
thanks @solomon-ucko @caesar , I wrapped a tool library win_subst based on windows-rs for convenient use.

- 137
- 1
- 3
- 11