2011-02-02 96 views
1

我有以下代碼在linux下工作。我想將它移植到Windows
,但我不知道在哪裏尋找。
將基於ctypes的代碼片段從linux移植到windows

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 

在DLL我需要尋找「開放」和「讀」調用的任何想法?

非常感謝

+2

爲什麼不使用Python內置的I/O工具? – delnan 2011-02-02 14:46:28

+0

你爲什麼用libc打開並讀取文件? Python有這樣做的本地設施。 – 2011-02-02 14:56:53

回答

2

These slides表明,這將工作:

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

然後我可以訪問libc.printf,並且libc.fopen,但不openread,雖然libc._openlibc._read可用。也許是調用約定的問題?