Alain
Alain

Reputation: 323

Porting ctypes-based code snippet from linux to windows

I have the following code that works under linux. I want to port it to Windows
but i have no idea where to look for.

import os
import sys
import ctypes 
import ctypes.util
MAX_CHARS = 1000    # maximum number of characters to read

if __name__ == "__main__":
    libc = ctypes.CDLL("libc.so.6") # LINUX
    fd = libc.open(sys.argv[0], os.O_RDONLY)
    buffer = " " * MAX_CHARS
    num_chars_read = libc.read(fd, buffer, MAX_CHARS)
    print buffer[:num_chars_read]
    libc.close(fd)

    # ALL OF THIS GIVES WRONG DLL's 'UNDER WINDOWS
    #libc = ctypes.CDLL(ctypes.util.find_library('c'))
    #libc =  ctypes.windll.kernel32 # WINDOWS
    #libc =  ctypes.windll.user32 # WINDOWS
    #libc =  ctypes.windll.shell32 # WINDOWS
    #libc =  ctypes.windll.gdi32 # WINDOWS
#libc =  ctypes.cdll.msvcrt # WINDOWS

Any idea in which DLL i need to look for 'open' and 'read' calls?

Many thanks

Upvotes: 3

Views: 1365

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 375584

These slides indicate that this will work:

>>> from ctypes import *
>>> libc = cdll.msvcrt

I can then access libc.printf, and libc.fopen, but not open or read, though libc._open and libc._read are available. Perhaps an issue with calling conventions?

Upvotes: 4

Related Questions