[IronPython] Using a c library as a dll or assembly

Curt Hagenlocher curt at hagenlocher.org
Tue Aug 5 17:46:58 CEST 2008


It's much easier to use C# (or other .NET) code from IronPython than vice
versa. In order to call your Python code from a C# app, you'll need to use
the hosting interface.  This has changed substantially between IronPython
1.x and IronPython 2.0, so you'll need to decide which you're targeting
before you invest too much time into it.

A good resource for this and other IronPython-related stuff is the
IronPython cookbook.  Go to http://www.ironpython.info/index.php/Contents and
scroll down to "Embedding" for this particular topic.

On Tue, Aug 5, 2008 at 7:49 AM, Anders Elfgren <
anders.elfgren at avalanchestudios.se> wrote:

>  Thanks a bunch!
>
> Seems I've gotten past my current problems.
>
> My next question, and I think I've heard something about this, but I'm not
> sure...
> Now that I've got my python code hooked up to ironpython, there shouldn't
> be anything that's python anymore, so to speak, right? I mean, when I import
> a python module using ironpython, it's all .net.
>
> My question is: What is the best way to access these classes in C#? Is it
> possible? I want to build up a gui containing data from the classes I've
> instantiated, and it would be nicer to do that in C# using VS.
>
> Another (unrelated) question: What's the best way to debug IronPython
> programs? "print-debugging" is not very effective. (maybe this deserves a
> mail thread of its own)
>
> /Anders
>
>  ------------------------------
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Curt Hagenlocher
> *Sent:* den 1 augusti 2008 16:39
> *To:* Discussion of IronPython
> *Subject:* Re: [IronPython] Using a c library as a dll or assembly
>
>  Here's how I think I would do it.  Add a class definition to your C
> source that looks something like this:
>
>  using namespace System::Runtime::InteropServices;
>
> ref class Exported {
> public:
>     static int AdfHash(String^ str) {
>         const char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
>         int result = ::AdfHash(str2);
>         Marshal::FreeHGlobal((System::IntPtr)(void*)str2);
>         return result;
>     }
> }
> You should then be able to import the Exported class from the assembly and
> access AdfHash through it.  You'll need to write a similar wrapper for every
> function you want to export.
>
> Disclaimer: I've never actually used either Managed C++ or C++/CLI except
> to write small test cases.
>
> On Fri, Aug 1, 2008 at 4:25 AM, Anders Elfgren <
> anders.elfgren at avalanchestudios.se> wrote:
>
>>  Hi, thanks for the comments!
>> In this case, I'm not trying to access a class, just a simple function
>> that takes a string (char* ) and returns an int. I was hoping that would
>> make it easier.
>>
>> I've compiled the dll with /CLR and I can load it like this:
>>
>> import clr
>> clr.AddReference("System.Windows.Forms")
>> clr.LoadAssemblyByName("AdfDll_R.dll")
>>
>> However, I still can't access anything inside it. Do I need to do
>> something more? (if I try to load a dll that doesn't exist I get an error,
>> so I'm assuming that there's not a problem with my dll). I've tried adding
>> an import but I just get No module named AdfDll_R.
>>
>>
>> The function is declared as:
>>
>> #ifdef __cplusplus
>> extern "C" {
>> #endif
>>
>> uint32   AdfHash(const char *str);
>>
>> }
>>
>> But when I try to call it, it doesn't work:
>> NameError: name 'AdfHash' not defined
>>
>>
>>
>> I'm exporting the functions using a def file that looks (pretty much) like
>> this:
>> LIBRARY   AdfDll_R
>> EXPORTS
>>
>> ; from Adf.h
>>    AdfHash
>>
>> I'm using IronPython 1.1.1, but if it helps I'll be glad to change to
>> another version.
>>
>> /Anders
>>
>>  ------------------------------
>> *From:* users-bounces at lists.ironpython.com [mailto:
>> users-bounces at lists.ironpython.com] *On Behalf Of *Curt Hagenlocher
>> *Sent:* den 31 juli 2008 20:21
>> *To:* Discussion of IronPython
>> *Subject:* Re: [IronPython] Using a c library as a dll or assembly
>>
>>    It wouldn't be sufficient to recompile with /clr.  When you do that,
>> your classes don't automatically become managed classes.  You would still
>> need to either modify your classes or to create wrappers using C++/CLI (
>> http://en.wikipedia.org/wiki/C%2B%2B/CLI).  In order for a C++ class to
>> be visible to IronPython, it would need to be declared as a "ref class" --
>> you can see more about that at the referenced Wikipedia article.
>>
>>
>> On Thu, Jul 31, 2008 at 9:22 AM, Anders Elfgren <
>> anders.elfgren at avalanchestudios.se> wrote:
>>
>>>  Hi,
>>>
>>> First let me say that I'm new to Iron Python, but at least I've read this
>>> in the FAQ.. :)
>>>
>>> Q: How do I build and call into *PYD* libraries?
>>> A: *IronPython* does not support using PYDs built for CPython since they
>>> leverage implementation details of CPython.  You can get a similar effect
>>> for new "*PYD*"s you would like to implement by writing them in C# or VB
>>> and building a DLL for .NET.
>>>
>>> That said, I've got some questions to see if what I'm doing is possible
>>> anyway.
>>>
>>> We've got a c library of which most of the code has also been written in
>>> Python. We want to create an editor using .net forms which we can use to
>>> edit some files, and to do that we need some data structures that exist in
>>> this library.
>>>
>>> My current idea is to interface against the python library using
>>> IronPython, and then accessing the datastructures through that in C#
>>> (although if absolutely necessary, we could write the editor in IronPython
>>> too). The thing that is standing against me now is that one function that
>>> exists a small C dll. Since IronPython can't access this (unless this has
>>> changed since the FAQ was written?), I was thinking that maybe I can compile
>>> the dll using the /clr flag and thus get a .net assembly. Would I then be
>>> able to use the function?
>>>
>>> The Python code just tries to import it:
>>> import AvHash
>>>
>>> And then use it..
>>> def AdfHash(str):
>>>     return AvHash.HashString(str)
>>>
>>>
>>> At the import statement, this error is raised:
>>> Traceback (most recent call last):
>>>   File X:\ctg\libs\ADF\main\common\modeditor.ipy, line 20, in Initialize
>>>   File , line 0, in __import__##4
>>>   File X:\ctg\libs\ADF\main\common\AdfLib.py, line 1, in Initialize
>>>   File , line 0, in __import__##4
>>> ImportError: No module named AvHash
>>>
>>>
>>>
>>> Or is there some other solution? I've just started researching how to
>>> interface between C/C#/Python/IronPython so there may well be things I
>>> haven't heard of...
>>> I hope I've made my problem clear, thanks for any responses. :)
>>>
>>> /Anders
>>>
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080805/7d1be84d/attachment.html>


More information about the Ironpython-users mailing list