📄 dlgcode.c
字号:
bResult = DeviceIoControl (hDriver, TC_IOCTL_OPEN_TEST,
driver, sizeof (OPEN_TEST_STRUCT),
NULL, 0,
&dwResult, NULL);
if (bResult == FALSE)
{
dwResult = GetLastError ();
if (dwResult == ERROR_SHARING_VIOLATION)
return TRUE;
else
return FALSE;
}
return TRUE;
}
// Tells the driver that it's running in traveler mode
void NotifyDriverOfTravelerMode (void)
{
if (hDriver != INVALID_HANDLE_VALUE)
{
DWORD dwResult;
DeviceIoControl (hDriver, TC_IOCTL_SET_TRAVELER_MODE_STATUS, NULL, 0, NULL, 0, &dwResult, NULL);
}
}
BOOL GetDriveLabel (int driveNo, wchar_t *label, int labelSize)
{
DWORD fileSystemFlags;
wchar_t root[] = { L'A' + driveNo, L':', L'\\', 0 };
return GetVolumeInformationW (root, label, labelSize / 2, NULL, NULL, &fileSystemFlags, NULL, 0);
}
// This function also populates SysPartitionDevicePath and SysDriveDevicePath for later use.
int GetAvailableFixedDisks (HWND hComboBox, char *lpszRootPath)
{
int i, n;
int line = 0;
LVITEM LvItem;
__int64 deviceSize = 0;
for (i = 0; i < 64; i++)
{
BOOL drivePresent = FALSE;
BOOL removable = FALSE;
LvItem.lParam = 0;
for (n = 0; n <= 32; n++)
{
char szTmp[TC_MAX_PATH];
wchar_t size[100] = {0};
OPEN_TEST_STRUCT driver;
sprintf (szTmp, lpszRootPath, i, n);
if (OpenDevice (szTmp, &driver))
{
BOOL bResult;
PARTITION_INFORMATION diskInfo;
DISK_GEOMETRY driveInfo;
drivePresent = TRUE;
bResult = GetPartitionInfo (szTmp, &diskInfo);
// Test if device is removable
if (n == 0 && GetDriveGeometry (szTmp, &driveInfo))
removable = driveInfo.MediaType == RemovableMedia;
if (bResult)
{
// System creates a virtual partition1 for some storage devices without
// partition table. We try to detect this case by comparing sizes of
// partition0 and partition1. If they match, no partition of the device
// is displayed to the user to avoid confusion. Drive letter assigned by
// system to partition1 is displayed as subitem of partition0
if (n == 1 && diskInfo.PartitionLength.QuadPart == deviceSize)
{
char drive[] = { 0, ':', 0 };
char device[MAX_PATH * 2];
int driveNo;
// Drive letter
strcpy (device, szTmp);
ToUNICODE (device);
driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
LvItem.iSubItem = 1;
LvItem.pszText = drive;
LvItem.mask = LVIF_TEXT;
SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
// Label
if (driveNo != -1)
{
wchar_t name[64];
if (GetDriveLabel (driveNo, name, sizeof (name)))
ListSubItemSetW (hComboBox, LvItem.iItem, 3, name);
}
// Mark the device as containing a virtual partition
LvItem.iSubItem = 0;
LvItem.mask = LVIF_PARAM;
LvItem.lParam |= SELDEVFLAG_VIRTUAL_PARTITION;
SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &LvItem);
break;
}
GetSizeString (diskInfo.PartitionLength.QuadPart, size);
}
memset (&LvItem,0,sizeof(LvItem));
LvItem.mask = LVIF_TEXT;
LvItem.iItem = line++;
// Device Name
if (n == 0)
{
wchar_t s[1024];
deviceSize = diskInfo.PartitionLength.QuadPart;
if (removable)
wsprintfW (s, L"Harddisk %d (%s):", i, GetString ("REMOVABLE"));
else
wsprintfW (s, L"Harddisk %d:", i);
ListItemAddW (hComboBox, LvItem.iItem, s);
}
else
{
LvItem.pszText = szTmp;
SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
}
// Size
ListSubItemSetW (hComboBox, LvItem.iItem, 2, size);
// Device type removable
if (removable)
{
// Mark as removable
LvItem.iSubItem = 0;
LvItem.mask = LVIF_PARAM;
LvItem.lParam |= SELDEVFLAG_REMOVABLE_HOST_DEVICE;
SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &LvItem);
LvItem.mask = LVIF_TEXT;
}
if (n > 0)
{
char drive[] = { 0, ':', 0 };
char device[MAX_PATH * 2];
int driveNo;
// Drive letter
strcpy (device, szTmp);
ToUNICODE (device);
driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
if (driveNo != -1
&& GetSystemDriveLetter() == 'A' + driveNo)
{
// Mark the partition as the system partition
LvItem.iSubItem = 0;
LvItem.mask = LVIF_PARAM;
LvItem.lParam |= SELDEVFLAG_SYSTEM_PARTITION;
SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &LvItem);
LvItem.mask = LVIF_TEXT;
// Store the device path of the system partition for later use (to save time significantly)
sprintf (SysPartitionDevicePath, lpszRootPath, i, n);
// Find the line of the drive containing this partition
{
char tmpDevicePath [TC_MAX_PATH];
char *ptrTmpDevicePath = tmpDevicePath;
LVITEM tmpLvItem;
memset (&tmpLvItem, 0, sizeof(tmpLvItem));
tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;
tmpLvItem.pszText = ptrTmpDevicePath;
tmpLvItem.cchTextMax = TC_MAX_PATH;
for (tmpLvItem.iItem = LvItem.iItem - 1;
tmpLvItem.iItem >= 0
&& tmpLvItem.iItem >= LvItem.iItem - n;
tmpLvItem.iItem--)
{
SendMessage (hComboBox, LVM_GETITEM, tmpLvItem.iItem, (LPARAM) &tmpLvItem);
if (ptrTmpDevicePath[0] == 'H')
{
if (sscanf (ptrTmpDevicePath, "Harddisk %d", &i) == 1)
{
// Mark the drive as a system drive
tmpLvItem.iSubItem = 0;
tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;
tmpLvItem.lParam |= SELDEVFLAG_SYSTEM_DRIVE;
SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &tmpLvItem);
// Store the device path of the system drive for later use (to save time significantly)
sprintf (SysDriveDevicePath, lpszRootPath, i, 0);
if (bRawDevicesDlgProcInstantExit
&& strlen (SysPartitionDevicePath) > 1)
{
bCachedSysDevicePathsValid = TRUE;
return 1;
}
break;
}
}
}
}
}
LvItem.iSubItem = 1;
LvItem.pszText = drive;
LvItem.mask = LVIF_TEXT;
SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
// Label
if (driveNo != -1)
{
wchar_t name[64];
if (GetDriveLabel (driveNo, name, sizeof (name)))
ListSubItemSetW (hComboBox, LvItem.iItem, 3, name);
}
}
if (n == 1)
{
// Mark the device as containing a partition
{
// Retrieve the whole-device item data so that we preserve its existing flags in LvItem.lParam
char tmpDevicePath [TC_MAX_PATH];
char *ptrTmpDevicePath = tmpDevicePath;
LVITEM tmpLvItem;
memset (&tmpLvItem, 0, sizeof(tmpLvItem));
tmpLvItem.iSubItem = 0;
tmpLvItem.iItem = line - 2;
tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;
tmpLvItem.pszText = ptrTmpDevicePath;
tmpLvItem.cchTextMax = sizeof(tmpDevicePath);
SendMessage (hComboBox, LVM_GETITEM, tmpLvItem.iItem, (LPARAM) &tmpLvItem);
if (ptrTmpDevicePath[0] == 'H')
{
if (sscanf (ptrTmpDevicePath, "Harddisk %d", &i) == 1)
{
tmpLvItem.iSubItem = 0;
tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;
// Mark the device as containing partition preserving existing flags
tmpLvItem.lParam |= SELDEVFLAG_CONTAINS_PARTITIONS;
SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &tmpLvItem);
}
}
}
}
}
else if (n == 0)
break;
}
if (drivePresent)
{
memset (&LvItem,0,sizeof(LvItem));
LvItem.mask = LVIF_TEXT;
LvItem.iItem = line++;
LvItem.pszText = "";
SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
}
}
i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
if (i != CB_ERR)
return i;
else
return 0;
}
int GetAvailableRemovables (HWND hComboBox, char *lpszRootPath)
{
char szTmp[TC_MAX_PATH];
int i;
LVITEM LvItem;
memset (&LvItem,0,sizeof(LvItem));
LvItem.mask = LVIF_TEXT;
LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;
if (QueryDosDevice ("A:", szTmp, sizeof (szTmp)) != 0 && GetDriveType ("A:\\") == DRIVE_REMOVABLE)
{
LvItem.pszText = "\\Device\\Floppy0";
LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
LvItem.iSubItem = 1;
LvItem.pszText = "A:";
SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
}
if (QueryDosDevice ("B:", szTmp, sizeof (szTmp)) != 0 && GetDriveType ("B:\\") == DRIVE_REMOVABLE)
{
LvItem.pszText = "\\Device\\Floppy1";
LvItem.iSubItem = 0;
LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;
LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
LvItem.iSubItem = 1;
LvItem.pszText = "B:";
SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
}
i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
if (i != CB_ERR)
return i;
else
return 0;
}
/* Stores the device path of the system partition in SysPartitionDevicePath and the device path of the system drive
in SysDriveDevicePath.
IMPORTANT: As this may take a very long time if called for the first time, it should be called only before performing
a dangerous operation (such as header backup restore or formatting a supposedly non-system device) never
at WM_INITDIALOG or any other GUI events -- instead call IsSystemDevicePath (path, hwndDlg, FALSE) for
very fast preliminary GUI checks; also note that right after the "Select Device" dialog exits with an OK
return code, you can use the global flags bSysPartitionSelected and bSysDriveSelected to see if the user
selected the system partition/device.
After this function completes successfully, the results are cached for the rest of the session and repeated
executions complete very fast. Returns TRUE if successful (otherwise FALSE). */
BOOL GetSysDevicePaths (HWND hwndDlg)
{
if (!bCachedSysDevicePathsValid
|| strlen (SysPartitionDevicePath) <= 1
|| strlen (SysDriveDevicePath) <= 1)
{
int nResult;
char tmp [TC_MAX_PATH];
bRawDevicesDlgProcInstantExit = TRUE;
nResult = DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_RAWDEVICES_DLG), hwndDlg,
(DLGPROC) RawDevicesDlgProc, (LPARAM) & tmp[0]);
bRawDevicesDlgProcInstantExit = FALSE;
}
return (bCachedSysDevicePathsValid
&& strlen (SysPartitionDevicePath) > 1
&& strlen (SysDriveDevicePath) > 1);
}
/* Determines whether the device path is the path of the system partition or of the system drive.
If bReliableRequired is TRUE, very fast execution is guaranteed, but the results cannot be relied upon.
If it's FALSE and the function is called for the first time, execution may take up to one minute but the
results are reliable.
IMPORTANT: As the execution may take a very long time if called for the first time with bReliableRequired set
to TRUE, it should be called with bReliableRequired set to TRUE only before performing a dangerous
operation (such as header backup restore or formatting a supposedly non-system device) never at
WM_INITDIALOG or any other GUI events (use IsSystemDevicePath(path, hwndDlg, FALSE) for fast
preliminary GUI checks; also note that right after the "Select Device" dialog exits with an OK
return code, you can use the global flags bSysPartitionSelected and bSysDriveSelected to see if the
user selected the system partition/device).
After this function completes successfully, the results are cached for the rest of the session, bReliableRequired
is ignored (TRUE implied), repeated executions complete very fast, and the results are always reliable.
Return codes:
1 - it is the system partition path (e.g. \Device\Harddisk0\Partition1)
2 - it is the system drive path (e.g. \Device\Harddisk0\Partition0)
0 - it's not the system partition/drive path
-1 - the result can't be determined, isn't reliable, or there was an error. */
int IsSystemDevicePath (char *path, HWND hwndDlg, BOOL bReliableRequired)
{
if (!bCachedSysDevicePathsValid
&& bReliableRequired)
{
if (!GetSysDevicePaths (hwndDlg))
return -1;
}
if (strlen (SysPartitionDevicePath) <= 1 || strlen (SysDriveDevicePath) <= 1)
return -1;
if (strncmp (path, SysPartitionDevicePath, max (strlen(path), strlen(SysPartitionDevicePath))) == 0)
return 1;
else if (strncmp (path, SysDriveDevicePath, max (strlen(path), strlen(SysDriveDevicePath))) == 0)
return 2;
return 0;
}
BOOL TextInfoDialogBox (int nID)
{
return DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_TEXT_INFO_DIALOG_BOX_DLG), MainDlg, (DLGPROC) TextInfoDialogBoxDlgProc, (LPARAM) nID);
}
BOOL CALLBACK TextInfoDialogBoxDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
WORD lw = LOWORD (wParam);
static int nID = 0;
switch (msg)
{
case WM_INITDIALOG:
{
nID = (int) lParam;
ShowWindow(GetDlgItem(hwndDlg, IDC_PRINT), SW_HIDE);
switch (nID)
{
case TC_TBXID_LEGAL_NOTICES:
LocalizeDialog (hwndDlg, "LEGAL_NOTICES_DLG_TITLE");
break;
case TC_TBXID_SYS_ENCRYPTION_PRETEST:
LocalizeDialog (hwndDlg, NULL);
ShowWindow(GetDlgItem(hwndDlg, IDC_PRINT), SW_SHOW);
break;
case TC_TBXID_SYS_ENC_RESCUE_DISK:
LocalizeDialog (hwndDlg, NULL);
ShowWindow(GetDlgItem(hwndDlg, IDC_PRINT), SW_SHOW);
break;
}
SendMessage (hwndDlg, TC_APPMSG_LOAD_TEXT_BOX_CONTENT, 0, 0);
}
return 0;
case WM_COMMAND:
if (lw == IDOK || lw == IDCANCEL)
{
NormalCursor ();
EndDialog (hwndDlg, 0);
return 1;
}
if (lw == IDC_PRINT)
{
switch (nID)
{
case TC_TBXID_SYS_ENCRYPTION_PRETEST:
PrintHardCopyTextUTF16 (GetString ("SYS_ENCRYPTION_PRETEST_INFO2"), "Pre-Boot Troubleshooting", wcslen (GetString ("SYS_ENCRYPTION_PRETEST_INFO2")) * 2);
break;
case TC_TBXID_SYS_ENC_RESCUE_DISK:
PrintHardCopyTextUTF16 (GetString ("RESCUE_DISK_HELP"), "TrueCrypt Rescue Disk Help", wcslen (GetString ("RESCUE_DISK_HELP")) * 2);
break
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -