Skip to content

Commit

Permalink
Program.cs: Hide vcruntime140.dll from the user on Windows.
Browse files Browse the repository at this point in the history
Instead of extracting the DLL to the same directory as the executable, this extracts it to the .NET created directory where libsodium.dll lives. This is unfortunately quite complicated because there are multiple directories that could be being used. The user can also avoid the default %TEMP%/.net directory by setting the DOTNET_BUNDLE_EXTRACT_BASE_DIR environment variable. See here: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli#native-libraries
  • Loading branch information
samuel-lucas6 committed Aug 11, 2024
1 parent 4c24110 commit 57e7236
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/Kryptor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class Program

[Option("-r|--recover", "recover your public key from your private key", CommandOptionType.NoValue)]
private bool RecoverPublicKey { get; }

[Option("-m|--modify", "change your private key passphrase", CommandOptionType.NoValue)]
private bool ChangePrivateKeyPassphrase { get; }

Expand Down Expand Up @@ -99,7 +99,9 @@ public class Program

private int OnExecute()
{
ExtractVisualCRuntime();
if (OperatingSystem.IsWindows()) {
ExtractVisualCRuntime();
}
Globals.Overwrite = Overwrite;
Globals.EncryptFileNames = EncryptFileNames;
Globals.TotalCount = FilePaths?.Length ?? 0;
Expand Down Expand Up @@ -144,20 +146,30 @@ private int OnExecute()
}
return Environment.ExitCode;
}

private static void ExtractVisualCRuntime()
{
try
{
string vcruntimeFilePath = Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), "vcruntime140.dll");
if (!OperatingSystem.IsWindows() || File.Exists(vcruntimeFilePath)) {
return;
try {
const string environmentVariable = "DOTNET_BUNDLE_EXTRACT_BASE_DIR";
string executableFileName = Path.GetFileNameWithoutExtension(Environment.ProcessPath);
// The environment variables take priority in this order
string environmentDirectory = Environment.GetEnvironmentVariable(environmentVariable, EnvironmentVariableTarget.Process) ?? Environment.GetEnvironmentVariable(environmentVariable, EnvironmentVariableTarget.User) ?? Environment.GetEnvironmentVariable(environmentVariable, EnvironmentVariableTarget.Machine);
if (!string.IsNullOrWhiteSpace(environmentDirectory)) {
environmentDirectory = Path.Combine(environmentDirectory, executableFileName);
}
if (Environment.Is64BitOperatingSystem) {
File.WriteAllBytes(vcruntimeFilePath, Properties.Resources.vcruntime140x64);
return;
string defaultDirectory = Path.Combine(Path.GetTempPath(), ".net", executableFileName);
string[] subdirectories = Directory.GetDirectories(!Directory.Exists(environmentDirectory) ? defaultDirectory : environmentDirectory);
foreach (var directory in subdirectories) {
string vcruntimeFilePath = Path.Combine(directory, "vcruntime140.dll");
if (File.Exists(vcruntimeFilePath)) {
continue;
}
if (Environment.Is64BitOperatingSystem) {
File.WriteAllBytes(vcruntimeFilePath, Properties.Resources.vcruntime140x64);
continue;
}
File.WriteAllBytes(vcruntimeFilePath, Properties.Resources.vcruntime140x86);
}
File.WriteAllBytes(vcruntimeFilePath, Properties.Resources.vcruntime140x86);
}
catch (Exception ex) when (ExceptionFilters.FileAccess(ex))
{
Expand All @@ -171,4 +183,4 @@ private static void ExtractVisualCRuntime()
private static string GetSigningPrivateKey(string privateKeyPath) => string.IsNullOrEmpty(privateKeyPath) ? Constants.DefaultSigningPrivateKeyPath : privateKeyPath;

public static string GetVersion() => Assembly.GetExecutingAssembly().GetName().Version?.ToString(fieldCount: 3);
}
}

0 comments on commit 57e7236

Please sign in to comment.