0

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.

Kerwin Bryant
  • 137
  • 1
  • 3
  • 11

3 Answers3

2

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.

Caesar
  • 6,733
  • 4
  • 38
  • 44
1

Use 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

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

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
1

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

Kerwin Bryant
  • 137
  • 1
  • 3
  • 11