LD-Uninstaller/src-csharp/LDUninstaller.Core/Native/Win32.cs

371 lines
14 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace LDUninstaller.Core.Native
{
public static class Win32
{
// --- MSI API ---
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
public static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
public static extern int MsiGetProductInfo(string szProduct, string szProperty, StringBuilder lpValueBuf, ref int pcchValueBuf);
// --- Authenticode & WinTrust ---
[DllImport("wintrust.dll", ExactSpelling = true, SetLastError = true)]
public static extern uint WinVerifyTrust(IntPtr hwnd, [MarshalAs(UnmanagedType.LPStruct)] Guid pgActionID, WinTrustData pWVTData);
private static readonly Guid WINTRUST_ACTION_GENERIC_VERIFY_V2 = new("00AAC56B-CD44-11d0-8CC2-00C04FC295EE");
public static bool VerifyEmbeddedSignature(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
return false;
try
{
using var wtd = new WinTrustFileInfo(filePath);
using var data = new WinTrustData(wtd.Handle);
uint result = WinVerifyTrust(new IntPtr(-1), WINTRUST_ACTION_GENERIC_VERIFY_V2, data);
return result == 0; // 0 == ERROR_SUCCESS
}
catch
{
return false;
}
}
// --- System Restore API (srclient.dll) (B11) ---
[DllImport("srclient.dll", CharSet = CharSet.Unicode)]
public static extern bool SRSetRestorePointW(ref RESTOREPOINTINFO pRestorePointInit, ref STATEMGRSTATUS pSMgrStatus);
public static bool CreateRestorePoint(string description, out long sequenceNumber)
{
sequenceNumber = 0;
try
{
var info = new RESTOREPOINTINFO
{
dwEventType = 100, // BEGIN_SYSTEM_CHANGE
dwRestorePtType = 0, // APPLICATION_INSTALL / UNINSTALL
llSequenceNumber = 0,
szDescription = description
};
var status = new STATEMGRSTATUS();
bool ok = SRSetRestorePointW(ref info, ref status);
if (ok && status.nStatus == 0)
{
sequenceNumber = status.llSequenceNumber;
return true;
}
}
catch
{
// System Restore service disabled or policy prevented
}
return false;
}
public static bool EndRestorePoint(long sequenceNumber)
{
if (sequenceNumber <= 0) return false;
try
{
var info = new RESTOREPOINTINFO
{
dwEventType = 101, // END_SYSTEM_CHANGE
dwRestorePtType = 0,
llSequenceNumber = sequenceNumber,
szDescription = string.Empty
};
var status = new STATEMGRSTATUS();
return SRSetRestorePointW(ref info, ref status) && status.nStatus == 0;
}
catch
{
return false;
}
}
// --- Windows Shell File Operations: SendToRecycleBin (B2) ---
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
public uint wFunc;
public string pFrom;
public string? pTo;
public ushort fFlags;
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string? lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
public const uint FO_DELETE = 0x0003;
public const ushort FOF_SILENT = 0x0004;
public const ushort FOF_NOCONFIRMATION = 0x0010;
public const ushort FOF_ALLOWUNDO = 0x0040;
public const ushort FOF_NOERRORUI = 0x0400;
public static bool SendToRecycleBin(string path)
{
if (string.IsNullOrWhiteSpace(path)) return false;
if (!File.Exists(path) && !Directory.Exists(path)) return false;
// 1. Primary: Microsoft Windows Desktop Shell Recycle Bin (IFileOperation wrapper)
try
{
if (Directory.Exists(path))
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(
path,
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
return !Directory.Exists(path);
}
else if (File.Exists(path))
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(
path,
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
return !File.Exists(path);
}
}
catch { }
// 2. Secondary: Native SHFileOperation with guaranteed double-null termination
try
{
var shf = new SHFILEOPSTRUCT
{
hwnd = IntPtr.Zero,
wFunc = FO_DELETE,
pFrom = path + "\0\0",
pTo = null,
fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI
};
int result = SHFileOperation(ref shf);
return result == 0 && !shf.fAnyOperationsAborted;
}
catch
{
return false;
}
}
// --- MoveFileEx (Reboot cleanup) ---
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool MoveFileEx(string lpExistingFileName, string? lpNewFileName, int dwFlags);
public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004;
public static bool ScheduleDeleteOnReboot(string path)
{
try
{
return MoveFileEx(path, null, MOVEFILE_DELAY_UNTIL_REBOOT);
}
catch
{
return false;
}
}
// --- Shell Icons & Handle Cleanup (P11) ---
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern uint ExtractIconEx(string szFileName, int nIconIndex, IntPtr[]? phiconLarge, IntPtr[]? phiconSmall, uint nIcons);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyIcon(IntPtr hIcon);
// --- Window & Process Control ---
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string? lpClassName, string? lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, uint fuFlags, uint uTimeout, out IntPtr lpdwResult);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public const uint WM_CLOSE = 0x0010;
public const uint SMTO_ABORTIFHUNG = 0x0002;
public const int SW_HIDE = 0;
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_NOZORDER = 0x0004;
// --- Process Path by Win32 QueryFullProcessImageName (P9) ---
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool QueryFullProcessImageName(IntPtr hProcess, int dwFlags, StringBuilder lpExeName, ref int lpdwSize);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);
public const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
public static string? GetProcessPath(int processId)
{
IntPtr hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId);
if (hProcess == IntPtr.Zero) return null;
try
{
var sb = new StringBuilder(1024);
int size = sb.Capacity;
if (QueryFullProcessImageName(hProcess, 0, sb, ref size))
{
return sb.ToString();
}
}
catch { }
finally
{
CloseHandle(hProcess);
}
return null;
}
public static void GentleKillProcess(Process process, int waitMs = 3000)
{
try
{
if (process.HasExited) return;
if (process.MainWindowHandle != IntPtr.Zero)
{
SendMessageTimeout(process.MainWindowHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero, SMTO_ABORTIFHUNG, (uint)waitMs, out _);
if (process.WaitForExit(waitMs))
return;
}
process.CloseMainWindow();
if (process.WaitForExit(waitMs))
return;
process.Kill(true);
}
catch
{
// Process already terminated or access denied
}
}
// --- PE COFF Architecture Reader ---
public static ushort GetPeArchitecture(string filePath)
{
if (!File.Exists(filePath)) return 0;
try
{
using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var br = new BinaryReader(fs);
if (fs.Length < 64) return 0;
if (br.ReadUInt16() != 0x5A4D) return 0; // MZ header
fs.Seek(0x3C, SeekOrigin.Begin);
int peOffset = br.ReadInt32();
if (peOffset + 6 > fs.Length) return 0;
fs.Seek(peOffset, SeekOrigin.Begin);
if (br.ReadUInt32() != 0x00004550) return 0; // PE\0\0
return br.ReadUInt16(); // Machine
}
catch
{
return 0;
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct RESTOREPOINTINFO
{
public int dwEventType;
public int dwRestorePtType;
public long llSequenceNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szDescription;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct STATEMGRSTATUS
{
public int nStatus;
public long llSequenceNumber;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class WinTrustData : IDisposable
{
private uint cbStruct = (uint)Marshal.SizeOf(typeof(WinTrustData));
private IntPtr pPolicyCallbackData = IntPtr.Zero;
private IntPtr pSIPClientData = IntPtr.Zero;
private uint dwUIChoice = 2; // WTD_UI_NONE
private uint fdwRevocationChecks = 0; // WTD_REVOKE_NONE
private uint dwUnionChoice = 1; // WTD_CHOICE_FILE
private IntPtr pFile;
private uint dwStateAction = 0;
private IntPtr hWVTStateData = IntPtr.Zero;
private IntPtr pwszURLReference = IntPtr.Zero;
private uint dwProvFlags = 0x00000040 | 0x00000010 | 0x00001000; // WTD_SAFER_FLAG | WTD_REVOCATION_CHECK_NONE | WTD_CACHE_ONLY_URL_RETRIEVAL
private uint dwUIContext = 0;
public WinTrustData(IntPtr fileInfoPtr)
{
pFile = fileInfoPtr;
}
public void Dispose()
{
// No unmanaged allocations inside this class directly
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WINTRUST_FILE_INFO_NATIVE
{
public uint cbStruct;
[MarshalAs(UnmanagedType.LPWStr)]
public string pcwszFilePath;
public IntPtr hFile;
public IntPtr pgKnownSubject;
}
public class WinTrustFileInfo : IDisposable
{
private IntPtr _nativeMemory = IntPtr.Zero;
public IntPtr Handle => _nativeMemory;
public WinTrustFileInfo(string filePath)
{
var native = new WINTRUST_FILE_INFO_NATIVE
{
cbStruct = (uint)Marshal.SizeOf(typeof(WINTRUST_FILE_INFO_NATIVE)),
pcwszFilePath = filePath,
hFile = IntPtr.Zero,
pgKnownSubject = IntPtr.Zero
};
_nativeMemory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WINTRUST_FILE_INFO_NATIVE)));
Marshal.StructureToPtr(native, _nativeMemory, false);
}
public void Dispose()
{
if (_nativeMemory != IntPtr.Zero)
{
Marshal.DestroyStructure<WINTRUST_FILE_INFO_NATIVE>(_nativeMemory);
Marshal.FreeHGlobal(_nativeMemory);
_nativeMemory = IntPtr.Zero;
}
}
}
}