Team Fortress 2 Source Code as on 22/4/2020
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.

182 lines
5.0 KiB

  1. //========= Copyright 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. uint64 GetVidMemBytes( void )
  22. {
  23. static int bBeenHere = false;
  24. static uint64 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. // Obtain the initial locator to WMI
  38. IWbemLocator *pLoc = NULL;
  39. hr = CoCreateInstance(
  40. CLSID_WbemLocator,
  41. 0,
  42. CLSCTX_INPROC_SERVER,
  43. IID_IWbemLocator, (LPVOID *) &pLoc);
  44. if ( FAILED( hr ) )
  45. {
  46. OutputDebugString ( "GetWMIDeviceStats - Failed to create IWbemLocator object.\n");
  47. CoUninitialize();
  48. return 0;
  49. }
  50. // Connect to WMI through the IWbemLocator::ConnectServer method
  51. IWbemServices *pSvc = NULL;
  52. // Connect to the root\cimv2 namespace with
  53. // the current user and obtain pointer pSvc
  54. // to make IWbemServices calls.
  55. hr = pLoc->ConnectServer(
  56. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  57. NULL, // User name. NULL = current user
  58. NULL, // User password. NULL = current
  59. 0, // Locale. NULL indicates current
  60. NULL, // Security flags.
  61. 0, // Authority (e.g. Kerberos)
  62. 0, // Context object
  63. &pSvc // pointer to IWbemServices proxy
  64. );
  65. if ( FAILED( hr ) )
  66. {
  67. OutputDebugString ( "GetWMIDeviceStats - Could not connect.\n");
  68. pLoc->Release();
  69. CoUninitialize();
  70. return 0;
  71. }
  72. // OutputDebugString ( L"GetWMIDeviceStats - Connected to ROOT\\CIMV2 WMI namespace\n");
  73. // Set security levels on the proxy
  74. hr = CoSetProxyBlanket(
  75. pSvc, // Indicates the proxy to set
  76. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  77. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  78. NULL, // Server principal name
  79. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  80. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  81. NULL, // client identity
  82. EOAC_NONE // proxy capabilities
  83. );
  84. if ( FAILED( hr ) )
  85. {
  86. OutputDebugString ( "GetWMIDeviceStats - Could not set proxy blanket.\n");
  87. pSvc->Release();
  88. pLoc->Release();
  89. CoUninitialize();
  90. return 0;
  91. }
  92. // Use the IWbemServices pointer to make requests of WMI
  93. //
  94. // --- Win32_VideoController --------------------------------------------------
  95. //
  96. IEnumWbemClassObject* pEnumerator = NULL;
  97. hr = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_VideoController"),
  98. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
  99. if ( FAILED( hr ) )
  100. {
  101. OutputDebugString ( "GetWMIDeviceStats - Query for Win32_VideoController failed.\n");
  102. pSvc->Release();
  103. pLoc->Release();
  104. CoUninitialize();
  105. return 0;
  106. }
  107. // Get the data from the above query
  108. IWbemClassObject *pclsObj = NULL;
  109. ULONG uReturn = 0;
  110. while ( pEnumerator )
  111. {
  112. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
  113. if(0 == uReturn)
  114. {
  115. break;
  116. }
  117. VARIANT vtProp;
  118. VariantInit(&vtProp);
  119. // Pluck a series of properties out of the query from Win32_VideoController
  120. // hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0); // Basically the same as "VideoProcessor"
  121. // if ( SUCCEEDED( hr ) )
  122. // {
  123. // wsprintf( pAdapter->m_szPrimaryAdapterDescription, vtProp.bstrVal );
  124. // }
  125. hr = pclsObj->Get(L"AdapterRAM", 0, &vtProp, 0, 0);
  126. if ( SUCCEEDED( hr ) )
  127. {
  128. nBytes = vtProp.ulVal; // Video RAM in bytes, AdatperRam is returned as the I4 type so we read it out as unsigned int,
  129. // see http://msdn.microsoft.com/en-us/library/windows/desktop/aa394512(v=vs.85).aspx
  130. }
  131. VariantClear(&vtProp);
  132. }
  133. // Cleanup
  134. pSvc->Release();
  135. pLoc->Release();
  136. pEnumerator->Release();
  137. if ( pclsObj )
  138. {
  139. pclsObj->Release();
  140. }
  141. CoUninitialize();
  142. return nBytes;
  143. }