-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
83 lines (70 loc) · 2.07 KB
/
Utils.cpp
File metadata and controls
83 lines (70 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Utils.cpp: implementation of the HelperFunctions class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "resource.h"
#include "Utils.h"
#include <Winsvc.h>
// Function resolves the fosedevice name to drive name
BOOL GetDrive( LPCTSTR pszDosName, CString& csDrive, bool bDriveLetterOnly )
{
TCHAR tcDeviceName[50];
TCHAR tcDrive[3] = _T("A:");
// Iterating through the drive letters
for ( TCHAR actDrive = _T('A'); actDrive <= _T('Z'); actDrive++ )
{
tcDrive[0] = actDrive;
// Query the device for the drive letter
if ( QueryDosDevice( tcDrive, tcDeviceName, 50 ) != 0 )
{
// Is this the drive letter we are looking for?
if ( _tcsnicmp( tcDeviceName, pszDosName, _tcslen( tcDeviceName ) ) == 0 )
{
if( bDriveLetterOnly )
{
csDrive = tcDrive;
}
else
{
csDrive = pszDosName;
csDrive.Replace( tcDeviceName, tcDrive );
}
return TRUE;
}
}
}
return FALSE;
}
BOOL EnableTokenPrivilege(LPCTSTR pszPrivilege)
{
// do it only once
static bool bEnabled = false;
if( bEnabled )
{
return TRUE;
}
bEnabled = true;
HANDLE hToken = 0;
TOKEN_PRIVILEGES tkp = {0};
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, &hToken))
{
return FALSE;
}
// Get the LUID for the privilege.
if(LookupPrivilegeValue(NULL, pszPrivilege,
&tkp.Privileges[0].Luid))
{
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Set the privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
return TRUE;
}
return FALSE;
}