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.
Related
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
I'm noob in C# and I already searched on the web. Anyway I'm still not sure about this and I do not have the total control of the code I have to implement, this is why I would like to be sure I needn't make any dll. I have a C++ file with a method, and I want to call this method from C# code. I just added "extern C" to the function.
When I just add the .h and .cpp files to the C# project they aren't detected. And of course, when I try to add it as reference, it doesn't work.
So do I absolutly have to make a dll ?
Your options for accessing the C++ code from C#:
Compile C++ as unmanaged DLL and access using p/invoke. This requires the C++ code be exposed using a C style API.
Compile C++ as unmanaged DLL and access using COM. This requires that you wrap your C++ in as COM objects.
Compile C++ as mixed/mode C++/CLI assembly and access the assembly as a managed reference. This requires that you wrap the original C++ as managed C++ ref classes.
All of these options, by necessity, involve the creation of another module/assembly. You cannot link the C++ code directly into your C# assembly.
You might like to try using the PInvoke Interop Assistant to generate the C# necessary to interact with the DLL via Platform Invoke. Be aware that this is imperfect though so YMMV.
Another alternative, if you have the knowledge and patience, is to make a COM component out of your native C++ DLL and consume that from C# by using the Type Library Importer to create a managed wrapper.
You won't be able to interact with .cpp/.h files since you need at least a binary object (assembly) for C# to interact with and C# won't generate any binaries from .cpp/.h. That's on the subject about adding these files as references to the project.
As for the argument that you don't have control over the code - well, don't make a DLL out of the actual .cpp/.h, but make your own thin DLL that has a wrapper object that just includes the headers, calls whatever method you would be calling and links to the appropriate .o files or .lib or whatever you have. If the interface changes you would just changed your thing wrapper which should be easy.
I am porting an application from Windows to Ubuntu written in C#. The application is used to view video from a capture card. I have had to write a C# wrapper for a Dll in windows to access the API for the card, and have written a Wrapper for the corresponding .so in Mono for Ubuntu.
To build and run a C example app for the .so in ubuntu I had to pass "-rdynamic" to gcc to make the application run correctly, else there would be errors such as "Undefined symbol: XMoveWindow". When I try to run my C# app I get "undefined symbol: XvShmCreateImage". I suspect that I need to pass something like "-rdynamic" to the compiler in mono, but I can't find an equivalent command for mono. Is there an equivalent command, and if so what is it?
Thanks in advance
Andrew
This likely means that your .so library is miscompiled (it's not referencing the X11 libs).
You should either recompile this .so lib correctly or you can P/Invoke some function inside libX11 and libXshm before invoking this lib (the functions need not exist, but in that case you need to catch the exceptions, this is needed to load the libraries in the process so that the loader later can find the symbols exported by them).
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>
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).