About the author

Related Articles

4 Comments

  1. 1
    Solidak@email.null'

    Solidak

    For others who came here and couldn’t get this to work on Python 3.x, the reason is that you must pass a bytes string (b”whatever”) not a regular python literal string.

    So this code works well on my OSX High Sierra:

    from ctypes import *
    
    libc = CDLL('/usr/lib/libc.dylib')
    message_string = 'Hello worldn'
    libc.printf(b"Testing: hello world")
    
    Reply
  2. 2
    Bella@email.null'

    Bella

    I don’t know how ctypes works in windows systems, but when I was using ubuntu system, I write like this : libc=CDLL(“libc.so.6”)
    So ,do you have something wrong with your libs?

    Reply
  3. 3
    David

    David Heffernan

    Why doesn’t it print Hello World?

    It does in my Python (ActiveState, 2.6), when run from the console:

    >>> from ctypes import *
    >>> libc = CDLL('msvcrt.dll')
    >>> libc.printf("Hello worldn")
    Hello world
    12
    

    Reply
  4. 4
    Greg

    Greg Hewgill

    The C printf() function itself is defined to return the number of characters printed to the output. This is the value that Python receives when you call libc.printf().

    The ctypes tutorial provides information on why the output from printf() may not work within your Python REPL (my psychic debugging skills indicate that you’re running the Windows GUI IDLE).

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2017 SolutionMmyself.com