📄 utils.cpp
字号:
// Move to the next slot.
j++;
nVol++;
}
} while ( FALSE );
return bRet;
}
__PU8 UtilGetFilesystemString ( __U8 FSId )
{
__U32 i;
__U32 nCount;
__PS8 szFileSys;
szFileSys = (__PS8) "Unknown";
nCount = ARRAYSIZE (gs_FilesystemTable);
for ( i = 0; i < nCount; i++)
{
if (gs_FilesystemTable[i].nFSId == FSId)
{
return (__PU8) gs_FilesystemTable[i].szFilesys ;
}
}
return (__PU8) "Unknown";
}
BOOL UtilGetFreeDiskSpace ( __U8 MntPt, __PU32 Free )
{
BOOL bRet;
DWORD dwSPC;
DWORD dwBPS;
DWORD dwFree;
DWORD dwTotal;
CHAR buf[16];
__U32 nRet;
nRet = 0;
dwSPC = 0;
dwBPS = 0;
dwFree = 0;
dwTotal = 0;
bRet = FALSE;
ZeroMemory (buf, sizeof ( buf ) );
sprintf(buf, "%C:\\", MntPt );
bRet = GetDiskFreeSpace (buf, &dwSPC, &dwBPS, &dwFree, &dwTotal);
if ( bRet )
{
(*Free) = UtilComputeFreeSpace (dwFree, dwSPC, dwBPS);
}
return bRet;
}
BOOL UtilGetVolumeLabel ( __U8 MntPt, CHAR szVolName[11] )
{
BOOL bRet;
CHAR buf[16];
bRet = FALSE;
ZeroMemory (buf, sizeof ( buf ) );
sprintf(buf, "%C:\\", MntPt );
bRet = GetVolumeInformation (buf, szVolName, 11, NULL, NULL, NULL, NULL, 0);
return bRet;
}
BOOL UtilGetDeviceName (PARTITION_INFO* Partition, __U8 szDevName[64])
{
sprintf ((PCHAR)szDevName, "/dev/hd%c%lu", Partition->nDiskNumber+'a', Partition->nPartNumber);
return TRUE;
}
BOOL UtilMount ( __U32 nDisk, __U32 nPartition, __U8 nDriveLetter, BOOL bPreserve )
{
BOOL bRet;
CHAR szDevName[128];
CHAR szMountPoint[32];
sprintf (szDevName, "\\Device\\Harddisk%d\\Partition%d", nDisk, nPartition);
sprintf (szMountPoint, "%C:", nDriveLetter);
bRet = DefineDosDevice (DDD_RAW_TARGET_PATH, szMountPoint, szDevName);
if (bRet)
{
// Do we need to make this mount persist across system restarts ?
if (bPreserve)
{
bRet = UtilSetAutoMount (nDisk, nPartition, nDriveLetter);
}
else
{
bRet = UtilRemoveAutoMount (nDriveLetter);
}
}
return bRet;
}
BOOL UtilUnMount ( __U8 nDriveLetter )
{
BOOL bRet;
HANDLE hDev;
DWORD dwBytes;
CHAR szDevName[128];
dwBytes = 0;
bRet = FALSE;
hDev = INVALID_HANDLE_VALUE;
ZeroMemory ( szDevName, sizeof ( szDevName ) );
do
{
sprintf ( szDevName, "\\\\.\\%C:", nDriveLetter );
hDev = CreateFile(
szDevName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
BREAK_IF_EQUAL (hDev, INVALID_HANDLE_VALUE);
bRet = DeviceIoControl(
hDev,
FSCTL_LOCK_VOLUME,
NULL, 0, NULL, 0, &dwBytes, NULL );
BREAK_IF_FALSE (bRet);
bRet = DeviceIoControl(
hDev,
FSCTL_DISMOUNT_VOLUME,
NULL, 0, NULL, 0, &dwBytes, NULL);
BREAK_IF_FALSE (bRet);
sprintf ( szDevName, "%C:", nDriveLetter );
bRet = DefineDosDevice(
DDD_REMOVE_DEFINITION,
szDevName, NULL );
if (bRet)
{
UtilRemoveAutoMount (nDriveLetter);
}
} while (FALSE);
SAFE_CLOSE_HANDLE(hDev);
return bRet;
}
//
// Taken from Microsoft KB: Q118626.
//
//
// Make up some private access rights.
//
#define ACCESS_READ 1
#define ACCESS_WRITE 2
BOOL UtilCheckIfAdmin ()
{
HANDLE hToken;
DWORD dwStatus;
DWORD dwAccessMask;
DWORD dwAccessDesired;
DWORD dwACLSize;
PACL pACL = NULL;
PSID psidAdmin = NULL;
BOOL bReturn = FALSE;
DWORD dwStructureSize = sizeof(PRIVILEGE_SET);
PRIVILEGE_SET ps;
GENERIC_MAPPING GenericMapping;
PSECURITY_DESCRIPTOR psdAdmin = NULL;
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
do
{
// AccessCheck() requires an impersonation token.
ImpersonateSelf (SecurityImpersonation);
if (!OpenThreadToken (GetCurrentThread (),
TOKEN_QUERY,
FALSE,
&hToken))
{
if (ERROR_NO_TOKEN != GetLastError ())
{
break;
}
//
// If the thread does not have an access token, we'll
// examine the access token associated with the process.
//
if (!OpenProcessToken (GetCurrentProcess (),
TOKEN_QUERY,
&hToken))
{
break;
}
}
if (!AllocateAndInitializeSid (&SystemSidAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &psidAdmin))
{
break;
}
psdAdmin = LocalAlloc (LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
BREAK_IF_NULL ( psdAdmin );
if (!InitializeSecurityDescriptor (psdAdmin,
SECURITY_DESCRIPTOR_REVISION))
{
break;
}
//
// Compute size needed for the ACL.
//
dwACLSize = sizeof(ACL) +
sizeof(ACCESS_ALLOWED_ACE) +
GetLengthSid(psidAdmin) - sizeof(DWORD);
//
// Allocate memory for ACL.
//
pACL = (PACL) LocalAlloc (LPTR, dwACLSize);
BREAK_IF_NULL ( pACL );
//
// Initialize the new ACL.
//
if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
{
break;
}
dwAccessMask = ACCESS_READ | ACCESS_WRITE;
//
// Add the access-allowed ACL to the DACL.
//
if (!AddAccessAllowedAce (pACL,
ACL_REVISION2,
dwAccessMask,
psidAdmin))
{
break;
}
//
// Set our DACL to the SD.
//
if (!SetSecurityDescriptorDacl (psdAdmin,
TRUE,
pACL,
FALSE))
{
break;
}
//
// AccessCheck is sensitive about what is in the SD; set
// the group and owner.
//
SetSecurityDescriptorGroup (psdAdmin, psidAdmin, FALSE);
SetSecurityDescriptorOwner (psdAdmin, psidAdmin, FALSE);
if (!IsValidSecurityDescriptor (psdAdmin))
{
break;
}
dwAccessDesired = ACCESS_READ;
//
// Initialize GenericMapping structure even though we
// won't be using generic rights.
//
GenericMapping.GenericRead = ACCESS_READ;
GenericMapping.GenericWrite = ACCESS_WRITE;
GenericMapping.GenericExecute = 0;
GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
if (!AccessCheck(psdAdmin,
hToken,
dwAccessDesired,
&GenericMapping,
&ps,
&dwStructureSize, &dwStatus, &bReturn))
{
break;
}
RevertToSelf ();
} while (FALSE);
// Cleanup.
if (pACL) LocalFree (pACL);
if (psdAdmin) LocalFree (psdAdmin);
if (psidAdmin) FreeSid (psidAdmin);
return bReturn;
}
BOOL UtilCheckDriverStatus ()
{
BOOL bRet;
SC_HANDLE hSCM;
SC_HANDLE hService;
SERVICE_STATUS srvStatus;
bRet = FALSE;
hSCM = NULL;
hService = NULL;
ZeroMemory (&srvStatus, sizeof (srvStatus));
do
{
// Open a handle to the Service Control Manager.
hSCM = OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS);
BREAK_IF_NULL (hSCM);
// Open a handle to the FSD service.
hService = OpenService ( hSCM,
STR_SERVICE_NAME,
SERVICE_QUERY_STATUS|SERVICE_START);
BREAK_IF_NULL (hService);
// Check the status of the service.
bRet = QueryServiceStatus ( hService, &srvStatus );
BREAK_IF_FALSE (bRet);
if ( SERVICE_RUNNING == srvStatus.dwCurrentState )
{
bRet = TRUE;
break;
}
// Try starting the service.
bRet = StartService ( hService, 0, NULL );
// StartService may also fail if the servcie is already running.
if (FALSE == bRet)
{
if (ERROR_SERVICE_ALREADY_RUNNING == GetLastError())
{
bRet = TRUE;
}
}
} while (FALSE);
if (NULL != hService)
{
CloseServiceHandle (hService);
}
if (NULL != hSCM)
{
CloseServiceHandle (hSCM);
}
return bRet;
}
VOID UtilShowMessage (LPCTSTR sErrMsg, DWORD dwStyle, DWORD dwErrCode )
{
LPVOID lpMsgBuf;
if (ERROR_SUCCESS != dwErrCode)
{
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dwErrCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL
);
CString str;
str.Format ("%s\r\n\r\nError Code: 0x%08lX\r\n\r\nError Message: %s", sErrMsg, dwErrCode, lpMsgBuf );
AfxMessageBox (str, dwStyle);
LocalFree (lpMsgBuf);
}
else
{
AfxMessageBox (sErrMsg, dwStyle);
}
}
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -