作为
[Python 3]: ctypes - A foreign function library for Python
国家,你必须
不
字符
当然可以忽略,但随后可能会出现意外(字符串被无声截断)。
~5个
一行行代码,粘贴整个东西:
:
#include <stdlib.h>
#if defined(_WIN32)
# define DLL_EXPORT __declspec(dllexport)
#else
# define DLL_EXPORT
#endif
#define LEN 5
typedef struct CharPtrWrapperTag {
int len;
char *data;
} CharPtrWrapper;
DLL_EXPORT CharPtrWrapper *get() {
CharPtrWrapper *ret = malloc(sizeof(CharPtrWrapper));
ret->len = LEN;
ret->data = malloc(LEN * sizeof(char));
ret->data[0] = 'A';
ret->data[1] = 'B';
ret->data[2] = 0;
ret->data[3] = 'C';
ret->data[4] = 'D';
return ret;
}
DLL_EXPORT void release(CharPtrWrapper *pWrap) {
if (pWrap) {
free(pWrap->data);
pWrap->data = NULL;
pWrap->len = 0;
free(pWrap);
}
}
:
#!/usr/bin/env python3
import sys
import ctypes
DLL_NAME = "./dll.dll"
CharPtr = ctypes.POINTER(ctypes.c_char)
class CharPtrWrapper(ctypes.Structure):
_fields_ = [
("len", ctypes.c_int),
("data", CharPtr),
]
CharPtrWrapperPtr = ctypes.POINTER(CharPtrWrapper)
def main():
dll = ctypes.CDLL(DLL_NAME)
get = dll.get
get.restype = CharPtrWrapperPtr
release = dll.release
release.argtypes = [CharPtrWrapperPtr]
wrap_ptr = get()
wrap = wrap_ptr.contents
print("{:}\n Len: {:d}".format(wrap, wrap.len))
for idx in range(wrap.len):
print(" {:d}: {:}".format(idx, wrap.data[idx]))
s = ctypes.cast(wrap.data, ctypes.c_char_p).value[:wrap.len]
print("\nctypes.c_char_p cast: {:}".format(s))
CharArr = ctypes.c_char * wrap.len
char_arr = CharArr(*wrap.data[:wrap.len])
print("CharArr: {:}".format(char_arr.raw))
release(wrap_ptr)
print("\nDone.")
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
输出
:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q055103298]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64
[prompt]> dir /b
code.py
dll.c
[prompt]> cl /nologo /DDLL /MD dll.c /link /NOLOGO /DLL /OUT:dll.dll
dll.c
Creating library dll.lib and object dll.exp
[prompt]> dir /b
code.py
dll.c
dll.dll
dll.exp
dll.lib
dll.obj
[prompt]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" code.py
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
<__main__.CharPtrWrapper object at 0x000001279250D248>
Len: 5
0: b'A'
1: b'B'
2: b'\x00'
3: b'C'
4: b'D'
ctypes.c_char_p cast: b'AB'
CharArr: b'AB\x00CD'
Done.