📄 cinstallhelper.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: CInstallHelper.cpp,v 1.6 2002/08/06 20:09:22 dallen Exp $
____________________________________________________________________________*/
#include "pgpClassesConfig.h"
#include "CString.h"
#include "CSharedMemory.h"
#include "CPath.h"
#include "CRegistryKey.h"
#include "CSharedMemory.h"
#include "UWinVersion.h"
#include "pgpClientErrors.h"
#include "DriverAPI.h"
#include "CInstallHelper.h"
_USING_PGP
_UNNAMED_BEGIN
// Constants
// Constants for old PGPdisk installs
enum
{
kOldSendPacketIoctl = CTL_CODE(FILE_DEVICE_DISK, 0x800,
METHOD_BUFFERED, FILE_ANY_ACCESS) | 0x40000000,
kPGPdiskADPacketMagic = 0x820F7353,
kAD_Mount = 0x0001,
kAD_Unmount = 0x0002,
kAD_QueryVersion = 0x0003,
kAD_QueryMounted = 0x0004,
kAD_QueryOpenFiles = 0x0005,
kAD_ChangePrefs = 0x0006,
kAD_LockUnlockMem = 0x0007,
kAD_GetPGPdiskInfo = 0x0008,
kAD_LockUnlockVol = 0x0009,
kAD_ReadWriteVol = 0x000A,
kAD_QueryVolInfo = 0x000B,
kAD_NotifyUserLogoff = 0x000C
};
const char *kPGPdiskResSharedMemName = "PGPdiskResidentSharedInfo";
// Data for spurious keys and values that need cleaning up.
const HKEY kRegistrySpurious1Root = HKEY_LOCAL_MACHINE;
const char *kRegistrySpurious1Section = "Enum\\ESDI";
const char *kRegistrySpurious1SubkeyMatch = "PGPDISK";
const HKEY kRegistrySpurious2Root = HKEY_LOCAL_MACHINE;
const char *kRegistrySpurious2Section =
"System\\CurrentControlSet\\Control\\ASD\\Prob";
const char *kRegistrySpurious2ValueMatch = "PGPDISK";
const HKEY kRegistrySpurious3Root = HKEY_DYN_DATA;
const char *kRegistrySpurious3Section = "Config Manager\\Enum";
const char *kRegistrySpurious3Value = "HardWareKey";
const char *kRegistrySpurious3StringMatch = "PGPDISK";
const HKEY kRegistryWin98LogConfigRoot = HKEY_LOCAL_MACHINE;
const char *kRegistryWin98LogConfigSection =
"Enum\\Root\\PGPdisk\\0000\\LogConfig";
// Types
//
// Types for old PGPdisk installs
// Align to 1.
#if PGP_WIN32
#pragma pack(push, 1)
#endif
class DualErr
{
public:
DualErr() : mZeroForSuccess(0) { }
~DualErr() { }
PGPBoolean IsError() const {return mZeroForSuccess != 0;}
PGPBoolean IsntError() const {return mZeroForSuccess == 0;}
private:
PGPUInt32 mZeroForSuccess;
PGPUInt32 mIgnored[2];
};
// Standard packet header.
typedef struct ADPacketHeader
{
PGPUInt32 magic; // must be kPGPdiskADPacketMagic
PGPUInt16 code; // packet code
DualErr *pDerr; // error (DualErr *)
} ADPacketHeader, *PADPacketHeader;
// Struct AD_Unmount.
typedef struct AD_Unmount
{
ADPacketHeader header; // standard header
PGPByte drive; // drive to unmount
PGPBoolean isThisEmergency; // emergency unmount?
} AD_Unmount, *PAD_Unmount;
// Struct AD_QueryMounted.
typedef struct AD_QueryMounted
{
ADPacketHeader header; // standard header
PGPBoolean trueIfUsePath; // TRUE to use path, FALSE to use drive
const char *path; // file to enquire about
PGPUInt32 sizePath; // size of path
PGPByte drive; // drive to enquire about
PGPBoolean *pIsPGPdisk; // is a mounted PGPdisk? (PGPBoolean *)
} AD_QueryMounted, *PAD_QueryMounted;
// Restore alignment.
#if PGP_WIN32
#pragma pack(pop)
#endif
_UNNAMED_END
// Class CInstallHelper member functions
CInstallHelper::CInstallHelper() : mDriverHandle(INVALID_HANDLE_VALUE)
{
OpenDriver();
}
CInstallHelper::~CInstallHelper()
{
CloseDriver();
}
void
CInstallHelper::KillPGPdiskResident()
{
CSharedMemory resShared;
resShared.Attach(kPGPdiskResSharedMemName);
HWND resWindow = *reinterpret_cast<HWND *>(resShared.Get());
PostMessage(resWindow, WM_QUIT, NULL, NULL);
}
void
CInstallHelper::PGPdiskCleanup()
{
if (!UWinVersion::IsWin95Compatible())
return;
// Wipe the "PGPDISK" subtree under "HKLM\Enum\ESDI".
try
{
CRegistryKey regKey;
regKey.Open(kRegistrySpurious1Root, kRegistrySpurious1Section);
PGPUInt32 i = 0;
while (TRUE)
{
CString keyName;
if (!regKey.EnumSubkeys(i++, keyName))
break;
if (keyName.Find(kRegistrySpurious1SubkeyMatch) != -1)
{
regKey.DeleteSubkey(keyName, TRUE);
break;
}
}
}
catch (CComboError&) { }
// Wipe all values containing "PGPDISK" in all keys that reside under
// "HKLM\System\CurrentControlSet\control\ASD\Prob".
try
{
CRegistryKey regKey;
regKey.Open(kRegistrySpurious2Root, kRegistrySpurious2Section);
PGPUInt32 i = 0;
while (TRUE)
{
CString keyName;
if (!regKey.EnumSubkeys(i++, keyName))
break;
CString keyPath(kRegistrySpurious2Section);
keyPath += "\\";
keyPath += keyName;
// Enumerate all values under this key.
CRegistryKey regKey2;
regKey2.Open(kRegistrySpurious2Root, keyPath);
PGPUInt32 j = 0;
while (TRUE)
{
CString valueName;
PGPUInt32 valueType;
if (!regKey2.EnumValues(j++, valueName, valueType))
break;
if (valueName.Find(kRegistrySpurious2ValueMatch) != -1)
{
regKey2.DeleteValue(valueName);
j = 0;
}
}
}
}
catch (CComboError&) { }
// Wipe all keys with a "HardWareKey" value containing "PGPDISK"
// under "HDD\Config Manager\Enum".
try
{
CRegistryKey regKey;
regKey.Open(kRegistrySpurious3Root, kRegistrySpurious3Section);
PGPUInt32 i = 0;
while (TRUE)
{
CString keyName;
if (!regKey.EnumSubkeys(i++, keyName))
break;
CString keyPath(kRegistrySpurious3Section);
keyPath += "\\";
keyPath += keyName;
CRegistryKey regKey2;
regKey2.Open(kRegistrySpurious3Root, keyPath);
try
{
CString valueData;
regKey2.GetValue(kRegistrySpurious3Value,
reinterpret_cast<PGPByte *>(valueData.GetBuffer(128)),
128);
valueData.ReleaseBuffer();
if (valueData.Find(kRegistrySpurious3StringMatch) != -1)
{
regKey2.Close();
regKey.DeleteSubkey(keyName);
i = 0;
}
}
catch (CComboError&) { }
}
}
catch (CComboError&) { }
}
BOOL
CInstallHelper::AreAnyPGPdisksMounted()
{
// Check if any drive letter is a mounted PGPdisk.
for (PGPUInt8 i = 0; i < 26; i++)
{
if (IsVolumeAPGPdisk(i))
return TRUE;
}
return FALSE;
}
BOOL
CInstallHelper::IsVolumeAPGPdisk(PGPByte drive)
{
PGPBoolean isPGPdisk = FALSE;
try
{
// Try new driver.
CPath root;
root.MakePlainLocalRoot(drive);
DriverAPI::CCommandQueryMounted QM(root, FALSE);
SendNewPacket(&QM.Header(), sizeof(QM));
isPGPdisk = QM.IsMountedPGPdisk();
}
catch (CComboError&)
{
// Try old driver.
AD_QueryMounted QM;
QM.trueIfUsePath = FALSE;
QM.drive = drive;
QM.pIsPGPdisk = &isPGPdisk;
SendOldPacket(reinterpret_cast<PADPacketHeader>(&QM),
kAD_QueryMounted, sizeof(QM));
}
return isPGPdisk;
}
void
CInstallHelper::UnmountAllPGPdisks()
{
PGPUInt32 drives = GetLogicalDrives();
for (PGPUInt8 i = 0; i < 26; i++)
{
if (drives & (1 << i))
{
if (IsVolumeAPGPdisk(i))
{
try
{
// Try new driver.
CPath root;
root.MakePlainLocalRoot(i);
DriverAPI::CCommandUnmount U(root, FALSE);
SendNewPacket(&U.Header(), sizeof(U));
}
catch (CComboError&)
{
// Try old driver.
AD_Unmount UNMNT;
UNMNT.drive = i;
UNMNT.isThisEmergency = FALSE;
SendOldPacket(reinterpret_cast<PADPacketHeader>(&UNMNT),
kAD_Unmount, sizeof(UNMNT));
}
}
}
}
}
void
CInstallHelper::PGPdiskPostInstall()
{
if (UWinVersion::IsWin95Compatible())
{
CRegistryKey regKey;
regKey.Open(kRegistryWin98LogConfigRoot,
kRegistryWin98LogConfigSection);
regKey.DeleteValue(NULL);
}
}
void
CInstallHelper::SendOldPacket(
void *pPacket,
PGPUInt16 code,
PGPUInt32 packetSize) const
{
pgpAssertAddrValid(pPacket, ADPacketHeader);
DualErr derr;
unsigned long nBytesReturned;
// Initialize the packet header.
ADPacketHeader *pADPH = static_cast<ADPacketHeader *>(pPacket);
pADPH->magic = kPGPdiskADPacketMagic;
pADPH->code = code;
pADPH->pDerr = &derr;
// Send the packet.
if (!DeviceIoControl(mDriverHandle, kOldSendPacketIoctl, pPacket,
packetSize, NULL, NULL, &nBytesReturned, NULL))
{
THROW_ERRORS(kPGPClientError_DiskDriverCommFailed, GetLastError());
}
if (derr.IsError())
THROW_PGPERROR(kPGPClientError_DiskDriverCommFailed);
}
void
CInstallHelper::SendNewPacket(
DriverAPI::CCommandHeader *pPacket,
PGPUInt32 size) const
{
pgpAssertAddrValid(pPacket, DriverAPI::CCommandHeader);
pgpAssert(size >= sizeof(DriverAPI::CCommandHeader));
unsigned long nBytesReturned;
if (!DeviceIoControl(mDriverHandle, DriverAPI::kSendPacketIoctl,
pPacket, size, NULL, NULL, &nBytesReturned, NULL))
{
THROW_ERRORS(kPGPClientError_DiskDriverCommFailed, GetLastError());
}
THROW_IF_ERROR(pPacket->DriverError());
}
void
CInstallHelper::OpenDriver()
{
pgpAssert(mDriverHandle == INVALID_HANDLE_VALUE);
CString cfDriverName(DriverAPI::kPGPdiskDriverNameA);
cfDriverName.Prepend("\\\\.\\");
mDriverHandle = CreateFile(cfDriverName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (mDriverHandle == INVALID_HANDLE_VALUE)
THROW_PGPERROR(kPGPClientError_DiskDriverNotInstalled);
}
void
CInstallHelper::CloseDriver()
{
pgpAssert(mDriverHandle != INVALID_HANDLE_VALUE);
CloseHandle(mDriverHandle);
mDriverHandle = INVALID_HANDLE_VALUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -