-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdapterInfo.cpp
More file actions
144 lines (126 loc) · 3.67 KB
/
AdapterInfo.cpp
File metadata and controls
144 lines (126 loc) · 3.67 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright © 2013 CCP ehf.
//
#include "StdAfx.h"
#include "AdapterInfo.h"
bool IsOptimus()
{
static bool initialized = false;
static bool isOptimus = false;
if( !initialized )
{
initialized = true;
isOptimus = GetModuleHandleW( L"nvd3d9wrap.dll" ) != nullptr;
}
return isOptimus;
}
bool GetHexIdFromDeviceId( const char* deviceId, uint32_t& deviceIdHex )
{
const char* deviceIdPrefix = "DEV_";
auto found = strstr( deviceId, deviceIdPrefix );
if( !found )
{
return false;
}
return sscanf_s( found + strlen( deviceIdPrefix ), "%x", &deviceIdHex ) == 1;
}
const char* GetRegistryPathToLocalMachine( const char* registryPath )
{
const char* rootPath = "\\Registry\\Machine\\";
if( strncmp( registryPath, rootPath, strlen( rootPath ) ) == 0 )
{
return registryPath + strlen( rootPath );
}
else
{
return registryPath;
}
}
bool GetDeviceRegistryKey( uint32_t deviceId, std::string& keyPath )
{
DISPLAY_DEVICE dd;
dd.cb = sizeof( DISPLAY_DEVICE );
for( int i = 0; EnumDisplayDevices( nullptr, i, &dd, 0 ); ++i )
{
uint32_t device;
if( GetHexIdFromDeviceId( dd.DeviceID, device ) && device == deviceId )
{
keyPath = GetRegistryPathToLocalMachine( dd.DeviceKey );
return true;
}
}
return false;
}
bool GetRegistryValue( HKEY key, const char* name, std::string& value )
{
char buffer[256];
DWORD dwcb_data = sizeof( buffer );
LONG result = RegQueryValueEx( key, name, nullptr, nullptr, reinterpret_cast<LPBYTE>( buffer ), &dwcb_data );
if( result == ERROR_SUCCESS )
{
value = buffer;
return true;
}
value = "";
return false;
}
BLUE_DEFINE_INTERFACE(IAdapterInfo);
BLUE_DEFINE( AdapterInfo );
const Be::ClassInfo* AdapterInfo::ExposeToBlue()
{
EXPOSURE_BEGIN( AdapterInfo, "" )
MAP_ATTRIBUTE( "index", index, "Video adapter index", Be::READ )
MAP_ATTRIBUTE( "driver", driver, "Video driver names", Be::READ )
MAP_ATTRIBUTE( "description", description, "Human-readable adapter name", Be::READ )
MAP_ATTRIBUTE( "deviceName", deviceName, "Adapter device name", Be::READ )
MAP_ATTRIBUTE( "driverVersion", driverVersion, "Video driver version", Be::READ )
MAP_ATTRIBUTE( "vendorID", vendorID, "Video adapter vendor ID", Be::READ )
MAP_ATTRIBUTE( "deviceID", deviceID, "Video adapter device ID", Be::READ )
MAP_ATTRIBUTE( "subSystemID", subSystemID, "Video adapter sub-system ID", Be::READ )
MAP_ATTRIBUTE( "revision", subSystemID, "", Be::READ )
MAP_ATTRIBUTE( "driverVersionString", driverVersionString, "", Be::READ )
MAP_ATTRIBUTE( "driverDate", driverDate, "", Be::READ )
MAP_ATTRIBUTE( "driverVendor", driverVendor, "", Be::READ )
MAP_ATTRIBUTE( "isOptimus", isOptimus, "", Be::READ )
MAP_PROPERTY_READONLY( "deviceIdentifier", GetDeviceIdentifierString, "Device identifier GUID as a string" )
EXPOSURE_END()
}
void AdapterInfo::PopulateDriverVersion()
{
std::string keyPath;
if( !GetDeviceRegistryKey( deviceID, keyPath ) )
{
return;
}
HKEY key;
LONG result = RegOpenKeyEx( HKEY_LOCAL_MACHINE, keyPath.c_str(), 0, KEY_QUERY_VALUE, &key );
if( result != ERROR_SUCCESS )
{
return;
}
GetRegistryValue( key, "DriverVersion", driverVersionString );
GetRegistryValue( key, "DriverDate", driverDate );
GetRegistryValue( key, "ProviderName", driverVendor );
isOptimus = IsOptimus();
RegCloseKey( key );
}
AdapterInfo::AdapterInfo() :
index( 0 ),
driverVersion( 0 ),
vendorID( 0 ),
deviceID( 0 ),
subSystemID( 0 ),
revision( 0 )
{
memset( &deviceIdentifier, 0, sizeof( deviceIdentifier ) );
}
std::string AdapterInfo::GetDeviceIdentifierString() const
{
LPOLESTR guidstr;
if( SUCCEEDED( StringFromCLSID( deviceIdentifier, &guidstr ) ) )
{
CW2A str( guidstr );
CoTaskMemFree( guidstr );
return (const char*)str;
}
return "";
}