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 <
[EMAIL PROTECTED]> 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:* [EMAIL PROTECTED] [mailto:
> [EMAIL PROTECTED] *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 <
> [EMAIL PROTECTED]> 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
>> [email protected]
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
> _______________________________________________
> Users mailing list
> [email protected]
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to