📄 dlgcode.c
字号:
else
RegCloseKey (k);
}
break;
}
}
#ifndef SETUP
if (CurrentOSMajor == 6 && CurrentOSMinor == 0 && osEx.dwBuildNumber < 6000)
{
Error ("UNSUPPORTED_BETA_OS");
exit (0);
}
#endif
}
/* Get the attributes for the standard dialog class */
if ((GetClassInfo (hInst, WINDOWS_DIALOG_CLASS, &wc)) == 0)
AbortProcess ("INIT_REGISTER");
#ifndef SETUP
wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRUECRYPT_ICON));
#else
#include "../setup/resource.h"
wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_SETUP));
#endif
wc.lpszClassName = TC_DLG_CLASS;
wc.lpfnWndProc = &CustomDlgProc;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.cbWndExtra = DLGWINDOWEXTRA;
hDlgClass = RegisterClass (&wc);
if (hDlgClass == 0)
AbortProcess ("INIT_REGISTER");
wc.lpszClassName = TC_SPLASH_CLASS;
wc.lpfnWndProc = &SplashDlgProc;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.cbWndExtra = DLGWINDOWEXTRA;
hSplashClass = RegisterClass (&wc);
if (hSplashClass == 0)
AbortProcess ("INIT_REGISTER");
// DPI and GUI aspect ratio
DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_AUXILIARY_DLG), NULL,
(DLGPROC) AuxiliaryDlgProc, (LPARAM) 1);
InitHelpFileName ();
#ifndef SETUP
if (!EncryptionThreadPoolStart())
{
handleWin32Error (NULL);
exit (1);
}
#endif
}
void InitHelpFileName (void)
{
char *lpszTmp;
GetModuleFileName (NULL, szHelpFile, sizeof (szHelpFile));
lpszTmp = strrchr (szHelpFile, '\\');
if (lpszTmp)
{
char szTemp[TC_MAX_PATH];
// Primary file name
if (strcmp (GetPreferredLangId(), "en") == 0
|| GetPreferredLangId() == NULL)
{
strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
}
else
{
sprintf (szTemp, "TrueCrypt User Guide.%s.pdf", GetPreferredLangId());
strcpy (++lpszTmp, szTemp);
}
// Secondary file name (used when localized documentation is not found).
GetModuleFileName (NULL, szHelpFile2, sizeof (szHelpFile2));
lpszTmp = strrchr (szHelpFile2, '\\');
if (lpszTmp)
{
strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
}
}
}
BOOL OpenDevice (char *lpszPath, OPEN_TEST_STRUCT *driver)
{
DWORD dwResult;
BOOL bResult;
strcpy ((char *) &driver->wszFileName[0], lpszPath);
ToUNICODE ((char *) &driver->wszFileName[0]);
driver->bDetectTCBootLoader = FALSE;
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 (or neither).
If bReliableRequired is TRUE, very fast execution is guaranteed, but the results cannot be relied upon.
If it's FALSE and the function is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -