글

라벨이 ctypes인 게시물 표시

python ctypes LoadLibrary로 windows dll 로드 및 함수 호출 예제

이미지
본 포스트는 python에서 ctypes의 LoadLibrary로 windows dll을 로드 하는 방법과 예제를 싣고 있다. ctypes는 c 호환 데이터 형식을 제공하고 dll, 공유 라이브러리에서 함수를 호출할 수 있도록 해주는 python 라이브러리다. [ https://docs.python.org/3.7/library/ctypes.html ] python에서 windows dll 로드 방법 import ctypes ctypes.windll.LoadLibrary(name) ctypes.oledll.LoadLibrary(name) 윈도우용 dll을 위의 두 API를 사용한다. 일반적인 dll을 로드 할 경우 WinDLL 인스턴스 의 LoadLibary를 사용하고, OLE dll을 로드 할 경우 OleDLL 인스턴스 의 LoadLibary를 사용한다. dll 로드시 아래와 아래와 같은 에러가 발생할 경우가 있다. 이런 경우는 64 bits python 환경에서 win32로 빌드된 dll을 호출하거나, 32 bits python 환경에서 win64로 빌드된 dll을 호출해 발생하는 문제인 경우가 많다. OSError: [WinError 193] %1은(는) 올바른 Win32 응용 프로그램이 아닙니다. python에서 dll의 함수 호출 방법 python에서 windows dll의 함수 호출은 아래 dll 소스의 api 호출 예제를 통해 알아보자. window dll 소스 코드 int xl_square(int x) { return x*x; } int xl_square_out(int x, int *result) { if (result) { *result = x*x; return 0; } return -1; } void xl_callback(callback cb...