Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Call DLL functions from Python
petercour
petercour

Posted on

     

Call DLL functions from Python

Do you hav experience with C programming?

You can call C functions fromPython, with ctypes. What is ctypes?

ctypes is a foreign function library for Python. It provides C compatible data types, and allowscalling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

This how to do it in Python 2.x (if you still have that):

#!/usr/bin/pythonfrom ctypes import *libc = cdll.LoadLibrary("/lib/x86_64-linux-gnu/libc.so.6")printf = libc.printfprintf("hello world\n")
Enter fullscreen modeExit fullscreen mode

For Python 3.x programs (yes the difference is one character)

#!/usr/bin/python3from ctypes import *libc = cdll.LoadLibrary("/lib/x86_64-linux-gnu/libc.so.6")printf = libc.printfprintf(b"hello world\n")
Enter fullscreen modeExit fullscreen mode

Make sure the path to your shared library (libc.so.6) is right.

Chances are it's on another location. On Windows or Mac its different path and name. Otherwise it's very basic, and this should work for any C library.

Related links:

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
advik2612 profile image
Advik
  • Joined

Omg TSYM

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More frompetercour

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp