Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Remove subsystem check and generate stubs for console apps
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Mar 25, 2022
1 parent 3fe7499 commit dcbdbdb
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 33 deletions.
31 changes: 0 additions & 31 deletions src/Squirrel/Internal/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,37 +586,6 @@ public static Uri AddQueryParamsToUri(Uri uri, IEnumerable<KeyValuePair<string,
return builder.Uri;
}

// http://stackoverflow.com/questions/3111669/how-can-i-determine-the-subsystem-used-by-a-given-net-assembly
public static bool ExecutableUsesWin32Subsystem(string peImage)
{
using (var s = new FileStream(peImage, FileMode.Open, FileAccess.Read)) {
var rawPeSignatureOffset = new byte[4];
s.Seek(0x3c, SeekOrigin.Begin);
s.Read(rawPeSignatureOffset, 0, 4);

int peSignatureOffset = rawPeSignatureOffset[0];
peSignatureOffset |= rawPeSignatureOffset[1] << 8;
peSignatureOffset |= rawPeSignatureOffset[2] << 16;
peSignatureOffset |= rawPeSignatureOffset[3] << 24;

var coffHeader = new byte[24];
s.Seek(peSignatureOffset, SeekOrigin.Begin);
s.Read(coffHeader, 0, 24);

byte[] signature = { (byte) 'P', (byte) 'E', (byte) '\0', (byte) '\0' };
for (int index = 0; index < 4; index++) {
if (coffHeader[index] != signature[index]) throw new Exception("File is not a PE image");
}

var subsystemBytes = new byte[2];
s.Seek(68, SeekOrigin.Current);
s.Read(subsystemBytes, 0, 2);

int subSystem = subsystemBytes[0] | subsystemBytes[1] << 8;
return subSystem == 2; /*IMAGE_SUBSYSTEM_WINDOWS_GUI*/
}
}

readonly static string[] peExtensions = new[] { ".exe", ".dll", ".node" };
public static bool FileIsLikelyPEImage(string name)
{
Expand Down
3 changes: 1 addition & 2 deletions src/SquirrelCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,8 @@ RuntimeCpu parseMachine(PeNet.Header.Pe.MachineType machine)
Log.Info("Creating stub executables");
new DirectoryInfo(pkgPath).GetAllFilesRecursively()
.Where(x => x.Name.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
.Where(x => !x.Name.Contains("squirrel.exe", StringComparison.InvariantCultureIgnoreCase))
.Where(x => !x.Name.Equals("squirrel.exe", StringComparison.InvariantCultureIgnoreCase))
.Where(x => Utility.IsFileTopLevelInPackage(x.FullName, pkgPath))
.Where(x => Utility.ExecutableUsesWin32Subsystem(x.FullName))
.ToArray() // materialize the IEnumerable so we never end up creating stubs for stubs
.ForEach(x => createExecutableStubForExe(x.FullName));
Expand Down

0 comments on commit dcbdbdb

Please sign in to comment.