From 1aa242b5c9055d28610e7f0f23ce42f908cf1b63 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Fri, 17 Jan 2014 16:27:16 +0200 Subject: [PATCH] DllExporter: Improve ildasm.exe path detection --- Plugins/API/DllExporter/AssemblyInfo.cs | 4 +- Plugins/API/DllExporter/Program.cs | 81 +++++++++++++++++++------ 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/Plugins/API/DllExporter/AssemblyInfo.cs b/Plugins/API/DllExporter/AssemblyInfo.cs index 23c9f981..c8267cd4 100644 --- a/Plugins/API/DllExporter/AssemblyInfo.cs +++ b/Plugins/API/DllExporter/AssemblyInfo.cs @@ -2,6 +2,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -[assembly: AssemblyCopyright("© 2011 - Birunthan Mohanathas")] -[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyCopyright("© 2013 - Birunthan Mohanathas")] +[assembly: AssemblyVersion("1.0.1.0")] [assembly: AssemblyProduct("Rainmeter")] \ No newline at end of file diff --git a/Plugins/API/DllExporter/Program.cs b/Plugins/API/DllExporter/Program.cs index 92317489..eee18623 100644 --- a/Plugins/API/DllExporter/Program.cs +++ b/Plugins/API/DllExporter/Program.cs @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.Text; using System.Diagnostics; using Microsoft.Build.Utilities; +using System.IO; namespace DllExporter { @@ -36,28 +37,20 @@ namespace DllExporter string targetResName = targetDllName + ".res"; string ilasmPath = ToolLocationHelper.GetPathToDotNetFrameworkFile("ilasm.exe", TargetDotNetFrameworkVersion.Version20); - if (!System.IO.File.Exists(ilasmPath)) + if (!File.Exists(ilasmPath)) { Console.WriteLine("DllExporter error: ilasm.exe not found"); return 1; } - string ildasmPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe"); - if (!System.IO.File.Exists(ildasmPath)) + string ildasmPath = FindIldasmPath(); + if (ildasmPath == null) { - ildasmPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe"); - if (!System.IO.File.Exists(ildasmPath)) - { - ildasmPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\ildasm.exe"); - if (!System.IO.File.Exists(ildasmPath)) - { - Console.WriteLine("DllExporter error: ildasm.exe not found"); - return 1; - } - } + Console.WriteLine("DllExporter error: ildasm.exe not found"); + return 1; } - System.IO.Directory.SetCurrentDirectory(targetDirectory); + Directory.SetCurrentDirectory(targetDirectory); bool is64 = platformTarget.ToLower().Equals("x64"); bool isDebug = configurationName.ToLower().Equals("debug"); @@ -84,10 +77,10 @@ namespace DllExporter return ildasmProc.ExitCode; } - bool hasResource = System.IO.File.Exists(targetResName); + bool hasResource = File.Exists(targetResName); // Read disassembly and find methods marked with DllExport attribute - List lines = new List(System.IO.File.ReadAllLines(targetIlName)); + List lines = new List(File.ReadAllLines(targetIlName)); int attributeIndex = 0; int exportCount = 0; while (true) @@ -151,7 +144,7 @@ namespace DllExporter } // Write everything back - System.IO.File.WriteAllLines(targetIlName, lines.ToArray()); + File.WriteAllLines(targetIlName, lines.ToArray()); // Reassemble Process ilasmProc = new Process(); @@ -172,10 +165,60 @@ namespace DllExporter } // Cleanup - System.IO.File.Delete(targetIlName); - System.IO.File.Delete(targetResName); + File.Delete(targetIlName); + File.Delete(targetResName); return 0; } + + /// + /// Finds path to ildasm.exe. + /// + private static string FindIldasmPath() + { + var sdkPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles%\Microsoft SDKs\Windows\"); + if (!Directory.Exists(sdkPath)) + { + sdkPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Microsoft SDKs\Windows\"); + } + + if (!Directory.Exists(sdkPath)) + { + throw new DirectoryNotFoundException("'Microsoft SDKs' directory not found"); + } + + // Get the version directories in reverse order (i.e. newest version first). + var sdkVersionDirectories = Directory.GetDirectories(sdkPath); + Array.Reverse(sdkVersionDirectories); + + foreach (var sdkVersionDirectory in sdkVersionDirectories) + { + var binDirectory = Path.Combine(sdkVersionDirectory, @"bin"); + if (!Directory.Exists(binDirectory)) + { + continue; + } + + // Check for e.g. 'Microsoft SDKs\v8.0A\bin\ildasm.exe'. + var ildasmPath = Path.Combine(binDirectory, @"ildasm.exe"); + if (File.Exists(ildasmPath)) + { + return ildasmPath; + } + + // Check for e.g. 'Microsoft SDKs\v8.0A\bin\NETFX 4.0 Tools\ildasm.exe'. + var toolsDirectories = Directory.GetDirectories(binDirectory, "NETFX*Tools"); + foreach (var toolDirectory in toolsDirectories) + { + ildasmPath = Path.Combine(toolDirectory, @"ildasm.exe"); + if (File.Exists(ildasmPath)) + { + return ildasmPath; + } + } + } + + return null; + } } }