Advertisement
theguild42

Untitled

Jun 22nd, 2020
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. /* Copyright (C) 2004 Rainmeter Project Developers
  2.  *
  3.  * This Source Code Form is subject to the terms of the GNU General Public
  4.  * License; either version 2 of the License, or (at your option) any later
  5.  * version. If a copy of the GPL was not distributed with this file, You can
  6.  * obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
  7.  
  8. #ifndef WIN32_LEAN_AND_MEAN
  9. #define WIN32_LEAN_AND_MEAN
  10. #endif
  11.  
  12. #include <windows.h>
  13. #include <algorithm>
  14. #include <WinSock2.h>
  15. #include <ws2tcpip.h>
  16. #include <Iphlpapi.h>
  17. #include <Netlistmgr.h>
  18. #include <lm.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "../API/RainmeterAPI.h"
  22. #include "../../Library/Export.h"
  23. #include"../../Common/Platform.h"
  24. #include "../../Common/StringUtil.h"
  25.  
  26. #pragma comment(lib, "Ws2_32.lib")
  27.  
  28. #define INADDR_ANY (ULONG)0x00000000
  29.  
  30. typedef struct
  31. {
  32.     int count;                      // Number of monitors
  33.     HMONITOR m_Monitors[32];        // Monitor info
  34.     RECT m_MonitorRect[32];         // Monitor rect on virtual screen
  35.     MONITORINFO m_MonitorInfo[32];  // Monitor information
  36. } MULTIMONITOR_INFO;
  37.  
  38. MULTIMONITOR_INFO m_Monitors = { 0 };
  39.  
  40. enum MeasureType
  41. {
  42.     MEASURE_COMPUTER_NAME,
  43.     MEASURE_USER_NAME,
  44.     MEASURE_WORK_AREA,
  45.     MEASURE_SCREEN_SIZE,
  46.     MEASURE_RAS_STATUS,
  47.     MEASURE_OS_VERSION,
  48.     MEASURE_PAGESIZE,
  49.     MEASURE_OS_BITS,
  50.     MEASURE_IDLE_TIME,
  51.     MEASURE_ADAPTER_DESCRIPTION,
  52.     MEASURE_ADAPTER_TYPE,
  53.     MEASURE_NET_MASK,
  54.     MEASURE_IP_ADDRESS,
  55.     MEASURE_IP6_ADDRESS,
  56.     MEASURE_GATEWAY_ADDRESS,
  57.     MEASURE_HOST_NAME,
  58.     MEASURE_DOMAIN_NAME,
  59.     MEASURE_DOMAINWORKGROUP,
  60.     MEASURE_DNS_SERVER,
  61.     MEASURE_INTERNET_CONNECTIVITY,
  62.     MEASURE_LAN_CONNECTIVITY,
  63.     MEASURE_WORK_AREA_TOP,
  64.     MEASURE_WORK_AREA_LEFT,
  65.     MEASURE_WORK_AREA_WIDTH,
  66.     MEASURE_WORK_AREA_HEIGHT,
  67.     MEASURE_SCREEN_WIDTH,
  68.     MEASURE_SCREEN_HEIGHT,
  69.     MEASURE_NUM_MONITORS,
  70.     MEASURE_VIRTUAL_SCREEN_TOP,
  71.     MEASURE_VIRTUAL_SCREEN_LEFT,
  72.     MEASURE_VIRTUAL_SCREEN_WIDTH,
  73.     MEASURE_VIRTUAL_SCREEN_HEIGHT,
  74.     MEASURE_TIMEZONE_ISDST,
  75.     MEASURE_TIMEZONE_BIAS,
  76.     MEASURE_TIMEZONE_STANDARD_BIAS,
  77.     MEASURE_TIMEZONE_STANDARD_NAME,
  78.     MEASURE_TIMEZONE_DAYLIGHT_BIAS,
  79.     MEASURE_TIMEZONE_DAYLIGHT_NAME,
  80.     MEASURE_USER_LOGONTIME
  81. };
  82.  
  83. struct MeasureData
  84. {
  85.     MeasureType type;
  86.     int data;
  87.  
  88.     bool useBestInterface;
  89.  
  90.     MeasureData() : type(), data(), useBestInterface(false) {}
  91. };
  92.  
  93. NLM_CONNECTIVITY GetNetworkConnectivity();
  94. BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
  95. int GetBestInterfaceOrByName(LPCWSTR data, bool& found);
  96.  
  97. bool g_Initialized = false;
  98.  
  99. PLUGIN_EXPORT void Initialize(void** data, void* rm)
  100. {
  101.     MeasureData* measure = new MeasureData;
  102.     *data = measure;
  103.  
  104.     if (!g_Initialized)
  105.     {
  106.         if (GetSystemMetrics(SM_CMONITORS) > 32)
  107.         {
  108.             LSLog(LOG_ERROR, nullptr, L"SysInfo.dll: Max amount of monitors supported is 32.");
  109.         }
  110.  
  111.         m_Monitors.count = 0;
  112.         EnumDisplayMonitors(nullptr, nullptr, MyInfoEnumProc, (LPARAM)(&m_Monitors));
  113.         g_Initialized = true;
  114.     }
  115. }
  116.  
  117. PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
  118. {
  119.     MeasureData* measure = (MeasureData*)data;
  120.  
  121.     int defaultData = -1;
  122.  
  123.     LPCTSTR type = RmReadString(rm, L"SysInfoType", L"");
  124.     if (_wcsicmp(L"COMPUTER_NAME", type) == 0)
  125.     {
  126.         measure->type = MEASURE_COMPUTER_NAME;
  127.     }
  128.     else if (_wcsicmp(L"USER_NAME", type) == 0)
  129.     {
  130.         measure->type = MEASURE_USER_NAME;
  131.     }
  132.     else if (_wcsicmp(L"WORK_AREA", type) == 0)
  133.     {
  134.         measure->type = MEASURE_WORK_AREA;
  135.     }
  136.     else if (_wcsicmp(L"SCREEN_SIZE", type) == 0)
  137.     {
  138.         measure->type = MEASURE_SCREEN_SIZE;
  139.     }
  140.     else if (_wcsicmp(L"RAS_STATUS", type) == 0)
  141.     {
  142.         measure->type = MEASURE_RAS_STATUS;
  143.     }
  144.     else if (_wcsicmp(L"OS_VERSION", type) == 0)
  145.     {
  146.         measure->type = MEASURE_OS_VERSION;
  147.     }
  148.     else if (_wcsicmp(L"PAGESIZE", type) == 0)
  149.     {
  150.         measure->type = MEASURE_PAGESIZE;
  151.     }
  152.     else if (_wcsicmp(L"OS_BITS", type) == 0)
  153.     {
  154.         measure->type = MEASURE_OS_BITS;
  155.     }
  156.     else if (_wcsicmp(L"IDLE_TIME", type) == 0)
  157.     {
  158.         measure->type = MEASURE_IDLE_TIME;
  159.     }
  160.     else if (_wcsicmp(L"ADAPTER_DESCRIPTION", type) == 0)
  161.     {
  162.         defaultData = 0;
  163.         measure->type = MEASURE_ADAPTER_DESCRIPTION;
  164.     }
  165.     else if (_wcsicmp(L"ADAPTER_TYPE", type) == 0)
  166.     {
  167.         defaultData = 0;
  168.         measure->type = MEASURE_ADAPTER_TYPE;
  169.     }
  170.     else if (_wcsicmp(L"NET_MASK", type) == 0)
  171.     {
  172.         defaultData = 0;
  173.         measure->type = MEASURE_NET_MASK;
  174.     }
  175.     else if (_wcsicmp(L"IP_ADDRESS", type) == 0)
  176.     {
  177.         defaultData = 0;
  178.         measure->type = MEASURE_IP_ADDRESS;
  179.     }
  180.     else if (_wcsicmp(L"IP6_ADDRESS", type) == 0)
  181.     {
  182.         defaultData = 0;
  183.         measure->type = MEASURE_IP6_ADDRESS;
  184.     }
  185.     else if (_wcsicmp(L"GATEWAY_ADDRESS", type) == 0)
  186.     {
  187.         defaultData = 0;
  188.         measure->type = MEASURE_GATEWAY_ADDRESS;
  189.     }
  190.     else if (_wcsicmp(L"HOST_NAME", type) == 0)
  191.     {
  192.         measure->type = MEASURE_HOST_NAME;
  193.     }
  194.     else if (_wcsicmp(L"DOMAIN_NAME", type) == 0)
  195.     {
  196.         measure->type = MEASURE_DOMAIN_NAME;
  197.     }
  198.     else if (_wcsicmp(L"DOMAINWORKGROUP", type) == 0)
  199.     {
  200.         measure->type = MEASURE_DOMAINWORKGROUP;
  201.     }
  202.     else if (_wcsicmp(L"DNS_SERVER", type) == 0)
  203.     {
  204.         measure->type = MEASURE_DNS_SERVER;
  205.     }
  206.     else if (_wcsicmp(L"INTERNET_CONNECTIVITY", type) == 0)
  207.     {
  208.         measure->type = MEASURE_INTERNET_CONNECTIVITY;
  209.     }
  210.     else if (_wcsicmp(L"LAN_CONNECTIVITY", type) == 0)
  211.     {
  212.         measure->type = MEASURE_LAN_CONNECTIVITY;
  213.     }
  214.     else if (_wcsicmp(L"WORK_AREA_TOP", type) == 0)
  215.     {
  216.         measure->type = MEASURE_WORK_AREA_TOP;
  217.     }
  218.     else if (_wcsicmp(L"WORK_AREA_LEFT", type) == 0)
  219.     {
  220.         measure->type = MEASURE_WORK_AREA_LEFT;
  221.     }
  222.     else if (_wcsicmp(L"WORK_AREA_WIDTH", type) == 0)
  223.     {
  224.         measure->type = MEASURE_WORK_AREA_WIDTH;
  225.     }
  226.     else if (_wcsicmp(L"WORK_AREA_HEIGHT", type) == 0)
  227.     {
  228.         measure->type = MEASURE_WORK_AREA_HEIGHT;
  229.     }
  230.     else if (_wcsicmp(L"SCREEN_WIDTH", type) == 0)
  231.     {
  232.         measure->type = MEASURE_SCREEN_WIDTH;
  233.     }
  234.     else if (_wcsicmp(L"SCREEN_HEIGHT", type) == 0)
  235.     {
  236.         measure->type = MEASURE_SCREEN_HEIGHT;
  237.     }
  238.     else if (_wcsicmp(L"NUM_MONITORS", type) == 0)
  239.     {
  240.         measure->type = MEASURE_NUM_MONITORS;
  241.     }
  242.     else if (_wcsicmp(L"VIRTUAL_SCREEN_TOP", type) == 0)
  243.     {
  244.         measure->type = MEASURE_VIRTUAL_SCREEN_TOP;
  245.     }
  246.     else if (_wcsicmp(L"VIRTUAL_SCREEN_LEFT", type) == 0)
  247.     {
  248.         measure->type = MEASURE_VIRTUAL_SCREEN_LEFT;
  249.     }
  250.     else if (_wcsicmp(L"VIRTUAL_SCREEN_WIDTH", type) == 0)
  251.     {
  252.         measure->type = MEASURE_VIRTUAL_SCREEN_WIDTH;
  253.     }
  254.     else if (_wcsicmp(L"VIRTUAL_SCREEN_HEIGHT", type) == 0)
  255.     {
  256.         measure->type = MEASURE_VIRTUAL_SCREEN_HEIGHT;
  257.     }
  258.     else if (_wcsicmp(L"TIMEZONE_ISDST", type) == 0)
  259.     {
  260.         measure->type = MEASURE_TIMEZONE_ISDST;
  261.     }
  262.     else if (_wcsicmp(L"TIMEZONE_BIAS", type) == 0)
  263.     {
  264.         measure->type = MEASURE_TIMEZONE_BIAS;
  265.     }
  266.     else if (_wcsicmp(L"TIMEZONE_STANDARD_BIAS", type) == 0)
  267.     {
  268.         measure->type = MEASURE_TIMEZONE_STANDARD_BIAS;
  269.     }
  270.     else if (_wcsicmp(L"TIMEZONE_STANDARD_NAME", type) == 0)
  271.     {
  272.         measure->type = MEASURE_TIMEZONE_STANDARD_NAME;
  273.     }
  274.     else if (_wcsicmp(L"TIMEZONE_DAYLIGHT_BIAS", type) == 0)
  275.     {
  276.         measure->type = MEASURE_TIMEZONE_DAYLIGHT_BIAS;
  277.     }
  278.     else if (_wcsicmp(L"TIMEZONE_DAYLIGHT_NAME", type) == 0)
  279.     {
  280.         measure->type = MEASURE_TIMEZONE_DAYLIGHT_NAME;
  281.     }
  282.     else if (_wcsicmp(L"USER_LOGONTIME", type) == 0)
  283.     {
  284.         measure->type = MEASURE_USER_LOGONTIME;
  285.     }
  286.     else
  287.     {
  288.         WCHAR buffer[256];
  289.         _snwprintf_s(buffer, _TRUNCATE, L"SysInfo.dll: SysInfoType=%s is not valid in [%s]", type, RmGetMeasureName(rm));
  290.         RmLog(LOG_ERROR, buffer);
  291.     }
  292.  
  293.     measure->useBestInterface = false;
  294.     if (measure->type >= MEASURE_ADAPTER_DESCRIPTION && measure->type <= MEASURE_GATEWAY_ADDRESS)
  295.     {
  296.         std::wstring siData = RmReadString(rm, L"SysInfoData", L"");
  297.         if (!siData.empty() && !std::all_of(siData.begin(), siData.end(), iswdigit))
  298.         {
  299.             measure->data = GetBestInterfaceOrByName(siData.c_str(), measure->useBestInterface);
  300.         }
  301.         else
  302.         {
  303.             measure->data = RmReadInt(rm, L"SysInfoData", defaultData);
  304.         }
  305.     }
  306.     else
  307.     {
  308.         measure->data = RmReadInt(rm, L"SysInfoData", defaultData);
  309.     }
  310. }
  311.  
  312. PLUGIN_EXPORT LPCWSTR GetString(void* data)
  313. {
  314.     MeasureData* measure = (MeasureData*)data;
  315.  
  316.     static WCHAR sBuffer[256];
  317.     DWORD sBufferLen = _countof(sBuffer);
  318.  
  319.     BYTE tmpBuffer[7168];
  320.     ULONG tmpBufferLen = _countof(tmpBuffer);
  321.  
  322.     auto convertToWide = [&](LPCSTR str)->LPCWSTR
  323.     {
  324.         MultiByteToWideChar(CP_ACP, 0, str, -1, sBuffer, 256);
  325.         return sBuffer;
  326.     };
  327.  
  328.     switch (measure->type)
  329.     {
  330.     case MEASURE_COMPUTER_NAME:
  331.         GetComputerName(sBuffer, &sBufferLen);
  332.         return sBuffer;
  333.  
  334.     case MEASURE_USER_NAME:
  335.         GetUserName(sBuffer, &sBufferLen);
  336.         return sBuffer;
  337.  
  338.     case MEASURE_WORK_AREA:
  339.         wsprintf(sBuffer, L"%i x %i", GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN));
  340.         return sBuffer;
  341.  
  342.     case MEASURE_SCREEN_SIZE:
  343.         wsprintf(sBuffer, L"%i x %i", GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
  344.         return sBuffer;
  345.  
  346.     case MEASURE_OS_VERSION:
  347.         return Platform::GetPlatformName();
  348.  
  349.     case MEASURE_ADAPTER_DESCRIPTION:
  350.         if (ERROR_SUCCESS == GetAdaptersInfo((IP_ADAPTER_INFO*)tmpBuffer, &tmpBufferLen))
  351.         {
  352.             PIP_ADAPTER_INFO info = (IP_ADAPTER_INFO*)tmpBuffer;
  353.             int i = 0;
  354.             while (info)
  355.             {
  356.                 if (measure->useBestInterface)
  357.                 {
  358.                     if (info->Index == measure->data)
  359.                     {
  360.                         return convertToWide(info->Description);
  361.                     }
  362.                 }
  363.                 else
  364.                 {
  365.                     if (i == measure->data)
  366.                     {
  367.                         return convertToWide(info->Description);
  368.                     }
  369.                 }
  370.  
  371.                 info = info->Next;
  372.                 ++i;
  373.             }
  374.         }
  375.         break;
  376.  
  377.     case MEASURE_ADAPTER_TYPE:
  378.         if (ERROR_SUCCESS == GetAdaptersInfo((IP_ADAPTER_INFO*)tmpBuffer, &tmpBufferLen))
  379.         {
  380.             PIP_ADAPTER_INFO info = (IP_ADAPTER_INFO*)tmpBuffer;
  381.             int i = 0;
  382.             while (info)
  383.             {
  384.                 if (measure->useBestInterface)
  385.                 {
  386.                     if (info->Index == measure->data)
  387.                     {
  388.                         switch (info->Type)
  389.                         {
  390.                         case IF_TYPE_ETHERNET_CSMACD: return L"Ethernet";
  391.                         case IF_TYPE_IEEE80211: return L"Wireless";
  392.                         }
  393.                         return L"Other";
  394.                     }
  395.                 }
  396.                 else
  397.                 {
  398.                     if (i == measure->data)
  399.                     {
  400.                         switch (info->Type)
  401.                         {
  402.                         case IF_TYPE_ETHERNET_CSMACD: return L"Ethernet";
  403.                         case IF_TYPE_IEEE80211: return L"Wireless";
  404.                         }
  405.                         return L"Other";
  406.                     }
  407.                 }
  408.  
  409.                 info = info->Next;
  410.                 ++i;
  411.             }
  412.         }
  413.         break;
  414.  
  415.         case MEASURE_IP6_ADDRESS:
  416.             if (NO_ERROR == GetTcp6Table((PMIB_TCP6TABLE)tmpBuffer, &tmpBufferLen, FALSE))
  417.             {
  418.                 PMIB_TCP6TABLE TcpTable = (PMIB_TCP6TABLE)tmpBuffer;
  419.                 if (measure->useBestInterface)
  420.                 {
  421.                     for (UINT i = 0; i < TcpTable->dwNumEntries; ++i)
  422.                     {
  423.                         if (TcpTable->table[i].dwLocalPort == measure->data)
  424.                         {
  425.                             DWORD ip = TcpTable->table[i].dwLocalPort;
  426.                             wsprintf(sBuffer, L"%x:%x:%x:%x:%x:%x:%x:%x", ip % 65535, (ip >> 32) % 65535, (ip >> 64) % 65535, (ip >> 96) % 65535, (ip >> 128) % 65535, (ip >> 160) % 65535, (ip >> 192) % 65535, (ip >> 224) % 65535);
  427.                             return sBuffer;
  428.                         }
  429.                     }
  430.                 }
  431.                 else if (measure->data >= 1000)
  432.                 {
  433.                     measure->data = measure->data - 999;
  434.                     for (UINT i = 0; i < TcpTable->dwNumEntries; ++i)
  435.                     {
  436.                         if ((TcpTable->table[i].dwLocalPort) & MIB_TCP_STATE_CLOSED) continue;
  437.                         --measure->data;
  438.                         if (measure->data == 0)
  439.                         {
  440.                             DWORD ip = TcpTable->table[i].dwLocalPort;
  441.                             wsprintf(sBuffer, L"%x:%x:%x:%x:%x:%x:%x:%x", ip % 65535, (ip >> 32) % 65535, (ip >> 64) % 65535, (ip >> 96) % 65535, (ip >> 128) % 65535, (ip >> 160) % 65535, (ip >> 192) % 65535, (ip >> 224) % 65535);
  442.                             return sBuffer;
  443.                         }
  444.                     }
  445.                 }
  446.                 else if (measure->data < (int)TcpTable->dwNumEntries)
  447.                 {
  448.                     DWORD ip = TcpTable->table[measure->data].dwLocalPort;
  449.                     wsprintf(sBuffer, L"%x:%x:%x:%x:%x:%x:%x:%x", ip % 65535, (ip >> 32) % 65535, (ip >> 64) % 65535, (ip >> 96) % 65535, (ip >> 128) % 65535, (ip >> 160) % 65535, (ip >> 192) % 65535, (ip >> 224) % 65535);
  450.                     return sBuffer;
  451.                 }
  452.             }
  453.             return L"";
  454.  
  455.     case MEASURE_IP_ADDRESS:
  456.         if (NO_ERROR == GetIpAddrTable((PMIB_IPADDRTABLE)tmpBuffer, &tmpBufferLen, FALSE))
  457.         {
  458.             PMIB_IPADDRTABLE ipTable = (PMIB_IPADDRTABLE)tmpBuffer;
  459.             if (measure->useBestInterface)
  460.             {
  461.                 for (UINT i = 0; i < ipTable->dwNumEntries; ++i)
  462.                 {
  463.                     if (ipTable->table[i].dwIndex == measure->data)
  464.                     {
  465.                         DWORD ip = ipTable->table[i].dwAddr;
  466.                         wsprintf(sBuffer, L"%i.%i.%i.%i", ip % 256, (ip >> 8) % 256, (ip >> 16) % 256, (ip >> 24) % 256);
  467.                         return sBuffer;
  468.                     }
  469.                 }
  470.             }
  471.             else if (measure->data >= 1000)
  472.             {
  473.                 measure->data = measure->data - 999;
  474.                 for (UINT i = 0; i < ipTable->dwNumEntries; ++i)
  475.                 {
  476.                     if ((ipTable->table[i].wType) & MIB_IPADDR_DISCONNECTED) continue;
  477.                     --measure->data;
  478.                     if (measure->data == 0)
  479.                     {
  480.                         DWORD ip = ipTable->table[i].dwAddr;
  481.                         wsprintf(sBuffer, L"%i.%i.%i.%i", ip % 256, (ip >> 8) % 256, (ip >> 16) % 256, (ip >> 24) % 256);
  482.                         return sBuffer;
  483.                     }
  484.                 }
  485.             }
  486.             else if (measure->data < (int)ipTable->dwNumEntries)
  487.             {
  488.                 DWORD ip = ipTable->table[measure->data].dwAddr;
  489.                 wsprintf(sBuffer, L"%i.%i.%i.%i", ip % 256, (ip >> 8) % 256, (ip >> 16) % 256, (ip >> 24) % 256);
  490.                 return sBuffer;
  491.             }
  492.         }
  493.         return L"";
  494.  
  495.     case MEASURE_NET_MASK:
  496.         if (NO_ERROR == GetIpAddrTable((PMIB_IPADDRTABLE)tmpBuffer, &tmpBufferLen, FALSE))
  497.         {
  498.             PMIB_IPADDRTABLE ipTable = (PMIB_IPADDRTABLE)tmpBuffer;
  499.             if (measure->useBestInterface)
  500.             {
  501.                 for (UINT i = 0; i < ipTable->dwNumEntries; ++i)
  502.                 {
  503.                     if (ipTable->table[i].dwIndex == measure->data)
  504.                     {
  505.                         DWORD ip = ipTable->table[i].dwMask;
  506.                         wsprintf(sBuffer, L"%i.%i.%i.%i", ip % 256, (ip >> 8) % 256, (ip >> 16) % 256, (ip >> 24) % 256);
  507.                         return sBuffer;
  508.                     }
  509.                 }
  510.             }
  511.             else if (measure->data < (int)ipTable->dwNumEntries)
  512.             {
  513.                 DWORD ip = ipTable->table[measure->data].dwMask;
  514.                 wsprintf(sBuffer, L"%i.%i.%i.%i", ip % 256, (ip >> 8) % 256, (ip >> 16) % 256, (ip >> 24) % 256);
  515.                 return sBuffer;
  516.             }
  517.         }
  518.         break;
  519.  
  520.     case MEASURE_GATEWAY_ADDRESS:
  521.         if (ERROR_SUCCESS == GetAdaptersInfo((IP_ADAPTER_INFO*)tmpBuffer, &tmpBufferLen))
  522.         {
  523.             PIP_ADAPTER_INFO info = (IP_ADAPTER_INFO*)tmpBuffer;
  524.             int i = 0;
  525.             while (info)
  526.             {
  527.                 if (measure->useBestInterface)
  528.                 {
  529.                     if (info->Index == measure->data)
  530.                     {
  531.                         return convertToWide(info->GatewayList.IpAddress.String);
  532.                     }
  533.                 }
  534.                 else if (i == measure->data)
  535.                 {
  536.                     return convertToWide(info->GatewayList.IpAddress.String);
  537.                 }
  538.                 info = info->Next;
  539.                 ++i;
  540.             }
  541.         }
  542.         break;
  543.  
  544.     case MEASURE_HOST_NAME:
  545.         if (ERROR_SUCCESS == GetNetworkParams((PFIXED_INFO)tmpBuffer, &tmpBufferLen))
  546.         {
  547.             PFIXED_INFO info = (PFIXED_INFO)tmpBuffer;
  548.             return convertToWide(info->HostName);
  549.         }
  550.         break;
  551.  
  552.     case MEASURE_DOMAIN_NAME:
  553.         if (ERROR_SUCCESS == GetNetworkParams((PFIXED_INFO)tmpBuffer, &tmpBufferLen))
  554.         {
  555.             PFIXED_INFO info = (PFIXED_INFO)tmpBuffer;
  556.             return convertToWide(info->DomainName);
  557.         }
  558.         break;
  559.  
  560.     case MEASURE_DOMAINWORKGROUP:
  561.     {
  562.         LPWKSTA_INFO_102 info = nullptr;
  563.         if (NERR_Success == NetWkstaGetInfo(nullptr, 102, (BYTE**)&info))
  564.         {
  565.             wcscpy(sBuffer, info->wki102_langroup);
  566.             NetApiBufferFree(info);
  567.             return sBuffer;
  568.         }
  569.     }
  570.     break;
  571.  
  572.     case MEASURE_DNS_SERVER:
  573.         if (ERROR_SUCCESS == GetNetworkParams((PFIXED_INFO)tmpBuffer, &tmpBufferLen))
  574.         {
  575.             PFIXED_INFO info = (PFIXED_INFO)tmpBuffer;
  576.             if (info->CurrentDnsServer)
  577.             {
  578.                 return convertToWide(info->CurrentDnsServer->IpAddress.String);
  579.             }
  580.             else
  581.             {
  582.                 return convertToWide(info->DnsServerList.IpAddress.String);
  583.             }
  584.         }
  585.         break;
  586.  
  587.     case MEASURE_TIMEZONE_STANDARD_NAME:
  588.         {
  589.             TIME_ZONE_INFORMATION tzi;
  590.             GetTimeZoneInformation(&tzi);
  591.             wcscpy(sBuffer, tzi.StandardName);
  592.             return sBuffer;
  593.         }
  594.  
  595.     case MEASURE_TIMEZONE_DAYLIGHT_NAME:
  596.         {
  597.             TIME_ZONE_INFORMATION tzi;
  598.             GetTimeZoneInformation(&tzi);
  599.             wcscpy(sBuffer, tzi.DaylightName);
  600.             return sBuffer;
  601.         }
  602.     }
  603.  
  604.     return nullptr;
  605. }
  606.  
  607. PLUGIN_EXPORT double Update(void* data)
  608. {
  609.     MeasureData* measure = (MeasureData*)data;
  610.  
  611.     switch (measure->type)
  612.     {
  613.     case MEASURE_PAGESIZE:
  614.         {
  615.             SYSTEM_INFO si = { 0 };
  616.             GetNativeSystemInfo(&si);
  617.             return (si.dwPageSize);
  618.         }
  619.  
  620.     case MEASURE_OS_BITS:
  621.         {
  622.             SYSTEM_INFO si = { 0 };
  623.             GetNativeSystemInfo(&si);
  624.             return (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  625.                 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) ? 64.0 : 32.0;
  626.         }
  627.  
  628.     case MEASURE_IDLE_TIME:
  629.         {
  630.             LASTINPUTINFO idle = { sizeof(LASTINPUTINFO) };
  631.             GetLastInputInfo(&idle);
  632.             return (double)((GetTickCount() - idle.dwTime) / 1000);
  633.         }
  634.  
  635.     case MEASURE_ADAPTER_TYPE:
  636.         {
  637.             BYTE tmpBuffer[7168];
  638.             ULONG tmpBufferLen = _countof(tmpBuffer);
  639.  
  640.             if (ERROR_SUCCESS == GetAdaptersInfo((IP_ADAPTER_INFO*)tmpBuffer, &tmpBufferLen))
  641.             {
  642.                 PIP_ADAPTER_INFO info = (IP_ADAPTER_INFO*)tmpBuffer;
  643.                 int i = 0;
  644.                 while (info)
  645.                 {
  646.                     if (measure->useBestInterface)
  647.                     {
  648.                         if (info->Index == measure->data)
  649.                         {
  650.                             return info->Type;
  651.                         }
  652.                     }
  653.                     else
  654.                     {
  655.                         if (i == measure->data)
  656.                         {
  657.                             return info->Type;
  658.                         }
  659.                     }
  660.  
  661.                     info = info->Next;
  662.                     ++i;
  663.                 }
  664.             }
  665.  
  666.             return 0.0;
  667.         }
  668.  
  669.     case MEASURE_INTERNET_CONNECTIVITY:
  670.         {
  671.             const auto connectivity = GetNetworkConnectivity();
  672.             return (connectivity & NLM_CONNECTIVITY_IPV4_INTERNET ||
  673.                 connectivity & NLM_CONNECTIVITY_IPV6_INTERNET) ? 1.0 : -1.0;
  674.         }
  675.  
  676.     case MEASURE_LAN_CONNECTIVITY:
  677.         return GetNetworkConnectivity() != NLM_CONNECTIVITY_DISCONNECTED ? 1.0 : -1.0;
  678.  
  679.     case MEASURE_WORK_AREA_WIDTH:
  680.         return (measure->data != -1)
  681.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcWork.right - m_Monitors.m_MonitorInfo[measure->data - 1].rcWork.left
  682.             : GetSystemMetrics(SM_CXFULLSCREEN);
  683.  
  684.     case MEASURE_WORK_AREA_HEIGHT:
  685.         return (measure->data != -1)
  686.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcWork.bottom - m_Monitors.m_MonitorInfo[measure->data - 1].rcWork.top
  687.             : GetSystemMetrics(SM_CYFULLSCREEN);
  688.  
  689.     case MEASURE_SCREEN_WIDTH:
  690.         return (measure->data != -1)
  691.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcMonitor.right - m_Monitors.m_MonitorInfo[measure->data - 1].rcMonitor.left
  692.             : GetSystemMetrics(SM_CXSCREEN);
  693.  
  694.     case MEASURE_SCREEN_HEIGHT:
  695.         return (measure->data != -1)
  696.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcMonitor.bottom - m_Monitors.m_MonitorInfo[measure->data - 1].rcMonitor.top
  697.             : GetSystemMetrics(SM_CYSCREEN);
  698.  
  699.     case MEASURE_VIRTUAL_SCREEN_WIDTH:
  700.         return GetSystemMetrics(SM_CXVIRTUALSCREEN);
  701.  
  702.     case MEASURE_VIRTUAL_SCREEN_HEIGHT:
  703.         return GetSystemMetrics(SM_CYVIRTUALSCREEN);
  704.  
  705.     case MEASURE_NUM_MONITORS:
  706.         return GetSystemMetrics(SM_CMONITORS);
  707.  
  708.     case MEASURE_WORK_AREA_TOP:
  709.         return (measure->data != -1)
  710.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcWork.top
  711.             : m_Monitors.m_MonitorInfo[0].rcWork.top;
  712.  
  713.     case MEASURE_WORK_AREA_LEFT:
  714.         return (measure->data != -1)
  715.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcWork.left
  716.             : m_Monitors.m_MonitorInfo[0].rcWork.left;
  717.  
  718.     case MEASURE_VIRTUAL_SCREEN_TOP:
  719.         return (measure->data != -1)
  720.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcMonitor.top
  721.             : GetSystemMetrics(SM_YVIRTUALSCREEN);
  722.  
  723.     case MEASURE_VIRTUAL_SCREEN_LEFT:
  724.         return (measure->data != -1)
  725.             ? m_Monitors.m_MonitorInfo[measure->data - 1].rcMonitor.left
  726.             : GetSystemMetrics(SM_XVIRTUALSCREEN);
  727.  
  728.     case MEASURE_TIMEZONE_ISDST:
  729.         {
  730.             TIME_ZONE_INFORMATION tzi;
  731.             DWORD ret = GetTimeZoneInformation(&tzi);
  732.             return ret == TIME_ZONE_ID_UNKNOWN ? -1.0 : ret - 1.0;
  733.         }
  734.  
  735.     case MEASURE_TIMEZONE_BIAS:
  736.         {
  737.             TIME_ZONE_INFORMATION tzi;
  738.             GetTimeZoneInformation(&tzi);
  739.             return (double)tzi.Bias;
  740.         }
  741.  
  742.     case MEASURE_TIMEZONE_STANDARD_BIAS:
  743.         {
  744.             TIME_ZONE_INFORMATION tzi;
  745.             GetTimeZoneInformation(&tzi);
  746.             return (double)tzi.StandardBias;
  747.         }
  748.  
  749.     case MEASURE_TIMEZONE_DAYLIGHT_BIAS:
  750.         {
  751.             TIME_ZONE_INFORMATION tzi;
  752.             GetTimeZoneInformation(&tzi);
  753.             return (double)tzi.DaylightBias;
  754.         }
  755.  
  756.     case MEASURE_USER_LOGONTIME:
  757.         {
  758.             // Get "last write time" of the current users environment
  759.             HKEY hKey;
  760.             FILETIME lastWrite;
  761.             if (RegOpenKey(HKEY_CURRENT_USER, L"Volatile Environment", &hKey) == ERROR_SUCCESS)
  762.             {
  763.                 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lastWrite) == ERROR_SUCCESS)
  764.                 {
  765.                     RegCloseKey(hKey);
  766.                     FileTimeToLocalFileTime(&lastWrite, &lastWrite);
  767.  
  768.                     LARGE_INTEGER li;
  769.                     li.LowPart = lastWrite.dwLowDateTime;
  770.                     li.HighPart = lastWrite.dwHighDateTime;
  771.                     return (double)(li.QuadPart / 10000000);
  772.                 }
  773.                 RegCloseKey(hKey);
  774.             }
  775.         }
  776.     }
  777.  
  778.     return 0.0;
  779. }
  780.  
  781. BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  782. {
  783.     MULTIMONITOR_INFO* m = (MULTIMONITOR_INFO*)dwData;
  784.     m->m_Monitors[m->count] = hMonitor;
  785.     memcpy(&(m->m_MonitorRect[m->count]), lprcMonitor, sizeof(RECT));
  786.     m->m_MonitorInfo[m->count].cbSize = sizeof(MONITORINFO);
  787.     GetMonitorInfo(hMonitor, &(m->m_MonitorInfo[m->count]));
  788.     ++m->count;
  789.     return true;
  790. }
  791.  
  792. PLUGIN_EXPORT void Finalize(void* data)
  793. {
  794.     MeasureData* measure = (MeasureData*)data;
  795.     delete measure;
  796. }
  797.  
  798. NLM_CONNECTIVITY GetNetworkConnectivity()
  799. {
  800.     // This is initialized like this in case INetworkListManager is not available (i.e. on WinXP).
  801.     // In such cases, we simply assume that there is an internet connection.
  802.     NLM_CONNECTIVITY connectivity =
  803.         (NLM_CONNECTIVITY)((int)NLM_CONNECTIVITY_IPV4_INTERNET | (int)NLM_CONNECTIVITY_IPV4_LOCALNETWORK);
  804.  
  805.     INetworkListManager* nlm;
  806.     HRESULT hr = CoCreateInstance(
  807.         CLSID_NetworkListManager, NULL, CLSCTX_INPROC_SERVER, __uuidof(INetworkListManager), (void**)&nlm);
  808.     if (SUCCEEDED(hr))
  809.     {
  810.         nlm->GetConnectivity(&connectivity);
  811.         nlm->Release();
  812.     }
  813.  
  814.     return connectivity;
  815. }
  816.  
  817. int GetBestInterfaceOrByName(LPCWSTR data, bool& found)
  818. {
  819.     int index = 0;
  820.  
  821.     if (_wcsicmp(data, L"BEST") == 0)
  822.     {
  823.         DWORD dwBestIndex;
  824.         if (NO_ERROR == GetBestInterface(INADDR_ANY, &dwBestIndex))
  825.         {
  826.             index = (int)dwBestIndex;
  827.             found = true;
  828.         }
  829.     }
  830.     else
  831.     {
  832.         BYTE buffer[7168];
  833.         ULONG bufLen = _countof(buffer);
  834.  
  835.         if (ERROR_SUCCESS == GetAdaptersInfo((IP_ADAPTER_INFO*)buffer, &bufLen))
  836.         {
  837.             PIP_ADAPTER_INFO info = (IP_ADAPTER_INFO*)buffer;
  838.             int i = 0;
  839.             while (info)
  840.             {
  841.                 if (_wcsicmp(data, StringUtil::Widen(info->Description).c_str()) == 0)
  842.                 {
  843.                     index = info->Index;
  844.                     found = true;
  845.                     break;
  846.                 }
  847.  
  848.                 info = info->Next;
  849.                 ++i;
  850.             }
  851.         }
  852.     }
  853.  
  854.     return index;
  855. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement