public static async Task <Dictionary <char, string> > getMounted() { return(await Task.Run <Dictionary <char, string> >(() => { var ret = new Dictionary <char, string>(); uint size = (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)); IntPtr buffer = Marshal.AllocHGlobal((int)size); uint bytesReturned; IntPtr _hdev = CreateFile("\\\\.\\VeraCrypt", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); bool bResult = DeviceIoControl(_hdev, TC_IOCTL_GET_MOUNTED_VOLUMES, buffer, size, buffer, size, out bytesReturned, IntPtr.Zero); // IMPORTANT! Otherwise, the struct fills up with random bytes from memory, if no VeraCrypt is available if (!bResult) { return ret; } MOUNT_LIST_STRUCT mount = new MOUNT_LIST_STRUCT(); Marshal.PtrToStructure(buffer, mount); Marshal.FreeHGlobal(buffer); for (int i = 0; i < 26; i++) { if (mount.wszVolume[i].ToString().Length > 0) { ret.Add((char)('A' + i), mount.wszVolume[i].ToString()); } } return ret; })); }
private String[] GetMountList() { uint size = (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)); IntPtr buffer = Marshal.AllocHGlobal((int)size); uint bytesReturned; IntPtr _hdev = CreateFile("\\\\.\\TrueCrypt", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); bool bResult = DeviceIoControl(_hdev, TC_IOCTL_GET_MOUNTED_VOLUMES, buffer, size, buffer, size, out bytesReturned, IntPtr.Zero); MOUNT_LIST_STRUCT mount = new MOUNT_LIST_STRUCT(); Marshal.PtrToStructure(buffer, mount); Marshal.FreeHGlobal(buffer); return(mount.wszVolume.Select(m => m.ToString()).ToArray()); }
static void Main(string[] args) { uint size = (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)); IntPtr buffer = Marshal.AllocHGlobal((int)size); uint bytesReturned; IntPtr _hdev = CreateFile("\\\\.\\TrueCrypt", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); bool bResult = DeviceIoControl(_hdev, TC_IOCTL_GET_MOUNTED_VOLUMES, buffer, size, buffer, size, out bytesReturned, IntPtr.Zero); MOUNT_LIST_STRUCT mount = new MOUNT_LIST_STRUCT(); Marshal.PtrToStructure(buffer, mount); Marshal.FreeHGlobal(buffer); for (int i = 0; i < 26; i++) { Console.WriteLine("{0}: => {1}", (char)('A' + i), mount.wszVolume[i]); } }
static void Main(string[] args) { uint size = (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)); IntPtr buffer = Marshal.AllocHGlobal((int)size); uint bytesReturned; IntPtr _hdev = CreateFile("\\\\.\\TrueCrypt", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); bool bResult = DeviceIoControl(_hdev, TC_IOCTL_GET_MOUNTED_VOLUMES, buffer, size, buffer, size, out bytesReturned, IntPtr.Zero); MOUNT_LIST_STRUCT mount = new MOUNT_LIST_STRUCT(); Marshal.PtrToStructure(buffer, mount); Marshal.FreeHGlobal(buffer); // Print output as a json array string outp = "["; for (int i = 0; i < 26; i++) { if (mount.wszVolume [i].ToString().Length != 0) { outp += "{ \"drive\" : \"" + (char)('A' + i) + "\", \"volume\" : \"" + mount.wszVolume[i] + "\" },"; } } Console.Write(outp.TrimEnd(',') + "]"); }
/// <summary> /// Dismounts all volumes mounted by the TestCrypt driver. /// </summary> public static void DismountAll() { IntPtr mountListPtr = IntPtr.Zero; IntPtr umountPtr = IntPtr.Zero; try { // open/load the TestCrypt driver first SafeFileHandle hDriver = OpenDriver(); // retrieve a list of all volumes currently mounted by the TestCrypt driver uint result = 0; mountListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT))); if (DeviceApi.DeviceIoControl(hDriver, TC_IOCTL_GET_MOUNTED_VOLUMES, mountListPtr, (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)), mountListPtr, (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)), ref result, IntPtr.Zero)) { MOUNT_LIST_STRUCT mountList = (MOUNT_LIST_STRUCT)Marshal.PtrToStructure(mountListPtr, typeof(MOUNT_LIST_STRUCT)); if (mountList.ulMountedDrives != 0) { // inform the operating system about pending device removal BroadcastDeviceChange(DBT_DEVICEREMOVEPENDING, 0, mountList.ulMountedDrives); UMOUNT_STRUCT umount = new UMOUNT_STRUCT(); umount.ignoreOpenFiles = true; umountPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UMOUNT_STRUCT))); Marshal.StructureToPtr(umount, umountPtr, true); if (DeviceApi.DeviceIoControl(hDriver, TC_IOCTL_DISMOUNT_ALL_VOLUMES, umountPtr, (uint)Marshal.SizeOf(typeof(UMOUNT_STRUCT)), umountPtr, (uint)Marshal.SizeOf(typeof(UMOUNT_STRUCT)), ref result, IntPtr.Zero)) { umount = (UMOUNT_STRUCT)Marshal.PtrToStructure(umountPtr, typeof(UMOUNT_STRUCT)); if (umount.nReturnCode != 0) { throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, umount.nReturnCode, "TC_IOCTL_DISMOUNT_ALL_VOLUMES", null); } else { // inform the operating system about completed device removal BroadcastDeviceChange(DBT_DEVICEREMOVECOMPLETE, 0, mountList.ulMountedDrives); } } else { Win32Exception ex = new Win32Exception(); throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, ex.ErrorCode, "TC_IOCTL_DISMOUNT_ALL_VOLUMES", ex.Message); } } else { // there is currently no volume mounted by the TestCrypt driver } } else { Win32Exception ex = new Win32Exception(); throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, ex.ErrorCode, "TC_IOCTL_GET_MOUNTED_VOLUMES", ex.Message); } hDriver.Close(); } finally { if (mountListPtr != IntPtr.Zero) { Marshal.FreeHGlobal(mountListPtr); } if (umountPtr != IntPtr.Zero) { Marshal.FreeHGlobal(umountPtr); } } }