Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# Assemblies from Python via pythonnet

I am using Windows 7, 64-bit. I have managed to download and install pythonnet, so

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form

works fine. I have also downloaded and compiled/run a C# application which creates lots of assemblies. The application in question is ARDrone-Control-.NET.

How can I use the generated DLL files from Python (and not just the built-in C# classes).

Since I have never used C# (which is why I want to use the library from Python), I'd be happy to clarify the question.

like image 759
Petter Avatar asked Jun 13 '11 16:06

Petter


People also ask

How do you use C programming?

We can use either a plain text editor (like Notepad) or the IDE's built-in editor. The source code must follow the syntax of the C programming language. After the source file is complete, save it as a *.c file. We'll need a compiler to compile our source code.

Why #is used in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more. It actually adds code to the source file before the final compilation.

Is C language still used?

The C programming language has been alive and kicking since 1972, and it still reigns as one of the fundamental building blocks of our software-studded world.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


1 Answers

Just to provide another method:

import sys
sys.path.append("C:\Path\to\your\assemblies")

clr.AddReference('MyAssembly')
from MyAssembly import MyClass

MyClass.does_something()

This assumes that in the C:\Path\to\your\assemblies folder you have a MyAssembly.dll file.

So the 'trick' is that you have to add your assemblies folder to the sys.path before clr.AddReference.

like image 59
John Avatar answered Oct 15 '22 18:10

John