2011-02-02 98 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可用。也许是调用约定的问题?