Skip to content

Commit

Permalink
Use ShellExecute to replace CreateProcesss
Browse files Browse the repository at this point in the history
Use ShellExecute to replace CreateProcesss
  • Loading branch information
Gaoyifei1011 committed Sep 4, 2024
1 parent 2085504 commit 48454aa
Show file tree
Hide file tree
Showing 28 changed files with 69 additions and 651 deletions.
Binary file modified GetStoreApp/GetStoreApp.res
Binary file not shown.
6 changes: 3 additions & 3 deletions GetStoreApp/Helpers/Controls/Store/QueryLinksHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public static List<QueryLinksModel> GetAppxPackages(string fileListXml, string r
string revisionNumber = xmlNode.GetElementsByName("UpdateIdentity").Attributes.GetNamedItem("RevisionNumber").InnerText;
string updateID = xmlNode.GetElementsByName("UpdateIdentity").Attributes.GetNamedItem("UpdateID").InnerText;
string uri = await GetAppxUrlAsync(updateID, revisionNumber, ring, digest);
string fileSizeString = FileSizeHelper.ConvertFileSizeToString(double.TryParse(fileSize, out double size) == true ? size : 0);
string fileSizeString = FileSizeHelper.ConvertFileSizeToString(double.TryParse(fileSize, out double size) ? size : 0);

appxPackagesLock.Enter();

Expand Down Expand Up @@ -530,9 +530,9 @@ public static async Task<List<QueryLinksModel>> GetNonAppxPackagesAsync(string p
string installerType = installerObject.GetNamedString("InstallerType");
string installerUrl = installerObject.GetNamedString("InstallerUrl");
string fileSize = await GetNonAppxPackageFileSizeAsync(installerUrl);
string fileSizeString = FileSizeHelper.ConvertFileSizeToString(double.TryParse(fileSize, out double size) == true ? size : 0);
string fileSizeString = FileSizeHelper.ConvertFileSizeToString(double.TryParse(fileSize, out double size) ? size : 0);

if (Equals(installerType, string.Empty) || installerUrl.ToLower().EndsWith(".exe") || installerUrl.ToLower().EndsWith(".msi"))
if (string.IsNullOrEmpty(installerType) || installerUrl.ToLower().EndsWith(".exe") || installerUrl.ToLower().EndsWith(".msi"))
{
nonAppxPackagesLock.Enter();

Expand Down
36 changes: 0 additions & 36 deletions GetStoreApp/Helpers/Root/ProcessHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GetStoreApp.Services.Root;
using GetStoreApp.WindowsAPI.PInvoke.Kernel32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using System;
using System.Collections.Generic;
using Windows.Foundation.Diagnostics;
Expand Down Expand Up @@ -54,40 +53,5 @@ public static unsafe List<uint> GetProcessPidByName(string processName)
return processEntry32PIDList;
}
}

/// <summary>
/// 创建进程
/// </summary>
public static unsafe void StartProcess(string processName, string arguments, out int processid)
{
Kernel32Library.GetStartupInfo(out STARTUPINFO startupInfo);
startupInfo.lpReserved = IntPtr.Zero;
startupInfo.lpDesktop = IntPtr.Zero;
startupInfo.lpTitle = IntPtr.Zero;
startupInfo.dwX = 0;
startupInfo.dwY = 0;
startupInfo.dwXSize = 0;
startupInfo.dwYSize = 0;
startupInfo.dwXCountChars = 500;
startupInfo.dwYCountChars = 500;
startupInfo.dwFlags = STARTF.STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = WindowShowStyle.SW_SHOWNORMAL;
startupInfo.cbReserved2 = 0;
startupInfo.lpReserved2 = IntPtr.Zero;
startupInfo.cb = sizeof(STARTUPINFO);

bool createResult = Kernel32Library.CreateProcess(null, string.Join(' ', new string[] { processName, arguments }), IntPtr.Zero, IntPtr.Zero, false, CREATE_PROCESS_FLAGS.None, IntPtr.Zero, null, ref startupInfo, out PROCESS_INFORMATION processInformation);

if (createResult)
{
if (processInformation.hProcess != IntPtr.Zero) Kernel32Library.CloseHandle(processInformation.hProcess);
if (processInformation.hThread != IntPtr.Zero) Kernel32Library.CloseHandle(processInformation.hThread);
processid = processInformation.dwProcessId;
}
else
{
processid = 0;
}
}
}
}
2 changes: 1 addition & 1 deletion GetStoreApp/Helpers/Root/RuntimeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void IsRunningElevated()
}
}

