Using .NET Assemblies with Peach 2
This is not a common task, but there have been a number of times when I’ve wanted to write something in C#/Microsoft .NET or have had access to a nice library. Happily there is a python to .NET bridge that allows using .NET assemblies just like python classes. These are the steps and software you will need to make it happen.
First, here is the setup I’ll be starting with:
- Windows 7 (32 or 64bit)
- ActiveState Python v2.5 32bit (c:\python25)
- Source version of Peach (from svn) (c:\peach)
Next you will need “Python for .NET” which you can download from here. At the time of writing the binary version was for .NET 2.0 and Python 2.5. I do not recommend compiling from scratch unless you know what your about.
Unzip the archive someplace. For the sake of this article lets unzip it to “c:\pnet”.
You will also want to create your Peach PIT file and place it in this folder along with any python code you might have written.
To use a .NET assembly you can simply use “from/import” as normal. The python patch will be searched just like for a python module. So for example you could say “from System.Reflection import Assembly” and then you have access to the .NET Assembly class!
OKAY – Now that you have your PIT and some code that uses .NET it’s time to run everything. This is were the source version of Peach comes in. The “python.exe” that we got from Python for .NET will load the normal binary Python runtime, but also special code to interact with .NET. This is all hidden, just run peach using this custom executable like normal:
python.exe c:\peach\peach.py mydotnet.xml
And that’s it!
NOTE FOR 64BIT USERS
If your on a 64bit machine you will get an error running this. You must mark python.exe as requiring the 32bit .NET runtime. This is fairly easy assuming you have the Windows SDK.
"c:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\CorFlags.exe" python.exe /32BIT+
