Skip to content

Instantly share code, notes, and snippets.

@ouroborus
Created May 6, 2017 21:39
Show Gist options
  • Save ouroborus/627fe8d8a2af6c289c60ac434691a4a3 to your computer and use it in GitHub Desktop.
Save ouroborus/627fe8d8a2af6c289c60ac434691a4a3 to your computer and use it in GitHub Desktop.
Restart LCore.exe when it's using to much memory
# LCore.exe (Logitech's tool for configuring and customizing their devices) has a pretty bad memory leak.
# This script kills LCore when it starts using more that 256MB, cleans up the task tray, then restarts LCore.
# It uses the same command line used to initially start LCore.
# Set LCore to start when Windows starts.
# (If you need to start it manually, include the /minimized argument: LCore.exe /minimized )
# Save this file to %USERPROFILE%\command\reset-LCore.ps1
# Create a Task Scheduler item with triggers for:
# every hour after logon, and
# every hour after created or modified.
# Set action to:
# Program: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
# Arguments: -WindowStyle Hidden -ExecutionPolicy Bypass -File %USERPROFILE%\command\reset-LCore.ps1
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System;
using System.Runtime.InteropServices;
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
public class pInvoke {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
public static void RefreshTrayArea() {
IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null);
IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null);
IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null);
IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area");
if (notificationAreaHandle == IntPtr.Zero) {
notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area");
IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null);
IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area");
RefreshTrayArea(overflowNotificationAreaHandle);
}
RefreshTrayArea(notificationAreaHandle);
}
private static void RefreshTrayArea(IntPtr windowHandle) {
const uint wmMousemove = 0x0200;
RECT rect;
GetClientRect(windowHandle, out rect);
for (var x = 0; x < rect.right; x += 5)
for (var y = 0; y < rect.bottom; y += 5)
SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x);
}
}
"@
$processName = "LCore"
$process = $(Get-Process $processName -ErrorAction SilentlyContinue)[0]
If ($process -And $process.PrivateMemorySize64 -ge 268435456) {
$processId = $process.Id
$cmd = (Get-WmiObject Win32_Process -Filter "ProcessId = '$processId'").CommandLine
Stop-Process -Id $processId
Wait-Process -Id $processId
[pInvoke]::RefreshTrayArea()
Invoke-Expression "& $cmd"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment