private void PinTempShortcutFromHwnd(IntPtr hWnd, string appId) { if (DisableShortcutChanging) { return; } // Get process ID from window int hwndPid; Interop.GetWindowThreadProcessId(hWnd, out hwndPid); // Get process handle from pID; get process path from handle IntPtr hwndPidHandle = Interop.OpenProcess(Interop.ProcessAccess.QueryInformation | Interop.ProcessAccess.VMRead, false, hwndPid); StringBuilder hwndProcessNameString = new StringBuilder(260); Interop.GetModuleFileNameEx(hwndPidHandle, IntPtr.Zero, hwndProcessNameString, 260); Interop.CloseHandle(hwndPidHandle); string hwndProcessPath; try { hwndProcessPath = hwndProcessNameString.ToString(); } catch (Exception e) { return; } if (!File.Exists(hwndProcessPath)) { return; } string hwndProcessName = Path.GetFileNameWithoutExtension(hwndProcessPath).ToLower(); if (IsAppPinnedByProcessName(hwndProcessName)) { return; } string newLnkPath = Path.Combine(Common.Path_AppData, "TempShortcuts" + "\\" + hwndProcessName + ".lnk"); using (ShellLink shortcut = new ShellLink()) { shortcut.Target = hwndProcessPath; shortcut.WorkingDirectory = Path.GetDirectoryName(hwndProcessPath); shortcut.Description = hwndProcessName; shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal; shortcut.Save(newLnkPath); } Common.TaskbarManagerInstance.SetApplicationIdForShortcut(newLnkPath, appId); if (TempShortcut_AppIdPairs.ContainsKey(newLnkPath)) { TempShortcut_AppIdPairs.Remove(newLnkPath); } TempShortcut_AppIdPairs.Add(newLnkPath, appId); PinShortcut(newLnkPath, false); }
private void UnpinTempShortcut(IntPtr hWnd, string appId) { if (DisableShortcutChanging) { return; } string hwndProcessPath = ""; string hwndProcessName = ""; string tempLnkPath = ""; if (!IntPtr.Equals(hWnd, IntPtr.Zero)) { // Get process ID from window int hwndPid; Interop.GetWindowThreadProcessId(hWnd, out hwndPid); // Get process handle from pID; get process path from handle IntPtr hwndPidHandle = Interop.OpenProcess(Interop.ProcessAccess.QueryInformation | Interop.ProcessAccess.VMRead, false, hwndPid); StringBuilder hwndProcessNameString = new StringBuilder(260); Interop.GetModuleFileNameEx(hwndPidHandle, IntPtr.Zero, hwndProcessNameString, 260); Interop.CloseHandle(hwndPidHandle); try { hwndProcessPath = hwndProcessNameString.ToString(); } catch (Exception e) { return; } if (!File.Exists(hwndProcessPath)) { return; } hwndProcessName = Path.GetFileNameWithoutExtension(hwndProcessPath).ToLower(); tempLnkPath = Path.Combine(Common.Path_AppData, "TempShortcuts" + "\\" + hwndProcessName + ".lnk"); } else { // Get lnk from appId foreach (KeyValuePair <string, string> lnkPair in TempShortcut_AppIdPairs) { string lnkName = lnkPair.Key; string lnkAppId = lnkPair.Value; if (appId == lnkAppId) { hwndProcessName = Path.GetFileNameWithoutExtension(lnkName).ToLower(); tempLnkPath = lnkName; } } } if (!IsAppPinnedByProcessName(hwndProcessName)) { return; } if (File.Exists(tempLnkPath)) { if (TempShortcut_AppIdPairs.ContainsKey(tempLnkPath)) { TempShortcut_AppIdPairs.Remove(tempLnkPath); } UnpinShortcut(tempLnkPath); File.Delete(tempLnkPath); } }