2011-04-25 5 views
20

저수준 목적을 위해, 정수로 주어진 임의의 주소에서 ctypes 포인터를 구성해야합니다. 예를 들어 :ctypes : 임의의 정수로부터 포인터를 구성하십시오.

INTP = ctypes.POINTER(ctypes.c_int) 
p = INTP(0x12345678) # i *know* this is the address 

그러나 이러한 모든 시도가

TypeError: expected c_long instead of int 

결과는 내가 이것을 극복하기 위해 할 수있는 일이 있나요? 어떤 사람이 내가 왜 이것을 필요로하는지 궁금해 할 때, 에서 struct를 추출하여 win32file wrapped API와 함께 ctypes-exposed 함수를 통합했다.
-Tomer

덕분에,

답변

31

당신은 ctypes.cast(addr, type)를 사용할 수 있습니다. 내가 알고있는 객체를 통해 주소를 획득하도록 예제를 확장 것이다 입증하기 :

INTP = ctypes.POINTER(ctypes.c_int) 
num = ctypes.c_int(42) 
addr = ctypes.addressof(num) 
print 'address:', addr, type(addr) 
ptr = ctypes.cast(addr, INTP) 
print 'pointer:', ptr 
print 'value:', ptr[0] 

출력 :

트릭을 수행
address: 4301122528 <type 'int'> 
pointer: <__main__.LP_c_int object at 0x1005decb0> 
value: 42 
+0

, 감사합니다! – sebulba

+1

@sebulba : 그렇다면 대답을 올바르게 표시하십시오. :) – vines