Reputation: 6172
We are needing to upgrade an application we have to the Universal Windows App (UWP) framework. The application's main purpose is to collect diagnostic statistics and send them back to our server via a rest call.
However I am not finding any api's available for getting the statistics
Prior we were
PerformanceCounter _memoryCounter = new PerformanceCounter();
public SystemProperty GetPhysicalMemory()
{
string s = _QueryComputerSystem("totalphysicalmemory");
double totalphysicalmemory = Convert.ToDouble(s);
double d = _GetCounterValue(_memoryCounter, "Memory", "Available Bytes", null);
return new SystemProperty { PropertyName = "Physical Memory", Total = totalphysicalmemory, Used = totalphysicalmemory - d };
}
which returned total used and free bytes of memory. We also collected network and cpu stats. None of which are compatible with the new framework. What namespace should I be looking at for this functionality? Or do I need to use something like Pinvoke (link)?
Upvotes: 0
Views: 1544
Reputation: 6172
In the end I found calling native functions to be the best solution. Link: really good pinvoke site
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Monitoring
{
public static class NativeCallsWrapper
{
private static SYSTEM_INFO sysInfo = new SYSTEM_INFO();
private static MEMORYSTATUSEX mem = new MEMORYSTATUSEX();
[DllImport("kernel32.dll", SetLastError = false)]
public static extern void GetSystemInfo([In, Out] SYSTEM_INFO Info);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
static NativeCallsWrapper()
{
GetSystemInfo(sysInfo);
GlobalMemoryStatusEx(mem);
}
[StructLayout(LayoutKind.Explicit)]
public struct SYSTEM_INFO_UNION
{
[FieldOffset(0)]
public UInt32 OemId;
[FieldOffset(0)]
public UInt16 ProcessorArchitecture;
[FieldOffset(2)]
public UInt16 Reserved;
}
public struct SYSTEM_INFO
{
public SYSTEM_INFO_UNION CpuInfo;
public UInt32 PageSize;
public UInt32 MinimumApplicationAddress;
public UInt32 MaximumApplicationAddress;
public UInt32 ActiveProcessorMask;
public UInt32 NumberOfProcessors;
public UInt32 ProcessorType;
public UInt32 AllocationGranularity;
public UInt16 ProcessorLevel;
public UInt16 ProcessorRevision;
}
[StructLayout(LayoutKind.Sequential)]
public class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
}
}
public static GeneralStatistics getGeneralStatistics()
{
GeneralStatistics generalStatistics = new GeneralStatistics();
generalStatistics.numberOfProcesses = (int)sysInfo.NumberOfProcessors;
generalStatistics.memoryTotal = mem.ullTotalPhys / 1048;
generalStatistics.memoryInUse = (mem.ullTotalPhys - mem.ullAvailPhys) / 1048;
return generalStatistics;
}
}
}
Upvotes: 2
Reputation: 1353
you can use MemoryManager to get the Memory of the device
https://msdn.microsoft.com/en-us/library/windows.system.memorymanager.aspx
also you can get the information of the device
https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.aspx
Update: Here is an example getting more details of the current Device https://www.suchan.cz/2015/08/uwp-quick-tip-getting-device-os-and-app-info/
Upvotes: 1