I want to run some c# function from python.
I know this can be done with
os.system("myapp.exe")
and other similar calls, but this would create a new process.
I would like to have the c# code run from the same process which is running the python code.
How can this be done?
One way I thought of is compiling the c# to dll, and calling its functions from there, but I still haven't been able to make this way work.
Is there a better way?
You can use the NuGet package UnmanagedExports for this.1
For Python ctypes compatible code, you need to add the attribute
[DllExport("<function name>", CallingConvention = CallingConvention.Cdecl)]
There is no way of doing this without modifying the C# assembly.
From Python code:
import ctypes
a = ctypes.cdll.LoadLibrary(<path to dll>)
You can now use the object a to call methods in the C# assembly that have been prefixed with the DllExport Attribute.
Information taken from the official website of UnmanagedExports.
[1] You can always use COM, however, this is much harder to do, and involves
much more restructuring of the C# assembly.
Is the use of a different interpreter all together on the table for consideration?
You could use IronPython, which would allow you to import a dll as you would a normal python package, and embed your C# classes into your python script.
clr.AddReference('mylib.dll') # contains namespace MyLib
import MyLib
Related
I had a dll written in C. So for a C# application, I wrote a .cs wrapper that worked in calling the dll functions..
Now I have a C# dll and need to call the functions using python 3.4 (LoadLibrary). What would be the preferred route of doing this
1) convert the C# dll code into C and make a new c-style dll (with exports).
2) Create some kind of wrapper in python that allows it to see the C# dll functions as-is? ( if possible)
3) Both Python for .Net and IronPython don't seem to be actively maintained anymore (The windows installer shows python 2.7). Is there a better alternative?
Take a look at this question on stack overflow.It tells how to use ctypes.It describes the ctypes.WinDLL command for including the required dll.You can set up prototype and parameters for the desired function call using ctypes.WINFUNCTYPE.
I have the c++ source code of functionality which is appealing to me.
What effort/work is involved/required in order to either reference this from a .net application or build this code as a .net assembly (preferably c#)?
This is my first attempt at porting code, so please breakdown your answer for me step by step.
There are several ways of doing it.
PInvoke
Create C++/CLI wrapper around your C++ native code (make static library out of C++ native code) and C++/CLI generated assembly can be easily utilized in .net application.
COM, i.e using interop (which is difficult among all the options)
In my suggestion easiest way is to use option 2, but you need to take care of proper marshaling.
Solution A:
If you have the source code, then compile the CPP program as a DLL file.
Use P/Invoke
Solution B (if the functionality you want is in a static library):
Create a stub function caller and compile THAT as a DLL
Same as solution A.2
I have been looking for a way to call a function written in a C# EXE project (VS 2008) from a C++ DLL project. I can include the C# code as part of the C++ project itself if that is possible. All that I have been able to find is calling the C# DLL from C++.
Ultimately I want to call C# code from VB6 but I ask the question this way because I don't believe the later way is possible without an intermediate step.
Thanks,
Ian
Ultimately I want to call C# code from VB6 but I ask the question this way because I don't believe the later way is possible without an intermediate step.
You can register the C# classes to be visible to COM, and then call them directly from C++ or VB6.
For details, see the Example COM Class on MSDN, as well as Interoperability.
You can compile your c++ project with the /CLR option and then call C# from within it. To do that you need to include vcclr.h and then add a using statement for each dll you need to call from.
#include <vcclr.h>
#using <System.dll>
I'm coming from C++ world. I have a small project in C#, I didn't found a way and I don't even know if it's possible to take a DLL like the XML-RPC.net and compile it so static lib , and then use it with my project DLL, link it statically .
You shouldn't need to do this (you usually just ship dependencies in the same directory as your entry point assembly) but you can merge it into your application using ILmerge if you really want which is equivalent to static linking in the CLR.
http://www.microsoft.com/download/en/details.aspx?id=17630
How would I export functions defined in a C# class library, while enabling them to be imported to and called from an unmanaged C++ application/DLL ?
Strictly speaking, you can't just export functions as you would in a classic .dll, as .NET .dll's aren't really .dll's at all. Your only three options are:
Use managed C++
Expose your C# classes as COM objects and consume them from your C++ code
Host the .NET runtime in your C++ project and interact with your C# classes through that.
Your C++ Apllication would have to start by hosting the CLR. There is nothing special required from the .NET DLL.
You would not. Not supported. You can pretty much only export COM objects from a C# class librarly.
You could also make a C++ wrapper for your C# library - a simple Managed C++ DLL that would import .NET methods and export them natively. This adds an additional layer, but it might be useful if C# library is a must have.
Another option is to tweak the compiled assembly to export the functions. A C# compiler cannot do this, but it takes a slight change of MSIL code to get the things done.
Have a look at this article - there're some links on how the stuff works, and a tool to automate it (though I haven't tried it myself).