I'm looking for a way to extract programmatically information like method declared, functions declared and called, strings et similar from a .exe wrote in c#.
I thought about getting the AST and then analyze it, I've found some examples but they point to dead links. I've tried using the reflection of .NET but I wasn't able to reach what I needed.
Any suggestion?
Example:
input: helloWorld.exe
output:
String: "Hello World!"
Declared function: PrintHelloWorld
called function: PrintHelloWorld, Console.WriteLine
or something similar.
You could use the ICSharpCode.Decompiler library to decompile the methods that you need.
Here an example:
var assemblyPath = #"MyPath\Demo.ConsoleApp.dll";
var decompiler = new CSharpDecompiler(assemblyPath, new DecompilerSettings());
var name = new FullTypeName("Demo.ConsoleApp.Foo");
ITypeDefinition typeInfo = decompiler.TypeSystem.MainModule.Compilation.FindType(name).GetDefinition();
var tokenOfFirstMethod = typeInfo.Methods.First().MetadataToken;
Console.WriteLine(decompiler.DecompileAsString(tokenOfFirstMethod));
And here the nuget package
Related
My problem is that I'm trying to use Python.NET inside Visual Studio, I installed Python 3.5, and the python.NET package trough nuget and trough pip too.
added namespace Python.Runtime in my Form application, and the Python.Runtime.dll is there in the references too.
I tried to use a sample code from the offical site: offical site
using Python.Runtime;
// create a person object
Person person = new Person("John", "Smith");
// acquire the GIL before using the Python interpreter
using (Py.GIL())
{
// create a Python scope
using (PyScope scope = Py.CreateScope())
{
// convert the Person object to a PyObject
PyObject pyPerson = person.ToPython();
// create a Python variable "person"
scope.Set("person", pyPerson);
// the person object may now be used in Python
string code = "fullName = person.FirstName + ' ' + person.LastName";
scope.Exec(code);
}
}
The Py.GIL() part works and I already tried to import numpy package and do some basic calculations with it, it worked well.
However the PyScope is just not recognized, nor do Py.CreateScope.
("The type or namespace PyScope could not be found")
Tried to write Python.Runtime.PyScope, tried reinstalling, tried older package, used console app and winforms app too, however nothing seems to work.
Am I missing something here?
I ran into this problem too. Using var instead of PyScope worked for me. As in:
using (var scope = Py.CreateScope())
Edit & Alternative: when I visited the definition of CreateScope(), the output type was PyModule:
public static PyModule CreateScope();
public static PyModule CreateScope(string name);
Using this instead of var also works for me:
using (PyModule scope = Py.CreateScope())
If that doesn't work for you, visiting the definition of the function and using the output type listed in your version likely will.
The var isn't picky though.
ILSpy is using cecil along with it's own libraries to accomplish this, i have heard that .NETReflector is using his own private algorithm to generate the higher level c#,f#,vb code from MSIL, any methods/references on how to do this without dependencies ?
EDIT: i'm now satified with ILSpy and cecil as a backbone
SOLUTION:
string GetSourceCode(string path) {
var asmDef = AssemblyDefinition.ReadAssembly(path);
var strBuilder = new StringBuilder();
foreach (var type in asmDef.MainModule.Types) {
if (!type.IsCompilerGenerated()) {
AstBuilder builder = new AstBuilder(new DecompilerContext(asmDef.MainModule));
builder.AddType(type);
var output = new StringWriter();
builder.GenerateCode(new PlainTextOutput(output));
strBuilder.Append(output.ToString());
output.Dispose();
}
}
return strBuilder.ToString();
}
Well, first thing you have to understand, is that there is no such thing as plain source code in compiled dll or exe file. Upon compilation C# code is compiled into CIL. Mono.Cecil reads dll or exe file as byte array and decodes it into CIL instructions. The same thing does .Net platform, when it runs your programm. What ILSpy does is that it tries to guess how did the source code look like, based upon decoded instructions.
.NETReflector does the same thing but without using Cecil
I have an embedded resource in my dll which is a python script. I'd like to make the classes and functions in that resource available to the python engine, so I can execute external .py files (as __main__) which would be able to do something like
import embedded_lib # where embedded_lib is an embedded python script
Is there a way to accomplish this? I was hoping there will be some sort of IronPython.ImportModule('module_name', source) so I've looked through IronPython docs and couldn't find anything, but I'm hoping I'm just bad at looking.. Maybe there is some way to intercept a call to import and load my script that way?
It is possible. You just need to add search paths to ScriptEngine object like this:
var paths = engine.GetSearchPaths();
paths.Add(yourLibsPath); // add directory to search
or
engine.SetSearchPaths(paths);
Then you could use any module in directories, which you add:
import pyFileName # without extension .py
Update
OK. If you want to use embedded resource strings like module, you may use this code:
var scope = engine.CreateScope(); // Create ScriptScope to use it like a module
engine.Execute("import clr\n" +
"clr.AddReference(\"System.Windows.Forms\")\n" +
"import System.Windows.Forms\n" +
"def Hello():\n" +
"\tSystem.Windows.Forms.MessageBox.Show(\"Hello World!\")", scope); // Execute code from string in scope.
Now you have a ScriptScope object (scope in code) containing all executed functions. And you may insert them into another scope like this:
foreach (var keyValuePair in scope.GetItems())
{
if(keyValuePair.Value != null)
anotherScope.SetVariable(keyValuePair.Key, keyValuePair.Value);
}
Or you can execute your scripts right in this ScriptScope:
dynamic executed = engine.ExecuteFile("Filename.py", scope);
executed.SomeFuncInFilename();
And in this script you may use all functions without import:
def SomeFuncInFilename():
Hello() # uses function from your scope
If I define a class in a file
~/App_Code/Extensions/MyExtension/MyClass.cs
Is it possible to retrieve the filename by type (or 'MyExtension' part of it) without hard coding it?
var extTypes = getExtensions();
foreach(var extType in extTypes)
{
// something like
var files = extType.GetSourceFiles();
//or maybe asp.net keeps track of types in the dynamically created assembly
var files2 = SomeAspNetClass.WhereDidThisTypeComeFrom(extType);
}
Or inject it to the class in any way?
[ThisFile]
public class MyClass : MyBase
{
private string _file = <thisfile>;
}
This sort of information is only really available in compiled languages (like C#) when you have debugging symbols included. So if you had a Debug build then you could get at this information by examining the current StackFrame.
var stackFrame = new StackFrame(true);
stackFrame.GetFileName()
Of course you probably don't want to have debug builds on your production code, so it may be worth-while looking at alternate ways to achieve whatever it is your trying to do here.
maybe you are looking for this?
Assembly.GetAssembly(typeof(YourTypeHere)).Location
I've written a class in python that I want to wrap into a .net assembly via IronPython and instantiate in a C# application. I've migrated the class to IronPython, created a library assembly and referenced it. Now, how do I actually get an instance of that class?
The class looks (partially) like this:
class PokerCard:
"A card for playing poker, immutable and unique."
def __init__(self, cardName):
The test stub I wrote in C# is:
using System;
namespace pokerapp
{
class Program
{
static void Main(string[] args)
{
var card = new PokerCard(); // I also tried new PokerCard("Ah")
Console.WriteLine(card.ToString());
Console.ReadLine();
}
}
}
What do I have to do in order to instantiate this class in C#?
IronPython classes are not .NET classes. They are instances of IronPython.Runtime.Types.PythonType which is the Python metaclass. This is because Python classes are dynamic and support addition and removal of methods at runtime, things you cannot do with .NET classes.
To use Python classes in C# you will need to use the ObjectOperations class. This class allows you to operate on python types and instances in the semantics of the language itself. e.g. it uses the magic methods when appropriate, auto-promotes integers to longs etc. You can find out more about ObjectOperations by looking at the source or using reflector.
Here is an example. Calculator.py contains a simple class:
class Calculator(object):
def add(self, a, b):
return a + b
You can use it from your pre .NET 4.0 C# code like this:
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
source.Execute(scope); // class object created
object klaz = scope.GetVariable("Calculator"); // get the class object
object instance = op.Call(klaz); // create the instance
object method = op.GetMember(instance, "add"); // get a method
int result = (int)op.Call(method, 4, 5); // call method and get result (9)
You will need to reference the assemblies IronPython.dll, Microsoft.Scripting and Microsoft.Scripting.Core.
C# 4 made this much easier with the new dynamic type.
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
dynamic Calculator = scope.GetVariable("Calculator");
dynamic calc = Calculator();
int result = calc.add(4, 5);
If you are using Visual Studio 2010 or later with NuGet support simply execute this to download and reference the appropriate libraries.
Install-Package IronPython
Now that .Net 4.0 is released and has the dynamic type, this example should be updated. Using the same python file as in m-sharp's original answer:
class Calculator(object):
def add(self, a, b):
return a + b
Here is how you would call it using .Net 4.0:
string scriptPath = "Calculator.py";
ScriptEngine engine = Python.CreateEngine();
engine.SetSearchPaths(new string[] {"Path to your lib's here. EG:", "C:\\Program Files (x86)\\IronPython 2.7.1\\Lib"});
ScriptSource source = engine.CreateScriptSourceFromFile(scriptPath);
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
source.Execute(scope);
dynamic Calculator = scope.GetVariable("Calculator");
dynamic calc = Calculator();
return calc.add(x,y);
Again, you need to add references to IronPython.dll and Microsoft.Scripting.
As you can see, the initial setting up and creating of the source file is the same.
But once the source is succesfully executed, working with the python functions is far easier thanks to the new "dynamic" keyword.
I am updating the above example provided by Clever Human for compiled IronPython classes (dll) instead of IronPython source code in a .py file.
# Compile IronPython calculator class to a dll
clr.CompileModules("calculator.dll", "calculator.py")
C# 4.0 code with the new dynamic type is as follows:
// IRONPYTHONPATH environment variable is not required. Core ironpython dll paths should be part of operating system path.
ScriptEngine pyEngine = Python.CreateEngine();
Assembly myclass = Assembly.LoadFile(Path.GetFullPath("calculator.dll"));
pyEngine.Runtime.LoadAssembly(myclass);
ScriptScope pyScope = pyEngine.Runtime.ImportModule("calculator");
dynamic Calculator = pyScope.GetVariable("Calculator");
dynamic calc = Calculator();
int result = calc.add(4, 5);
References:
Using Compiled Python Classes from .NET/CSharp IP 2.6
Static Compilation of IronPython scripts
I have searched high and low and I am afraid that there does not seem to be much information pertaining to this. I am pretty much certain that no one has devised a way to do this in the clean manner that you would like.
The main reason I think this is a problem is that in order to see the PokerCard type in your C# application you would have to compile your Python code to IL. I don't believe that there are any Python->IL compilers out there.