新增系统信息,CPU

This commit is contained in:
snltty
2023-09-20 17:57:32 +08:00
parent 2843122591
commit 0b1659a569
45 changed files with 348 additions and 93 deletions

View File

@@ -58,7 +58,7 @@ public:
}; };
extern "C" __declspec(dllexport) IWbemLocator * InitIWbemLocator() extern "C" __declspec(dllexport) IWbemLocator * InitSystemIWbemLocator()
{ {
HRESULT hr; HRESULT hr;
hr = CoInitialize(nullptr); hr = CoInitialize(nullptr);
@@ -101,7 +101,7 @@ extern "C" __declspec(dllexport) IWbemLocator * InitIWbemLocator()
return pWbemLocator; return pWbemLocator;
} }
extern "C" __declspec(dllexport) IWbemServices * InitIWbemServices(IWbemLocator * pWbemLocator) extern "C" __declspec(dllexport) IWbemServices * InitSystemIWbemServices(IWbemLocator * pWbemLocator)
{ {
HRESULT hr; HRESULT hr;
// 连接WMI服务 // 连接WMI服务
@@ -128,7 +128,7 @@ extern "C" __declspec(dllexport) IWbemServices * InitIWbemServices(IWbemLocator
return pWbemServices; return pWbemServices;
} }
extern "C" __declspec(dllexport) HRESULT GetBrightness(IWbemServices* pServices, BYTE* pBrightness) extern "C" __declspec(dllexport) HRESULT GetSystemBrightness(IWbemServices* pServices, BYTE* pBrightness)
{ {
HRESULT hResult = S_OK; HRESULT hResult = S_OK;
@@ -173,7 +173,7 @@ extern "C" __declspec(dllexport) HRESULT GetBrightness(IWbemServices* pServices,
return hResult; return hResult;
} }
extern "C" __declspec(dllexport) HRESULT SetBrightness(IWbemServices * pServices, BYTE brightness) extern "C" __declspec(dllexport) HRESULT SetSystemBrightness(IWbemServices * pServices, BYTE brightness)
{ {
HRESULT hResult = S_OK; HRESULT hResult = S_OK;
@@ -272,13 +272,12 @@ extern "C" __declspec(dllexport) HRESULT SetBrightness(IWbemServices * pServices
} }
extern "C" __declspec(dllexport) bool FreeLight(IWbemLocator * pWbemLocator, IWbemServices * pWbemServices, IWbemObjectSink * pEventSink) extern "C" __declspec(dllexport) bool FreeSystemBrightness(IWbemLocator * pWbemLocator, IWbemServices * pWbemServices, IWbemObjectSink * pEventSink)
{ {
if (pWbemLocator != NULL) if (pWbemLocator != NULL)
{ {
pWbemLocator->Release(); pWbemLocator->Release();
} }
if (pWbemServices != NULL) if (pWbemServices != NULL)
{ {
pWbemServices->Release(); pWbemServices->Release();
@@ -291,7 +290,7 @@ extern "C" __declspec(dllexport) bool FreeLight(IWbemLocator * pWbemLocator, IWb
return true; return true;
} }
extern "C" __declspec(dllexport) IWbemObjectSink * StartListener(IWbemServices * pWbemServices, BrightnessChangeCallback g_pfnCallback) extern "C" __declspec(dllexport) IWbemObjectSink * ListenSystemBrightness(IWbemServices * pWbemServices, BrightnessChangeCallback g_pfnCallback)
{ {
HRESULT hr; HRESULT hr;
// 设置安全性 // 设置安全性

View File

@@ -21,8 +21,9 @@ BOOL APIENTRY DllMain(HMODULE hModule,
} }
extern "C" __declspec(dllexport) IMMDeviceEnumerator * InitEnumerator() extern "C" __declspec(dllexport) IMMDeviceEnumerator * InitSystemDeviceEnumerator()
{ {
CoUninitialize();
HRESULT hr; HRESULT hr;
hr = CoInitialize(nullptr); hr = CoInitialize(nullptr);
if (FAILED(hr)) if (FAILED(hr))
@@ -34,9 +35,9 @@ extern "C" __declspec(dllexport) IMMDeviceEnumerator * InitEnumerator()
hr = CoCreateInstance( hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator), __uuidof(MMDeviceEnumerator),
NULL, NULL,
CLSCTX_ALL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), __uuidof(IMMDeviceEnumerator),
(LPVOID*)&pEnumerator (void**)&pEnumerator
); );
if (FAILED(hr)) if (FAILED(hr))
{ {
@@ -45,7 +46,7 @@ extern "C" __declspec(dllexport) IMMDeviceEnumerator * InitEnumerator()
} }
return pEnumerator; return pEnumerator;
} }
extern "C" __declspec(dllexport) IMMDevice * InitDevice(IMMDeviceEnumerator * pEnumerator) extern "C" __declspec(dllexport) IMMDevice * InitSystemDevice(IMMDeviceEnumerator * pEnumerator)
{ {
if (pEnumerator == NULL) if (pEnumerator == NULL)
{ {
@@ -62,7 +63,7 @@ extern "C" __declspec(dllexport) IMMDevice * InitDevice(IMMDeviceEnumerator * pE
} }
return pDevice; return pDevice;
} }
extern "C" __declspec(dllexport) IAudioEndpointVolume * InitVolume(IMMDevice * pDevice) extern "C" __declspec(dllexport) IAudioEndpointVolume * InitSystemAudioEndpointVolume(IMMDevice * pDevice)
{ {
if (pDevice == NULL) if (pDevice == NULL)
{ {
@@ -75,7 +76,7 @@ extern "C" __declspec(dllexport) IAudioEndpointVolume * InitVolume(IMMDevice * p
__uuidof(IAudioEndpointVolume), __uuidof(IAudioEndpointVolume),
CLSCTX_ALL, CLSCTX_ALL,
NULL, NULL,
(LPVOID*)&pEndpointVolume (void**)&pEndpointVolume
); );
if (FAILED(hr)) if (FAILED(hr))
{ {
@@ -84,7 +85,7 @@ extern "C" __declspec(dllexport) IAudioEndpointVolume * InitVolume(IMMDevice * p
} }
return pEndpointVolume; return pEndpointVolume;
} }
extern "C" __declspec(dllexport) IAudioMeterInformation * InitMeterInfo(IMMDevice * pDevice) { extern "C" __declspec(dllexport) IAudioMeterInformation * InitSystemAudioMeterInformation(IMMDevice * pDevice) {
if (pDevice == NULL) if (pDevice == NULL)
{ {
@@ -96,7 +97,7 @@ extern "C" __declspec(dllexport) IAudioMeterInformation * InitMeterInfo(IMMDevic
__uuidof(IAudioMeterInformation), __uuidof(IAudioMeterInformation),
CLSCTX_ALL, CLSCTX_ALL,
nullptr, nullptr,
reinterpret_cast<void**>(&pMeterInfo)); (void**)(&pMeterInfo));
if (FAILED(hr)) if (FAILED(hr))
{ {
@@ -107,7 +108,7 @@ extern "C" __declspec(dllexport) IAudioMeterInformation * InitMeterInfo(IMMDevic
return pMeterInfo; return pMeterInfo;
} }
extern "C" __declspec(dllexport) bool FreeVolume(IMMDeviceEnumerator * pEnumerator, IMMDevice * pDevice, IAudioEndpointVolume * pEndpointVolume, IAudioMeterInformation * pMeterInfo) extern "C" __declspec(dllexport) bool FreeSystemDevice(IMMDeviceEnumerator * pEnumerator, IMMDevice * pDevice, IAudioEndpointVolume * pEndpointVolume, IAudioMeterInformation * pMeterInfo)
{ {
if (pEnumerator != NULL) if (pEnumerator != NULL)
{ {
@@ -130,33 +131,32 @@ extern "C" __declspec(dllexport) bool FreeVolume(IMMDeviceEnumerator * pEnumerat
CoUninitialize(); CoUninitialize();
return true; return true;
} }
extern "C" __declspec(dllexport) float GetSystemVolume(IAudioEndpointVolume * pEndpointVolume) extern "C" __declspec(dllexport) float GetSystemVolume(IAudioEndpointVolume * pEndpointVolume)
{ {
if (pEndpointVolume == NULL) if (pEndpointVolume == NULL)
{ {
return 1; return 0;
} }
float volume = 0.0f; float volume = 0.0f;
HRESULT hr = pEndpointVolume->GetMasterVolumeLevelScalar(&volume); HRESULT hr = pEndpointVolume->GetMasterVolumeLevelScalar(&volume);
if (FAILED(hr)) if (FAILED(hr))
{ {
return 2; return 0;
} }
return 3; return volume;
} }
extern "C" __declspec(dllexport) int SetSystemVolume(IAudioEndpointVolume * pEndpointVolume, float volume) extern "C" __declspec(dllexport) bool SetSystemVolume(IAudioEndpointVolume * pEndpointVolume, float volume)
{ {
if (pEndpointVolume == NULL) if (pEndpointVolume == NULL)
{ {
return false; return FALSE;
} }
HRESULT hr = pEndpointVolume->SetMasterVolumeLevelScalar(volume, NULL); HRESULT hr = pEndpointVolume->SetMasterVolumeLevelScalar(volume, NULL);
if (FAILED(hr)) if (FAILED(hr))
{ {
return false; return FALSE;
} }
return true; return true;
@@ -182,14 +182,14 @@ extern "C" __declspec(dllexport) bool GetSystemMute(IAudioEndpointVolume * pEndp
{ {
if (pEndpointVolume == NULL) if (pEndpointVolume == NULL)
{ {
return 0; return FALSE;
} }
BOOL mute; BOOL mute;
HRESULT hr = pEndpointVolume->GetMute(&mute); HRESULT hr = pEndpointVolume->GetMute(&mute);
if (FAILED(hr)) if (FAILED(hr))
{ {
return mute; return FALSE;
} }
return mute; return mute;
} }
@@ -197,13 +197,13 @@ extern "C" __declspec(dllexport) bool SetSystemMute(IAudioEndpointVolume * pEndp
{ {
if (pEndpointVolume == NULL) if (pEndpointVolume == NULL)
{ {
return 0; return FALSE;
} }
HRESULT hr = pEndpointVolume->SetMute(mute, NULL); HRESULT hr = pEndpointVolume->SetMute(mute, NULL);
if (FAILED(hr)) if (FAILED(hr))
{ {
return false; return FALSE;
} }
return true; return true;
} }

View File

@@ -0,0 +1 @@
x64\Debug\\_IsIncrementalBuild

View File

@@ -0,0 +1,17 @@
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.pch
d:\desktop\cmonitor\cmonitor.volume\x64\debug\vc143.pdb
d:\desktop\cmonitor\cmonitor.volume\x64\debug\vc143.idb
d:\desktop\cmonitor\cmonitor.volume\x64\debug\pch.obj
d:\desktop\cmonitor\cmonitor.volume\x64\debug\dllmain.obj
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.ilk
d:\desktop\cmonitor\x64\debug\cmonitor.volume.dll
d:\desktop\cmonitor\x64\debug\cmonitor.volume.pdb
d:\desktop\cmonitor\x64\debug\cmonitor.volume.lib
d:\desktop\cmonitor\x64\debug\cmonitor.volume.exp
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\cl.command.1.tlog
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\cl.read.1.tlog
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\cl.write.1.tlog
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\link.command.1.tlog
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\link.read.1.tlog
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\link.write.1.tlog
d:\desktop\cmonitor\cmonitor.volume\x64\debug\cmonitor.volume.tlog\link.write.2u.tlog

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ProjectOutputs>
<ProjectOutput>
<FullPath>D:\desktop\cmonitor\x64\Debug\cmonitor.volume.dll</FullPath>
</ProjectOutput>
</ProjectOutputs>
<ContentFiles />
<SatelliteDlls />
<NonRecipeFileRefs />
</Project>

Binary file not shown.

View File

@@ -0,0 +1,4 @@
 pch.cpp
dllmain.cpp
正在创建库 D:\desktop\cmonitor\x64\Debug\cmonitor.volume.lib 和对象 D:\desktop\cmonitor\x64\Debug\cmonitor.volume.exp
cmonitor.volume.vcxproj -> D:\desktop\cmonitor\x64\Debug\cmonitor.volume.dll

Binary file not shown.

View File

@@ -0,0 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.34.31933:TargetPlatformVersion=10.0.22621.0:
Debug|x64|D:\desktop\cmonitor\|

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -29,6 +29,7 @@ using cmonitor.server.service.messengers.light;
using System.Reflection; using System.Reflection;
using cmonitor.server.client.reports.share; using cmonitor.server.client.reports.share;
using cmonitor.server.service.messengers.share; using cmonitor.server.service.messengers.share;
using cmonitor.server.client.reports.system;
namespace cmonitor namespace cmonitor
{ {
@@ -138,7 +139,7 @@ namespace cmonitor
serviceCollection.AddSingleton<WallpaperReport>(); serviceCollection.AddSingleton<WallpaperReport>();
serviceCollection.AddSingleton<LightReport>(); serviceCollection.AddSingleton<LightReport>();
serviceCollection.AddSingleton<ShareReport>(); serviceCollection.AddSingleton<ShareReport>();
serviceCollection.AddSingleton<SystemReport>();
//服务 //服务
serviceCollection.AddSingleton<TcpServer>(); serviceCollection.AddSingleton<TcpServer>();

View File

@@ -65,8 +65,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MemoryPack" Version="1.9.16" /> <PackageReference Include="MemoryPack" Version="1.9.16" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="NAudio.Wasapi" Version="2.2.1" />
<PackageReference Include="System.Management" Version="7.0.2" /> <PackageReference Include="System.Management" Version="7.0.2" />
<TrimmerRootAssembly Include="System.Management" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Binary file not shown.

View File

@@ -29,6 +29,7 @@ namespace cmonitor.server.api.services
&& cache.Connected && cache.Connected
&& cache.GetReport(config.ReportDelay) && cache.GetReport(config.ReportDelay)
&& Interlocked.CompareExchange(ref cache.ReportFlag, 0, 1) == 1; && Interlocked.CompareExchange(ref cache.ReportFlag, 0, 1) == 1;
if (res) if (res)
{ {
cache.UpdateReport(); cache.UpdateReport();

View File

@@ -5,7 +5,6 @@ using MemoryPack;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using common.libs.extends; using common.libs.extends;
using System.Reflection; using System.Reflection;
using System.Diagnostics;
namespace cmonitor.server.client.reports namespace cmonitor.server.client.reports
{ {
@@ -50,11 +49,13 @@ namespace cmonitor.server.client.reports
reports = types.Select(c => (IReport)serviceProvider.GetService(c)).Where(c => string.IsNullOrWhiteSpace(c.Name) == false).ToList(); reports = types.Select(c => (IReport)serviceProvider.GetService(c)).Where(c => string.IsNullOrWhiteSpace(c.Name) == false).ToList();
reportObj = new Dictionary<string, object>(reports.Count); reportObj = new Dictionary<string, object>(reports.Count);
} }
private uint reportFlag = 0; private uint reportFlag = 0;
public void Update() public void Update()
{ {
Interlocked.CompareExchange(ref reportFlag, 1, 0); Interlocked.CompareExchange(ref reportFlag, 1, 0);
} }
private void ReportTask() private void ReportTask()
{ {
Task.Factory.StartNew(async () => Task.Factory.StartNew(async () =>

View File

@@ -24,6 +24,9 @@ namespace cmonitor.server.client.reports.active
{ {
Timers(); Timers();
DisallowInit(); DisallowInit();
AppDomain.CurrentDomain.ProcessExit += (s, e) => DisallowRun(Array.Empty<string>());
Console.CancelKeyPress += (s, e) => DisallowRun(Array.Empty<string>());
} }
} }

View File

@@ -0,0 +1,20 @@
namespace cmonitor.server.client.reports.system
{
public sealed class SystemReport : IReport
{
public string Name => "System";
public SystemReport(Config config)
{
}
public object GetReports()
{
return null;
}
}
public sealed class SystemReportInfo
{
public bool Value { get; set; }
}
}

View File

@@ -0,0 +1,80 @@
using System.Runtime.InteropServices;
namespace cmonitor.server.client.reports.system
{
public static class CPUHelper
{
public static CPUTime GetCPUTime()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return WindowsCPU.GetCPUTime();
return new CPUTime();
}
public static double CalculateCPULoad(CPUTime oldTime, CPUTime newTime)
{
ulong totalTicksSinceLastTime = newTime.SystemTime - oldTime.SystemTime;
ulong idleTicksSinceLastTime = newTime.IdleTime - oldTime.IdleTime;
double ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((double)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);
return Math.Round(ret * 100, 2);
}
}
public partial class WindowsCPU
{
#if NET7_0_OR_GREATER
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
#else
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
#endif
public static CPUTime GetCPUTime(FILETIME lpIdleTime, FILETIME lpKernelTime, FILETIME lpUserTime)
{
var IdleTime = ((ulong)lpIdleTime.DateTimeHigh << 32) | lpIdleTime.DateTimeLow;
var KernelTime = ((ulong)lpKernelTime.DateTimeHigh << 32) | lpKernelTime.DateTimeLow;
var UserTime = ((ulong)lpUserTime.DateTimeHigh << 32) | lpUserTime.DateTimeLow;
var SystemTime = KernelTime + UserTime;
return new CPUTime(IdleTime, SystemTime);
}
public static CPUTime GetCPUTime()
{
FILETIME lpIdleTime = default;
FILETIME lpKernelTime = default;
FILETIME lpUserTime = default;
if (!GetSystemTimes(out lpIdleTime, out lpKernelTime, out lpUserTime))
{
return default;
}
return GetCPUTime(lpIdleTime, lpKernelTime, lpUserTime);
}
}
public struct CPUTime
{
public CPUTime(ulong idleTime, ulong systemTime)
{
IdleTime = idleTime;
SystemTime = systemTime;
}
public ulong IdleTime { get; private set; }
public ulong SystemTime { get; private set; }
}
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME
{
public uint DateTimeLow;
public uint DateTimeHigh;
}
}

View File

@@ -0,0 +1,20 @@
namespace cmonitor.server.client.reports.system
{
public static class WindowsDrive
{
public static List<string> GetAllDrives()
{
List<string> drives = new List<string>();
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
{
if (driveInfo.IsReady && driveInfo.DriveType == DriveType.Fixed)
{
drives.Add(driveInfo.Name);
}
}
return drives;
}
}
}

View File

@@ -0,0 +1,39 @@
using System.Runtime.InteropServices;
namespace cmonitor.server.client.reports.system
{
public static class WindowsMemory
{
private static double GetMemoryUsage()
{
MEMORYSTATUSEX memoryStatus = new MEMORYSTATUSEX();
memoryStatus.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
if (GlobalMemoryStatusEx(ref memoryStatus) == false)
{
return 0;
}
return Math.Round((float)(memoryStatus.ullTotalPhys - memoryStatus.ullAvailPhys) / memoryStatus.ullTotalPhys * 100f, 2);
}
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
[StructLayout(LayoutKind.Sequential)]
public struct 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;
}
}
}

View File

@@ -9,11 +9,15 @@ namespace cmonitor.server.client.reports.llock
{ {
public string Name => "Usb"; public string Name => "Usb";
private UsbReportInfo report = new UsbReportInfo(); private UsbReportInfo report = new UsbReportInfo();
public UsbReport(Config config)
public UsbReport() {
if (config.IsCLient)
{ {
UnLockUsb(); UnLockUsb();
report.Value = GetHasUSBDisabled(); report.Value = GetHasUSBDisabled();
AppDomain.CurrentDomain.ProcessExit += (s, e) => UnLockUsb();
Console.CancelKeyPress += (s, e) => UnLockUsb();
}
} }
public object GetReports() public object GetReports()

View File

@@ -1,94 +1,70 @@
using common.libs; using common.libs;
#if DEBUG || RELEASE using System.Runtime.InteropServices;
using NAudio.CoreAudioApi;
#endif
namespace cmonitor.server.client.reports.volume namespace cmonitor.server.client.reports.volume
{ {
public sealed class VolumeReport : IReport public sealed class VolumeReport : IReport, IDisposable
{ {
public string Name => "Volume"; public string Name => "Volume";
private VolumeReportInfo report = new VolumeReportInfo(); private VolumeReportInfo report = new VolumeReportInfo();
private IntPtr pEnumerator = IntPtr.Zero;
private IntPtr pDevice = IntPtr.Zero;
private IntPtr pEndpointVolume = IntPtr.Zero;
private IntPtr pMeterInfo = IntPtr.Zero;
public VolumeReport() public VolumeReport()
{ {
Volume(); Init();
} }
public object GetReports() public object GetReports()
{
if (OperatingSystem.IsWindows())
{ {
report.Value = GetVolume(); report.Value = GetVolume();
report.Mute = GetVolumeMute(); report.Mute = GetVolumeMute();
report.MasterPeak = GetMasterPeakValue(); report.MasterPeak = GetMasterPeakValue();
}
return report; return report;
} }
#if DEBUG || RELEASE
MMDeviceEnumerator enumerator;
MMDevice device;
AudioEndpointVolume volumeObject;
#endif
private void Volume()
{
if (OperatingSystem.IsWindows())
{
try
{
#if DEBUG || RELEASE
enumerator = new MMDeviceEnumerator();
device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
volumeObject = device.AudioEndpointVolume;
#endif
}
catch (Exception ex)
{
Logger.Instance.Error(ex);
}
}
}
private float GetVolume() private float GetVolume()
{ {
try try
{ {
#if DEBUG || RELEASE if (pEndpointVolume != IntPtr.Zero)
if (volumeObject != null)
{ {
return volumeObject.MasterVolumeLevelScalar * 100; return GetSystemVolume(pEndpointVolume) * 100;
} }
#endif
} }
catch (Exception) catch (Exception)
{ {
} }
return -1; return 0;
} }
private float GetMasterPeakValue() private float GetMasterPeakValue()
{ {
try try
{ {
#if DEBUG || RELEASE if (pMeterInfo != IntPtr.Zero)
if (device != null)
{ {
return device.AudioMeterInformation.MasterPeakValue * 100; return GetSystemMasterPeak(pMeterInfo) * 100;
} }
#endif
} }
catch (Exception) catch (Exception)
{ {
} }
return -1; return 0;
} }
private bool GetVolumeMute() private bool GetVolumeMute()
{ {
try try
{ {
#if DEBUG || RELEASE if (pEndpointVolume != IntPtr.Zero)
if (volumeObject != null)
{ {
return volumeObject.Mute; return GetSystemMute(pEndpointVolume);
} }
#endif
} }
catch (Exception) catch (Exception)
{ {
@@ -99,13 +75,11 @@ namespace cmonitor.server.client.reports.volume
{ {
try try
{ {
#if DEBUG || RELEASE
volume = Math.Max(0, Math.Min(1, volume)); volume = Math.Max(0, Math.Min(1, volume));
if (volumeObject != null) if (pEndpointVolume != IntPtr.Zero)
{ {
volumeObject.MasterVolumeLevelScalar = volume; SetSystemVolume(pEndpointVolume, volume);
} }
#endif
} }
catch (Exception) catch (Exception)
{ {
@@ -115,17 +89,95 @@ namespace cmonitor.server.client.reports.volume
{ {
try try
{ {
#if DEBUG || RELEASE if (pEndpointVolume != IntPtr.Zero)
if (volumeObject != null)
{ {
volumeObject.Mute = mute; SetSystemMute(pEndpointVolume, mute);
} }
#endif
} }
catch (Exception) catch (Exception)
{ {
} }
} }
private void Init()
{
if (OperatingSystem.IsWindows())
{
try
{
pEnumerator = InitSystemDeviceEnumerator();
if (pEnumerator != IntPtr.Zero)
{
pDevice = InitSystemDevice(pEnumerator);
if (pDevice != IntPtr.Zero)
{
pEndpointVolume = InitSystemAudioEndpointVolume(pDevice);
pMeterInfo = InitSystemAudioMeterInformation(pDevice);
}
}
FreeDevice();
}
catch (Exception ex)
{
Logger.Instance.Error(ex);
}
}
}
private void FreeVolume()
{
FreeSystemDevice(pEnumerator, pDevice, pEndpointVolume, pMeterInfo);
pEnumerator = IntPtr.Zero;
pDevice = IntPtr.Zero;
pEndpointVolume = IntPtr.Zero;
pMeterInfo = IntPtr.Zero;
}
private void FreeDevice()
{
FreeSystemDevice(pEnumerator, pDevice, IntPtr.Zero, IntPtr.Zero);
pEnumerator = IntPtr.Zero;
pDevice = IntPtr.Zero;
}
~VolumeReport()
{
FreeVolume();
pEnumerator = IntPtr.Zero;
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing && pEnumerator != IntPtr.Zero)
{
FreeVolume();
}
}
[DllImport("cmonitor.volume.dll")]
public static extern IntPtr InitSystemDeviceEnumerator();
[DllImport("cmonitor.volume.dll")]
public static extern IntPtr InitSystemDevice(IntPtr pEnumerator);
[DllImport("cmonitor.volume.dll")]
public static extern IntPtr InitSystemAudioEndpointVolume(IntPtr pDevice);
[DllImport("cmonitor.volume.dll")]
public static extern IntPtr InitSystemAudioMeterInformation(IntPtr pDevice);
[DllImport("cmonitor.volume.dll")]
public static extern bool FreeSystemDevice(IntPtr pEnumerator, IntPtr pDevice, IntPtr pEndpointVolume, IntPtr pMeterInfo);
[DllImport("cmonitor.volume.dll")]
public static extern float GetSystemVolume(IntPtr pEndpointVolume);
[DllImport("cmonitor.volume.dll")]
public static extern bool SetSystemVolume(IntPtr pEndpointVolume, float volume);
[DllImport("cmonitor.volume.dll")]
public static extern float GetSystemMasterPeak(IntPtr pMeterInfo);
[DllImport("cmonitor.volume.dll")]
public static extern bool GetSystemMute(IntPtr pEndpointVolume);
[DllImport("cmonitor.volume.dll")]
public static extern bool SetSystemMute(IntPtr pEndpointVolume, bool mute);
} }
public sealed class VolumeReportInfo public sealed class VolumeReportInfo

View File

@@ -43,7 +43,6 @@ namespace cmonitor.server.service
MessengerIdAttribute mid = method.GetCustomAttribute(midType) as MessengerIdAttribute; MessengerIdAttribute mid = method.GetCustomAttribute(midType) as MessengerIdAttribute;
if (mid != null) if (mid != null)
{ {
if (messengers.ContainsKey(mid.Id) == false) if (messengers.ContainsKey(mid.Id) == false)
{ {
MessengerCacheInfo cache = new MessengerCacheInfo MessengerCacheInfo cache = new MessengerCacheInfo
@@ -85,6 +84,7 @@ namespace cmonitor.server.service
MessageRequestWrap requestWrap = connection.ReceiveRequestWrap; MessageRequestWrap requestWrap = connection.ReceiveRequestWrap;
try try
{ {
//回复的消息 //回复的消息
if ((MessageTypes)(readReceive.Span[0] & 0b0000_1111) == MessageTypes.RESPONSE) if ((MessageTypes)(readReceive.Span[0] & 0b0000_1111) == MessageTypes.RESPONSE)
{ {

View File

@@ -4,7 +4,7 @@ rd /s /q public\\publish
rd /s /q public\\publish-zip rd /s /q public\\publish-zip
mkdir public\\publish-zip mkdir public\\publish-zip
dotnet publish ./cmonitor -c release -f net7.0 -o ./public/publish/win-x64 -r win-x64 -p:TrimMode=partial --self-contained true -p:TieredPGO=true -p:DebugType=none -p:DebugSymbols=false -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true -p:DebuggerSupport=false -p:EnableUnsafeBinaryFormatterSerialization=false -p:EnableUnsafeUTF7Encoding=false -p:HttpActivityPropagationSupport=false -p:InvariantGlobalization=true -p:MetadataUpdaterSupport=false -p:UseSystemResourceKeys=true dotnet publish ./cmonitor -c release -f net7.0 -o ./public/publish/win-x64 -r win-x64 -p:PublishTrimmed=true -p:TrimMode=partial --self-contained true -p:TieredPGO=true -p:DebugType=none -p:DebugSymbols=false -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true -p:DebuggerSupport=false -p:EnableUnsafeBinaryFormatterSerialization=false -p:EnableUnsafeUTF7Encoding=false -p:HttpActivityPropagationSupport=false -p:InvariantGlobalization=true -p:MetadataUpdaterSupport=false -p:UseSystemResourceKeys=true
dotnet publish ./cmonitor -c releaselinux -f net7.0 -o ./public/publish/linux-x64 -r linux-x64 -p:PublishTrimmed=true --self-contained true -p:TieredPGO=true -p:DebugType=none -p:DebugSymbols=false -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true -p:DebuggerSupport=false -p:EnableUnsafeBinaryFormatterSerialization=false -p:EnableUnsafeUTF7Encoding=false -p:HttpActivityPropagationSupport=false -p:InvariantGlobalization=true -p:MetadataUpdaterSupport=false -p:UseSystemResourceKeys=true -p:TrimMode=partial dotnet publish ./cmonitor -c releaselinux -f net7.0 -o ./public/publish/linux-x64 -r linux-x64 -p:PublishTrimmed=true --self-contained true -p:TieredPGO=true -p:DebugType=none -p:DebugSymbols=false -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true -p:DebuggerSupport=false -p:EnableUnsafeBinaryFormatterSerialization=false -p:EnableUnsafeUTF7Encoding=false -p:HttpActivityPropagationSupport=false -p:InvariantGlobalization=true -p:MetadataUpdaterSupport=false -p:UseSystemResourceKeys=true -p:TrimMode=partial