Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.8 KiB

  1. //====== Copyright � 1996-2006, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "resource.h"
  7. // Avoid conflicts with MSVC headers and memdbgon.h
  8. #undef PROTECTED_THINGS_ENABLE
  9. #include "basetypes.h"
  10. #define _WIN32_DCOM
  11. #include <comdef.h>
  12. #pragma warning( disable : 4127 ) // VS 2010 warning?
  13. #pragma warning( disable : 4805 ) // VS 2013 warning: warning C4805: '==' : unsafe mix of type 'INT' and type 'bool' in operation
  14. #include <atlcomtime.h>
  15. #pragma warning( default : 4805 )
  16. #pragma warning( default : 4127 )
  17. #include <Wbemidl.h>
  18. // NOTE: This has to be the last file included!
  19. #include "tier0/memdbgon.h"
  20. # pragma comment(lib, "wbemuuid.lib")
  21. int GetVidMemBytes( void )
  22. {
  23. static int bBeenHere = false;
  24. static int nBytes = 0;
  25. if( bBeenHere )
  26. {
  27. return nBytes;
  28. }
  29. bBeenHere = true;
  30. // Initialize COM
  31. HRESULT hr = CoInitialize( NULL );
  32. if ( FAILED( hr ) )
  33. {
  34. OutputDebugString ( "GetWMIDeviceStats - Unable to initialize COM library.\n");
  35. return 0;
  36. }
  37. // Set general COM security levels --------------------------
  38. // Note: If you are using Windows 2000, you need to specify
  39. // the default authentication credentials for a user by using
  40. // a SOLE_AUTHENTICATION_LIST structure in the pAuthList
  41. // parameter of CoInitializeSecurity ------------------------
  42. hr = CoInitializeSecurity(
  43. NULL,
  44. -1, // COM authentication
  45. NULL, // Authentication services
  46. NULL, // Reserved
  47. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  48. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  49. NULL, // Authentication info
  50. EOAC_NONE, // Additional capabilities
  51. NULL // Reserved
  52. );
  53. if ( FAILED( hr ) )
  54. {
  55. OutputDebugString ( "GetWMIDeviceStats - Unable to initialize security.\n");
  56. CoUninitialize();
  57. return 0;
  58. }
  59. // Obtain the initial locator to WMI
  60. IWbemLocator *pLoc = NULL;
  61. hr = CoCreateInstance(
  62. CLSID_WbemLocator,
  63. 0,
  64. CLSCTX_INPROC_SERVER,
  65. IID_IWbemLocator, (LPVOID *) &pLoc);
  66. if ( FAILED( hr ) )
  67. {
  68. OutputDebugString ( "GetWMIDeviceStats - Failed to create IWbemLocator object.\n");
  69. CoUninitialize();
  70. return 0;
  71. }
  72. // Connect to WMI through the IWbemLocator::ConnectServer method
  73. IWbemServices *pSvc = NULL;
  74. // Connect to the root\cimv2 namespace with
  75. // the current user and obtain pointer pSvc
  76. // to make IWbemServices calls.
  77. hr = pLoc->ConnectServer(
  78. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  79. NULL, // User name. NULL = current user
  80. NULL, // User password. NULL = current
  81. 0, // Locale. NULL indicates current
  82. NULL, // Security flags.
  83. 0, // Authority (e.g. Kerberos)
  84. 0, // Context object
  85. &pSvc // pointer to IWbemServices proxy
  86. );
  87. if ( FAILED( hr ) )
  88. {
  89. OutputDebugString ( "GetWMIDeviceStats - Could not connect.\n");
  90. pLoc->Release();
  91. CoUninitialize();
  92. return 0;
  93. }
  94. // OutputDebugString ( L"GetWMIDeviceStats - Connected to ROOT\\CIMV2 WMI namespace\n");
  95. // Set security levels on the proxy
  96. hr = CoSetProxyBlanket(
  97. pSvc, // Indicates the proxy to set
  98. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  99. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  100. NULL, // Server principal name
  101. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  102. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  103. NULL, // client identity
  104. EOAC_NONE // proxy capabilities
  105. );
  106. if ( FAILED( hr ) )
  107. {
  108. OutputDebugString ( "GetWMIDeviceStats - Could not set proxy blanket.\n");
  109. pSvc->Release();
  110. pLoc->Release();
  111. CoUninitialize();
  112. return 0;
  113. }
  114. // Use the IWbemServices pointer to make requests of WMI
  115. //
  116. // --- Win32_VideoController --------------------------------------------------
  117. //
  118. IEnumWbemClassObject* pEnumerator = NULL;
  119. hr = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_VideoController"),
  120. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
  121. if ( FAILED( hr ) )
  122. {
  123. OutputDebugString ( "GetWMIDeviceStats - Query for Win32_VideoController failed.\n");
  124. pSvc->Release();
  125. pLoc->Release();
  126. CoUninitialize();
  127. return 0;
  128. }
  129. // Get the data from the above query
  130. IWbemClassObject *pclsObj = NULL;
  131. ULONG uReturn = 0;
  132. while ( pEnumerator )
  133. {
  134. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
  135. if(0 == uReturn)
  136. {
  137. break;
  138. }
  139. VARIANT vtProp;
  140. VariantInit(&vtProp);
  141. // Pluck a series of properties out of the query from Win32_VideoController
  142. // hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0); // Basically the same as "VideoProcessor"
  143. // if ( SUCCEEDED( hr ) )
  144. // {
  145. // wsprintf( pAdapter->m_szPrimaryAdapterDescription, vtProp.bstrVal );
  146. // }
  147. hr = pclsObj->Get(L"AdapterRAM", 0, &vtProp, 0, 0);
  148. if ( SUCCEEDED( hr ) )
  149. {
  150. nBytes = vtProp.intVal; // Video RAM in bytes
  151. }
  152. VariantClear(&vtProp);
  153. }
  154. // Cleanup
  155. pSvc->Release();
  156. pLoc->Release();
  157. pEnumerator->Release();
  158. pclsObj->Release();
  159. CoUninitialize();
  160. return nBytes;
  161. }