DllExporter: Improve ildasm.exe path detection

This commit is contained in:
Birunthan Mohanathas 2014-01-17 16:27:16 +02:00
parent c833f604c5
commit 1aa242b5c9
2 changed files with 64 additions and 21 deletions

View File

@ -2,6 +2,6 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
[assembly: AssemblyCopyright("© 2011 - Birunthan Mohanathas")] [assembly: AssemblyCopyright("© 2013 - Birunthan Mohanathas")]
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyProduct("Rainmeter")] [assembly: AssemblyProduct("Rainmeter")]

View File

@ -21,6 +21,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Diagnostics; using System.Diagnostics;
using Microsoft.Build.Utilities; using Microsoft.Build.Utilities;
using System.IO;
namespace DllExporter namespace DllExporter
{ {
@ -36,28 +37,20 @@ namespace DllExporter
string targetResName = targetDllName + ".res"; string targetResName = targetDllName + ".res";
string ilasmPath = ToolLocationHelper.GetPathToDotNetFrameworkFile("ilasm.exe", TargetDotNetFrameworkVersion.Version20); 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"); Console.WriteLine("DllExporter error: ilasm.exe not found");
return 1; return 1;
} }
string ildasmPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe"); string ildasmPath = FindIldasmPath();
if (!System.IO.File.Exists(ildasmPath)) if (ildasmPath == null)
{ {
ildasmPath = Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe"); Console.WriteLine("DllExporter error: ildasm.exe not found");
if (!System.IO.File.Exists(ildasmPath)) return 1;
{
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;
}
}
} }
System.IO.Directory.SetCurrentDirectory(targetDirectory); Directory.SetCurrentDirectory(targetDirectory);
bool is64 = platformTarget.ToLower().Equals("x64"); bool is64 = platformTarget.ToLower().Equals("x64");
bool isDebug = configurationName.ToLower().Equals("debug"); bool isDebug = configurationName.ToLower().Equals("debug");
@ -84,10 +77,10 @@ namespace DllExporter
return ildasmProc.ExitCode; return ildasmProc.ExitCode;
} }
bool hasResource = System.IO.File.Exists(targetResName); bool hasResource = File.Exists(targetResName);
// Read disassembly and find methods marked with DllExport attribute // Read disassembly and find methods marked with DllExport attribute
List<string> lines = new List<string>(System.IO.File.ReadAllLines(targetIlName)); List<string> lines = new List<string>(File.ReadAllLines(targetIlName));
int attributeIndex = 0; int attributeIndex = 0;
int exportCount = 0; int exportCount = 0;
while (true) while (true)
@ -151,7 +144,7 @@ namespace DllExporter
} }
// Write everything back // Write everything back
System.IO.File.WriteAllLines(targetIlName, lines.ToArray()); File.WriteAllLines(targetIlName, lines.ToArray());
// Reassemble // Reassemble
Process ilasmProc = new Process(); Process ilasmProc = new Process();
@ -172,10 +165,60 @@ namespace DllExporter
} }
// Cleanup // Cleanup
System.IO.File.Delete(targetIlName); File.Delete(targetIlName);
System.IO.File.Delete(targetResName); File.Delete(targetResName);
return 0; return 0;
} }
/// <summary>
/// Finds path to ildasm.exe.
/// </summary>
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;
}
} }
} }