4

I have the problem loading the DLL file and calling the functions in Python. I have tried a lot of tutorials, but still can't figure out how it works. This is my class to export as DLL file. I use simple C# code.

namespace DemoClassLib
{
    public class cLib
    {
        public int increment(int x)
        {
            return x + 1;
        }
    }
}

After building the C# code, I get the DLL file called "DemoClassLib.dll". I use ctypes to load the DLL file. Everything is okay until now.

from ctypes import *
myDll = WinDLL('D:\\Resources\\DemoClassLib\\bin\\Debug\\DemoClassLib.dll')

Starting from that point, I can't continue. All the commands I have tried are failed.

n = c_int(1)
myDll.increment(n)

It keeps on showing me the errors. How can I call the method "increment()" in Python? And how can I pass the input and retrieve the output from that? I am very new to Python. Can someone help me please? I would be very appreciated if you can provide me the source code tutorial.

1

1 Answer 1

2

You can't do this with ctypes because there is no symbol in the binary called simply "increment", as your increment method is a member of a class. Even if this were C++ the name would be mangled. But with C# you don't even get a mangled name in the symbol table because the code is interpreted by the .NET framework.

If you must for some reason interface with a C# library you may want to consider trying IronPython (http://ironpython.net/) which is Python running on the .NET framework with full access to the CLR. The comment above suggesting exposing a COM interface could also work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.