2012-01-08 17:35:29 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011 Birunthan Mohanathas
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2012-01-08 17:35:29 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Diagnostics;
|
2012-12-16 18:19:07 +00:00
|
|
|
|
using Microsoft.Build.Utilities;
|
2014-01-17 14:27:16 +00:00
|
|
|
|
using System.IO;
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
|
|
namespace DllExporter
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
static int Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
string configurationName = args[0];
|
|
|
|
|
string platformTarget = args[1];
|
|
|
|
|
string targetDirectory = args[2];
|
|
|
|
|
string targetDllName = targetDirectory + args[3];
|
|
|
|
|
string targetIlName = targetDllName + ".il";
|
|
|
|
|
string targetResName = targetDllName + ".res";
|
2012-12-16 18:19:07 +00:00
|
|
|
|
|
|
|
|
|
string ilasmPath = ToolLocationHelper.GetPathToDotNetFrameworkFile("ilasm.exe", TargetDotNetFrameworkVersion.Version20);
|
2014-01-17 14:27:16 +00:00
|
|
|
|
if (!File.Exists(ilasmPath))
|
2012-12-16 18:19:07 +00:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: ilasm.exe not found");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 14:27:16 +00:00
|
|
|
|
string ildasmPath = FindIldasmPath();
|
|
|
|
|
if (ildasmPath == null)
|
2012-12-16 18:19:07 +00:00
|
|
|
|
{
|
2014-01-17 14:27:16 +00:00
|
|
|
|
Console.WriteLine("DllExporter error: ildasm.exe not found");
|
|
|
|
|
return 1;
|
2012-12-16 18:19:07 +00:00
|
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
2014-01-17 14:27:16 +00:00
|
|
|
|
Directory.SetCurrentDirectory(targetDirectory);
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
|
|
bool is64 = platformTarget.ToLower().Equals("x64");
|
|
|
|
|
bool isDebug = configurationName.ToLower().Equals("debug");
|
|
|
|
|
|
|
|
|
|
// Disassemble
|
|
|
|
|
Process ildasmProc = new Process();
|
2013-05-30 14:00:05 +00:00
|
|
|
|
string ildasmArgs = string.Format(
|
|
|
|
|
"/nobar {0} /output=\"{1}\" \"{2}\"",
|
|
|
|
|
isDebug ? "/linenum" : "",
|
|
|
|
|
targetIlName,
|
|
|
|
|
targetDllName);
|
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
|
ildasmProc.StartInfo = new ProcessStartInfo(ildasmPath, ildasmArgs);
|
|
|
|
|
ildasmProc.StartInfo.UseShellExecute = false;
|
|
|
|
|
ildasmProc.StartInfo.CreateNoWindow = false;
|
|
|
|
|
ildasmProc.StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
ildasmProc.Start();
|
|
|
|
|
ildasmProc.WaitForExit();
|
|
|
|
|
|
|
|
|
|
if (ildasmProc.ExitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: Unable to disassemble!");
|
|
|
|
|
Console.WriteLine(ildasmProc.StandardOutput.ReadToEnd());
|
|
|
|
|
return ildasmProc.ExitCode;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 14:27:16 +00:00
|
|
|
|
bool hasResource = File.Exists(targetResName);
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
|
|
// Read disassembly and find methods marked with DllExport attribute
|
2014-01-17 14:27:16 +00:00
|
|
|
|
List<string> lines = new List<string>(File.ReadAllLines(targetIlName));
|
2012-01-08 17:35:29 +00:00
|
|
|
|
int attributeIndex = 0;
|
|
|
|
|
int exportCount = 0;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
attributeIndex = lines.FindIndex(attributeIndex, new Predicate<string>(x => x.Contains(".custom instance void") && x.Contains("DllExport::.ctor()")));
|
|
|
|
|
if (attributeIndex < 8) break;
|
|
|
|
|
|
|
|
|
|
int methodIndex = lines.FindLastIndex(attributeIndex, attributeIndex, new Predicate<string>(x => x.Contains(".method")));
|
|
|
|
|
if (methodIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: Unable to parse disassembly (.method not found)!");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int functionIndex = lines.FindIndex(methodIndex, new Predicate<string>(x => x.Contains("(")));
|
|
|
|
|
if (functionIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: Unable to parse disassembly (bracket not found)!");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bracketPos = lines[functionIndex].IndexOf('(');
|
|
|
|
|
int functionNamePos = lines[functionIndex].LastIndexOf(' ', bracketPos);
|
|
|
|
|
string functionName = lines[functionIndex].Substring(functionNamePos, bracketPos - functionNamePos);
|
|
|
|
|
|
|
|
|
|
// Change calling convention to cdecl
|
|
|
|
|
lines[functionIndex] = string.Format("{0} modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) {1}", lines[functionIndex].Substring(0, functionNamePos - 1), lines[functionIndex].Substring(functionNamePos));
|
|
|
|
|
|
|
|
|
|
int attributeBeginPos = lines[attributeIndex].IndexOf('.');
|
|
|
|
|
string spaces = new string(' ', attributeBeginPos);
|
|
|
|
|
|
|
|
|
|
// Replace attribute with export
|
|
|
|
|
++exportCount;
|
|
|
|
|
lines[attributeIndex] = string.Format("{0}.export [{1}] as {2}", spaces, exportCount, functionName);
|
|
|
|
|
|
|
|
|
|
++attributeIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (exportCount == 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter warning: Nothing found to export.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the DllExport class
|
|
|
|
|
int classIndex = lines.FindIndex(new Predicate<string>(x => x.Contains(".class ") && x.EndsWith(".DllExport")));
|
|
|
|
|
if (classIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: Unable to parse disassembly (DllExport class not found)!");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int classEndIndex = lines.FindIndex(classIndex, new Predicate<string>(x => x.Contains("} // end of class") && x.EndsWith(".DllExport")));
|
|
|
|
|
if (classEndIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: Unable to parse disassembly (DllExport class end not found)!");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines.RemoveRange(classIndex, classEndIndex - classIndex + 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write everything back
|
2014-01-17 14:27:16 +00:00
|
|
|
|
File.WriteAllLines(targetIlName, lines.ToArray());
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
|
|
// Reassemble
|
|
|
|
|
Process ilasmProc = new Process();
|
2012-10-04 17:18:24 +00:00
|
|
|
|
string resource = hasResource ? string.Format("/resource=\"{0}\"", targetResName) : "";
|
|
|
|
|
string ilasmArgs = string.Format("/nologo /quiet /dll {0} {1} /output=\"{2}\" {3} \"{4}\"", isDebug ? "/debug /pdb" : "/optimize", is64 ? "/x64 /PE64" : "", targetDllName, resource, targetIlName);
|
2012-01-08 17:35:29 +00:00
|
|
|
|
ilasmProc.StartInfo = new ProcessStartInfo(ilasmPath, ilasmArgs);
|
|
|
|
|
ilasmProc.StartInfo.UseShellExecute = false;
|
|
|
|
|
ilasmProc.StartInfo.CreateNoWindow = false;
|
|
|
|
|
ilasmProc.StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
ilasmProc.Start();
|
|
|
|
|
ilasmProc.WaitForExit();
|
|
|
|
|
|
|
|
|
|
if (ilasmProc.ExitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("DllExporter error: Unable to assemble!");
|
|
|
|
|
Console.WriteLine(ilasmProc.StandardOutput.ReadToEnd());
|
|
|
|
|
return ilasmProc.ExitCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
2014-01-17 14:27:16 +00:00
|
|
|
|
File.Delete(targetIlName);
|
|
|
|
|
File.Delete(targetResName);
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-01-17 14:27:16 +00:00
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|