IsElevated = token_elevation_type == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
IsElevated = token_elevation_type is TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
}
}
}
6 changes: 3 additions & 3 deletions GetStoreApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("获取商店应用")]
[assembly: AssemblyFileVersion("4.6.902.0")]
[assembly: AssemblyInformationalVersion("4.6.902.0")]
[assembly: AssemblyFileVersion("4.6.904.0")]
[assembly: AssemblyInformationalVersion("4.6.904.0")]
[assembly: AssemblyProduct("获取商店应用")]
[assembly: AssemblyTitle("获取商店应用")]
[assembly: AssemblyVersion("4.6.902.0")]
[assembly: AssemblyVersion("4.6.904.0")]

// 应用程序默认区域性的资源控制器设置
[assembly: NeutralResourcesLanguage("en-us")]
Expand Down
5 changes: 3 additions & 2 deletions GetStoreApp/Services/Root/ToastNotificationService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using GetStoreApp.Extensions.DataType.Enums;
using GetStoreApp.Helpers.Root;
using GetStoreApp.Services.Controls.Settings;
using GetStoreApp.Views.Pages;
using GetStoreApp.Views.Windows;
using GetStoreApp.WindowsAPI.PInvoke.Shell32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using Microsoft.UI.Xaml;
using System;
using System.IO;
Expand Down Expand Up @@ -58,7 +59,7 @@ public static async Task HandleToastNotificationAsync(string content)
if (splitList.Length > 1)
{
string appId = splitList[1];
ProcessHelper.StartProcess("winget.exe", string.Format("install {0}", appId), out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", "winget.exe", string.Format("install {0}", appId), null, WindowShowStyle.SW_SHOWNORMAL);
}
Program.IsNeedAppLaunch = Application.Current is not null;
}
Expand Down
4 changes: 3 additions & 1 deletion GetStoreApp/UI/Controls/Download/CompletedControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using GetStoreApp.UI.Dialogs.Download;
using GetStoreApp.UI.TeachingTips;
using GetStoreApp.Views.Windows;
using GetStoreApp.WindowsAPI.PInvoke.Shell32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
Expand Down Expand Up @@ -201,7 +203,7 @@ await Task.Run(async () =>
// 普通应用:直接安装
if (completedItem.FilePath.EndsWith(".exe") || completedItem.FileName.EndsWith(".msi"))
{
ProcessHelper.StartProcess("explorer.exe ", completedItem.FilePath, out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", completedItem.FilePath, string.Empty, null, WindowShowStyle.SW_SHOWNORMAL);
}
// 商店打包应用:使用应用安装程序安装或直接安装
else
Expand Down
8 changes: 5 additions & 3 deletions GetStoreApp/UI/Controls/WinGet/InstalledAppsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using GetStoreApp.Services.Root;
using GetStoreApp.UI.Dialogs.WinGet;
using GetStoreApp.UI.TeachingTips;
using GetStoreApp.WindowsAPI.PInvoke.Shell32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using Microsoft.Management.Deployment;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -193,7 +195,7 @@ private void OnUnInstallExecuteRequested(XamlUICommand sender, ExecuteRequestedE

if (result is ContentDialogResult.Primary)
{
ProcessHelper.StartProcess(Path.Combine(InfoHelper.SystemDataPath.Windows, "System32", "Shutdown.exe"), "-r -t 120", out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", Path.Combine(InfoHelper.SystemDataPath.Windows, "System32", "Shutdown.exe"), "-r -t 120", null, WindowShowStyle.SW_SHOWNORMAL);
}
}

Expand Down Expand Up @@ -291,7 +293,7 @@ private void OnTextChanged(object sender, AutoSuggestBoxTextChangedEventArgs arg
if (sender is AutoSuggestBox autoSuggestBox)
{
SearchText = autoSuggestBox.Text;
if (SearchText == string.Empty && MatchResultList.Count > 0)
if (string.IsNullOrEmpty(SearchText) && MatchResultList.Count > 0)
{
InitializeData();
}
Expand Down Expand Up @@ -322,7 +324,7 @@ private void GetInstalledApps()
for (int index = 0; index < list.Count; index++)
{
MatchResult matchResultItem = list[index];
if (matchResultItem.CatalogPackage.InstalledVersion.Publisher != string.Empty)
if (!string.IsNullOrEmpty(matchResultItem.CatalogPackage.InstalledVersion.Publisher))
{
MatchResultList.Add(matchResultItem);
}
Expand Down
6 changes: 4 additions & 2 deletions GetStoreApp/UI/Controls/WinGet/SearchAppsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using GetStoreApp.UI.Dialogs.WinGet;
using GetStoreApp.UI.TeachingTips;
using GetStoreApp.Views.Pages;
using GetStoreApp.WindowsAPI.PInvoke.Shell32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using Microsoft.Management.Deployment;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -299,7 +301,7 @@ private void OnInstallExecuteRequested(XamlUICommand sender, ExecuteRequestedEve

if (result is ContentDialogResult.Primary)
{
ProcessHelper.StartProcess(Path.Combine(InfoHelper.SystemDataPath.Windows, "System32", "Shutdown.exe"), "-r -t 120", out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", Path.Combine(InfoHelper.SystemDataPath.Windows, "System32", "Shutdown.exe"), "-r -t 120", null, WindowShowStyle.SW_SHOWNORMAL);
}
}
}
Expand Down Expand Up @@ -419,7 +421,7 @@ private void OnInstallWithCmdExecuteRequested(XamlUICommand sender, ExecuteReque
{
Task.Run(() =>
{
ProcessHelper.StartProcess("winget.exe", string.Format("install {0}", appId), out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", "winget.exe", string.Format("install {0}", appId), null, WindowShowStyle.SW_SHOWNORMAL);
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions GetStoreApp/UI/Controls/WinGet/UpgradableAppsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using GetStoreApp.UI.Dialogs.WinGet;
using GetStoreApp.UI.TeachingTips;
using GetStoreApp.Views.Pages;
using GetStoreApp.WindowsAPI.PInvoke.Shell32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using Microsoft.Management.Deployment;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -117,7 +119,7 @@ private void OnInstallWithCmdExecuteRequested(XamlUICommand sender, ExecuteReque
{
Task.Run(() =>
{
ProcessHelper.StartProcess("winget.exe", string.Format("install {0}", appId), out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", "winget.exe", string.Format("install {0}", appId), null, WindowShowStyle.SW_SHOWNORMAL);
});
}
}
Expand Down Expand Up @@ -299,7 +301,7 @@ private void OnUpdateExecuteRequested(XamlUICommand sender, ExecuteRequestedEven

if (result is ContentDialogResult.Primary)
{
ProcessHelper.StartProcess(Path.Combine(InfoHelper.SystemDataPath.Windows, "System32", "Shutdown.exe"), "-r -t 120", out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", Path.Combine(InfoHelper.SystemDataPath.Windows, "System32", "Shutdown.exe"), "-r -t 120", null, WindowShowStyle.SW_SHOWNORMAL);
}
}

Expand Down
3 changes: 1 addition & 2 deletions GetStoreApp/Views/Pages/AppManagerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,9 +1189,8 @@ private void OnTextChanged(object sender, AutoSuggestBoxTextChangedEventArgs arg
if (sender is AutoSuggestBox autoSuggestBox)
{
SearchText = autoSuggestBox.Text;
if (SearchText.Equals(string.Empty))
if (string.IsNullOrEmpty(SearchText))
{
SearchText = string.Empty;
InitializeData();
}
}
Expand Down
2 changes: 1 addition & 1 deletion GetStoreApp/Views/Pages/AppUpdatePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private void OnUpdateAllClicked(object sender, RoutedEventArgs args)
{
AppInstallItem appInstallItem = await appInstallManager.UpdateAppByPackageFamilyNameAsync(appUpdateItem.PackageFamilyName);

if (appInstallItem != null && !AppInstallingDict.ContainsKey(appInstallItem.PackageFamilyName))
if (appInstallItem is not null && !AppInstallingDict.ContainsKey(appInstallItem.PackageFamilyName))
{
appInstallItem.Completed += OnAppInstallItemCompleted;
appInstallItem.StatusChanged += OnAppInstallItemStatausChanged;
Expand Down
4 changes: 3 additions & 1 deletion GetStoreApp/Views/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using GetStoreApp.UI.TeachingTips;
using GetStoreApp.Views.Windows;
using GetStoreApp.WindowsAPI.ComTypes;
using GetStoreApp.WindowsAPI.PInvoke.Shell32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
Expand Down Expand Up @@ -517,7 +519,7 @@ private void OnOpenWinGetSettingsClicked(object sender, RoutedEventArgs args)
}
else
{
ProcessHelper.StartProcess("winget.exe", "settings", out _);
Shell32Library.ShellExecute(IntPtr.Zero, "open", "winget.exe", "settings", null, WindowShowStyle.SW_HIDE);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion GetStoreApp/Views/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ private void CheckNetwork()
try
{
ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isConnected = connectionProfile != null && connectionProfile.GetNetworkConnectivityLevel() is NetworkConnectivityLevel.InternetAccess;
bool isConnected = connectionProfile is not null && connectionProfile.GetNetworkConnectivityLevel() is NetworkConnectivityLevel.InternetAccess;

if (!isConnected)
{
Expand Down
Loading

0 comments on commit 48454aa

Please sign in to comment.