Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AppControl Manager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@ internal void RefreshLocalizedContent()
AutomationProperties.SetHelpText(EXIFManagerNavigationViewItem, GlobalVars.GetStr("EXIFManagerNavigationViewItem/AutomationProperties/HelpText"));
ToolTipService.SetToolTip(EXIFManagerNavigationViewItem, GlobalVars.GetStr("EXIFManagerNavigationViewItem/ToolTipService/ToolTip"));

ServiceManagerNavItem.Content = GlobalVars.GetStr("ServiceManagerNavItem/Content");
AutomationProperties.SetHelpText(ServiceManagerNavItem, GlobalVars.GetStr("ServiceManagerNavItem/AutomationProperties/HelpText"));
ToolTipService.SetToolTip(ServiceManagerNavItem, GlobalVars.GetStr("ServiceManagerNavItem/ToolTipService/ToolTip"));

#endif

#if APP_CONTROL_MANAGER
Expand Down
67 changes: 25 additions & 42 deletions AppControl Manager/Others/GetExtendedFileAttrib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ internal static partial class GetExtendedFileAttrib
/// operations.
/// https://learn.microsoft.com/windows/win32/api/winver/nf-winver-getfileversioninfoexw#parameters
/// </summary>
private const int FILE_VER_GET_NEUTRAL = 2;

/// <summary>
/// Represents an error code indicating that a specified resource type could not be found. The value is 1813 (-2147023083).
/// </summary>
private const int ERROR_RESOURCE_TYPE_NOT_FOUND = 1813;
private const uint FILE_VER_GET_NEUTRAL = 2;

/// <summary>
/// Retrieves extended file information, including version, original file name, internal name, file description, and
Expand Down Expand Up @@ -77,16 +72,15 @@ internal static ExFileInfo Get(string filePath)
Span<byte> spanData = new(versionData);

// Extract version from the version data
if (!TryGetVersion(spanData, out Version? version, out int versionErrorCode))
if (!TryGetVersion(spanData, out Version? version))
{
// Throw Win32Exception using captured error code
throw new Win32Exception(versionErrorCode);
return BadCaseReturnVal;
}

// Extract locale and encoding information
if (!TryGetLocaleAndEncoding(spanData, out string? locale, out string? encoding, out int localeErrorCode))
if (!TryGetLocaleAndEncoding(spanData, out string? locale, out string? encoding))
{
throw new Win32Exception(localeErrorCode);
return BadCaseReturnVal;
}

// Retrieve various file information based on locale and encoding and return the result
Expand All @@ -112,29 +106,24 @@ internal static ExFileInfo Get(string filePath)
}
}


