How can I use a DLL file from Python?

前端 未结 7 1482
迷失自我
迷失自我 2020-11-22 16:06

What is the easiest way to use a DLL file from within Python?

Specifically, how can this be done without writing any additional wr

7条回答
  •  醉酒成梦
    2020-11-22 16:12

    This page has a very simple example of calling functions from a DLL file.

    Paraphrasing the details here for completeness:

    It's very easy to call a DLL function in Python. I have a self-made DLL file with two functions: add and sub which take two arguments.

    add(a, b) returns addition of two numbers
    sub(a, b) returns substraction of two numbers

    The name of the DLL file will be "demo.dll"

    Program:

    from ctypes import*
    # give location of dll
    mydll = cdll.LoadLibrary("C:\\demo.dll")
    result1= mydll.add(10,1)
    result2= mydll.sub(10,1)
    print "Addition value:"+result1
    print "Substraction:"+result2

    Output:

    Addition value:11
    Substraction:9

提交回复
热议问题