Skip to content

Commit

Permalink
dropped User32 requirement for StartInfo.ShowWindow also renamed Wind…
Browse files Browse the repository at this point in the history
…owStyle
  • Loading branch information
lostmsu committed Jun 25, 2023
1 parent fd43818 commit d6c367e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/NDetours.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<!-- Package stuff -->
<Version>0.0.8</Version>
<Version>0.0.9</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Description>Managed package that simplifies injection of Detours-based DLLs</Description>
Expand Down Expand Up @@ -36,7 +36,6 @@
<!-- The following is recommended for public projects -->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="PInvoke.Kernel32" Version="0.7.124" />
<PackageReference Include="PInvoke.User32" Version="0.7.124" />
</ItemGroup>

</Project>
15 changes: 13 additions & 2 deletions src/ProcessDetour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ public static unsafe Process Start(StartInfo startInfo) {
var startupInfo = new Kernel32.STARTUPINFO {
cb = sizeof(Kernel32.STARTUPINFO),
};
if (startInfo.ShowWindow is { } style) {
if (startInfo.WindowStyle is { } style && style != ProcessWindowStyle.Normal) {
startupInfo.dwFlags |= Kernel32.StartupInfoFlags.STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = (ushort)style;
startupInfo.wShowWindow = style switch {
ProcessWindowStyle.Hidden => ShowWindow.HIDE,
ProcessWindowStyle.Minimized => ShowWindow.MINIMIZE,
ProcessWindowStyle.Maximized => ShowWindow.MAXIMIZE,
_ => throw new ArgumentOutOfRangeException(nameof(startInfo.WindowStyle)),
};
}
Kernel32.PROCESS_INFORMATION processInfo;
var flags = startInfo.Flags;
Expand Down Expand Up @@ -200,4 +205,10 @@ static unsafe void EnableDebugging(IPackageDebugSettings debugSettings,
sb.Append('\0');
return sb.ToString().ToCharArray();
}

class ShowWindow {
public const int HIDE = 0;
public const int MINIMIZE = 2;
public const int MAXIMIZE = 3;
}
}
15 changes: 2 additions & 13 deletions src/StartInfo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace NDetours;

using System.Collections.ObjectModel;
using System.Diagnostics;

using static PInvoke.Kernel32;
using static PInvoke.User32;

partial class ProcessDetour {
public sealed class StartInfo {
Expand All @@ -13,18 +13,7 @@ public sealed class StartInfo {
public string? CommandLine { get; set; }
public IDictionary<string, string>? Environment { get; set; }
public CreateProcessFlags Flags { get; set; } = CreateProcessFlags.None;

WindowShowStyle showWindow = WindowShowStyle.SW_SHOWDEFAULT;
public WindowShowStyle? ShowWindow {
get => this.showWindow == WindowShowStyle.SW_SHOWDEFAULT
? null : this.showWindow;
set {
if (value == WindowShowStyle.SW_SHOWDEFAULT)
throw new ArgumentException($"{nameof(WindowShowStyle.SW_SHOWDEFAULT)} can not be used when starting new process.");

this.showWindow = value ?? WindowShowStyle.SW_SHOWDEFAULT;
}
}
public ProcessWindowStyle WindowStyle { get; set; }

public StartInfo(string executable) {
this.Executable = executable ?? throw new ArgumentNullException(nameof(executable));
Expand Down

0 comments on commit d6c367e

Please sign in to comment.