/// <summary>
/// Extracts version information from a byte array and outputs it as a Version object.
/// </summary>
/// <param name="data">The byte array containing version information to be extracted.</param>
/// <param name="version">Outputs the extracted version information as a Version object.</param>
/// <param name="win32Error">Outputs the Win32 error code when extraction fails.</param>
/// <returns>Returns true if the version was successfully extracted, otherwise false.</returns>
private static unsafe bool TryGetVersion(Span<byte> data, out Version? version, out int win32Error)
private static unsafe bool TryGetVersion(Span<byte> data, out Version? version)
{
version = null;
win32Error = 0;

// pin span directly
fixed (byte* pData = data)
{
IntPtr basePtr = (IntPtr)pData;

// Query the root block for version info
if (!NativeMethods.VerQueryValueW(basePtr, "\\", out nint buffer, out _))
// Query the root block for version info and make sure the returned length is large enough for the struct
if (NativeMethods.VerQueryValueW(basePtr, "\\", out nint buffer, out uint len) == 0 || buffer == IntPtr.Zero || len < sizeof(VS_FIXEDFILEINFO))
{
// Capture Win32 error immediately after failure.
win32Error = Marshal.GetLastPInvokeError();
return false;
}

Expand All @@ -152,40 +141,35 @@ private static unsafe bool TryGetVersion(Span<byte> data, out Version? version,
}
}


/// <summary>
/// Extracts locale and encoding information from a byte span.
/// </summary>
/// <param name="data">The byte span containing data from which locale and encoding are extracted.</param>
/// <param name="locale">Outputs the locale information derived from the data.</param>
/// <param name="encoding">Outputs the encoding information derived from the data.</param>
/// <param name="win32Error">Outputs the Win32 error code when extraction fails.</param>
/// <returns>Returns a boolean indicating the success of the extraction process.</returns>
private static unsafe bool TryGetLocaleAndEncoding(Span<byte> data, out string? locale, out string? encoding, out int win32Error)
private static unsafe bool TryGetLocaleAndEncoding(Span<byte> data, out string? locale, out string? encoding)
{
locale = null;
encoding = null;
win32Error = 0;

// pin span instead of allocating a new array each call.
fixed (byte* pData = data)
{
IntPtr basePtr = (IntPtr)pData;

// Query the translation block for locale and encoding
if (!NativeMethods.VerQueryValueW(basePtr, "\\VarFileInfo\\Translation", out nint buffer, out _))
// Query the translation block for locale and encoding, verifying length is at least 4 bytes (2 WORDs)
if (NativeMethods.VerQueryValueW(basePtr, "\\VarFileInfo\\Translation", out nint buffer, out uint len) == 0 || buffer == IntPtr.Zero || len < 4)
{
win32Error = Marshal.GetLastPInvokeError();
return false;
}

// Copy the translation values (two WORDs)
short[] translations = new short[2];
Marshal.Copy(buffer, translations, 0, 2);
// Cast directly to ushort* (WORD)
ushort* pTranslations = (ushort*)buffer;

// Convert the translation values to hex strings
locale = translations[0].ToString("X4", CultureInfo.InvariantCulture);
encoding = translations[1].ToString("X4", CultureInfo.InvariantCulture);
locale = pTranslations[0].ToString("X4", CultureInfo.InvariantCulture);
encoding = pTranslations[1].ToString("X4", CultureInfo.InvariantCulture);
return true;
}
}
Expand All @@ -200,7 +184,7 @@ private static unsafe bool TryGetLocaleAndEncoding(Span<byte> data, out string?
/// <returns>Returns the localized resource string or null if not found.</returns>
private static unsafe string? GetLocalizedResource(Span<byte> versionBlock, string encoding, string locale, string resource)
{
string[] encodings = [encoding, Cp1252FallbackCode, UnicodeFallbackCode];
ReadOnlySpan<string> encodings = [encoding, Cp1252FallbackCode, UnicodeFallbackCode];

// pin once, reuse base pointer across attempts.
fixed (byte* pData = versionBlock)
Expand All @@ -209,23 +193,22 @@ private static unsafe bool TryGetLocaleAndEncoding(Span<byte> data, out string?

foreach (string enc in encodings)
{
string subBlock = $"StringFileInfo\\{locale}{enc}{resource}";

if (NativeMethods.VerQueryValueW(basePtr, subBlock, out nint buffer, out _))
return Marshal.PtrToStringAuto(buffer);
string subBlock = $"\\StringFileInfo\\{locale}{enc}{resource}";

// Capture the VerQueryValueW's error immediately if it failed above.
int lastError = Marshal.GetLastPInvokeError();
// Grab the length to safely instantiate the string
if (NativeMethods.VerQueryValueW(basePtr, subBlock, out nint buffer, out uint len) != 0 && buffer != IntPtr.Zero && len > 0)
{
ReadOnlySpan<char> valueSpan = new((void*)buffer, (int)len);
valueSpan = valueSpan.TrimEnd('\0');

// If error is not resource type not found, throw the error
if (lastError != ERROR_RESOURCE_TYPE_NOT_FOUND)
throw new Win32Exception(lastError);
// If the resulting string is empty, return the interned string.Empty to avoid allocating a new object on the heap
return valueSpan.IsEmpty ? string.Empty : new string(valueSpan);
}
}
}
return null;
}


/// <summary>
/// Check if a string is null or whitespace and return null if it is
/// </summary>
Expand Down
22 changes: 14 additions & 8 deletions AppControl Manager/Others/ListViewHelper-Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,25 @@ internal static double MeasureText(string? text, double maxWidth)
private const double InitValueAdded = 20;

/// <summary>
/// Select all of the items in the ListView's ItemsSource
/// Select all of the items in the ListView's ItemsSource.
/// </summary>
/// <param name="lw"></param>
/// <param name="source"></param>
internal static void SelectAll(ListView lw, IList source)
internal static void SelectAll(ListView? lw)
{
// Clear existing selections from the List View
lw.SelectedItems.Clear();
if (lw is null) return;

foreach (var item in source)
switch (lw.SelectionMode)
{
// Select each item
lw.SelectedItems.Add(item);
case ListViewSelectionMode.Multiple:
case ListViewSelectionMode.Extended:
lw.SelectRange(new(0, (uint)lw.Items.Count));
break;
case ListViewSelectionMode.Single:
lw.SelectedItem = lw.Items.FirstOrDefault();
break;
case ListViewSelectionMode.None:
default:
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ internal sealed partial class ListViewIncrementalController(
/// <summary>
/// Selects all of the displayed rows on the ListView
/// </summary>
internal void SelectAll_Click()
{
if (ListViewRef is null || ObservableSource is null) return;
ListViewHelper.SelectAll(ListViewRef, ObservableSource);
}
internal void SelectAll_Click() => ListViewHelper.SelectAll(ListViewRef);

/// <summary>
/// Event handler for the Clear Data button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1606,9 +1606,7 @@ internal void StrictKernel_ClearDataButton_Click()
internal void StrictKernel_SelectAll_Click()
{
ListView? lv = ListViewHelper.GetListViewFromCache(ListViewHelper.ListViewsRegistry.SupplementalPolicy_StrictKernelMode_ScanResults);
if (lv is null) return;

ListViewHelper.SelectAll(lv, StrictKernelModeScanResults);
ListViewHelper.SelectAll(lv);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ internal void ClearDataButton_Click()
internal void SelectAll_Click()
{
ListView? lv = ListViewHelper.GetListViewFromCache(ListViewHelper.ListViewsRegistry.Event_Logs);
if (lv is null) return;

ListViewHelper.SelectAll(lv, FileIdentities);
ListViewHelper.SelectAll(lv);
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions AppControl Manager/ViewModels/FirewallSentinelVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,8 +1619,7 @@ internal void ClearBlockedPacketsLogs_Click()
internal void BlockedPacketsListView_SelectAll_Click()
{
ListView? lv = ListViewHelper.GetListViewFromCache(ListViewHelper.ListViewsRegistry.FirewallBlockedLogs);
if (lv is null) return;
ListViewHelper.SelectAll(lv, BlockedPackets);
ListViewHelper.SelectAll(lv);
}

internal void BlockedPacketsListView_DeSelectAll_Click()
Expand Down
9 changes: 2 additions & 7 deletions AppControl Manager/ViewModels/IntuneDeploymentDetailsVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal IntuneDeploymentDetailsVM()
() => MainInfoBarIsClosable, value => MainInfoBarIsClosable = value,
Dispatcher, null, null);

CalculateColumnWidths();
_ = Dispatcher.TryEnqueue(CalculateColumnWidths);
}

internal readonly InfoBarSettings MainInfoBar;
Expand Down Expand Up @@ -436,12 +436,7 @@ internal void CtrlC_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvok
internal void SelectAll_Click()
{
ListView? lv = ListViewHelper.GetListViewFromCache(ListViewHelper.ListViewsRegistry.Deployment_IntuneGroupsListView);
if (lv is null)
{
return;
}

ListViewHelper.SelectAll(lv, GroupNamesCollection);
ListViewHelper.SelectAll(lv);
}

/// <summary>
Expand Down
8 changes: 2 additions & 6 deletions AppControl Manager/ViewModels/MDEAHPolicyCreationVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,7 @@ private void ApplyFilters() => ListViewHelper.ApplyFilters(
internal void SelectAll_Click()
{
ListView? lv = ListViewHelper.GetListViewFromCache(ListViewHelper.ListViewsRegistry.MDE_AdvancedHunting);
if (lv is null) return;

ListViewHelper.SelectAll(lv, FileIdentities);
ListViewHelper.SelectAll(lv);
}

/// <summary>
Expand All @@ -449,9 +447,7 @@ internal void SelectAll_Click()
internal void DeSelectAll_Click()
{
ListView? lv = ListViewHelper.GetListViewFromCache(ListViewHelper.ListViewsRegistry.MDE_AdvancedHunting);
if (lv is null) return;

lv.SelectedItems.Clear(); // Deselect all rows by clearing SelectedItems
lv?.SelectedItems.Clear(); // Deselect all rows by clearing SelectedItems
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Power\PowerPlan.cs" />
<Compile Include="$(MSBuildThisFileDirectory)QuantumRelay\BinaryProtocol.cs" />
<Compile Include="$(MSBuildThisFileDirectory)QuantumRelay\Helpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ServiceManagement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ThermalMonitors\StorageTemperature.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ThermalMonitors\TemperatureSampler.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace CommonCore.GroupPolicy;
/// <summary>
/// Specifies the intent or purpose of the device.
/// This enumeration is used to apply policies and configurations based on usage intent.
/// It is IMPORTANT for the order of this to remain static and if changed, the values in the IntentCircles's class must also change, as well as those in the JSON files etc.
/// </summary>
internal enum Intent : int
{
Expand All @@ -35,10 +36,6 @@ internal enum Intent : int
/// <summary>
/// Defines an intent item with associated metadata to display it in the UI.
/// </summary>
/// <param name="intent"></param>
/// <param name="title"></param>
/// <param name="description"></param>
/// <param name="image"></param>
internal sealed class IntentItem(Intent intent, string title, string description, Uri image)
{
internal Intent Intent => intent;
Expand Down
Loading
Loading