尽管您熟悉这些概念,但这里是[Python 3.Docs]: ctypes - A foreign function library for Python。
Python 代码在 C 代码运行时轮询内存区域,这意味着多个线程。
这是一个例子。
dll.c:
#include <stdio.h>
#include <Windows.h>
#if defined(_WIN32)
# define DLL_EXPORT __declspec(dllexport)
#else
# define DLL_EXPORT
#endif
#define PRINT_MSG_2XI(ARG0, ARG1) printf("From C - [%s] (%d) - [%s]: ARG0: 0x%016p, ARG1: %d\n", __FILE__, __LINE__, __FUNCTION__, ARG0, ARG1)
typedef struct Srtruct_ {
int value;
int sentinel;
} Struct;
DLL_EXPORT int func0(Struct *ptr) {
int counter = 0;
if (!ptr) {
return -1;
}
while (ptr->sentinel) {
ptr->value++;
counter++;
PRINT_MSG_2XI(ptr, ptr->value);
Sleep(200);
}
return counter;
}
code.py:
#!/usr/bin/env python3
import sys
import ctypes
import time
import threading
DLL_NAME = "./dll.dll"
class Struct(ctypes.Structure):
_fields_ = [
("value", ctypes.c_int),
("sentinel", ctypes.c_int),
]
def thread_func(func, arg0):
ret = func(arg0)
print("\nFrom Python - Function returned {:d}".format(ret))
def main():
dll = ctypes.CDLL(DLL_NAME)
func0 = dll.func0
func0.argtypes = [ctypes.POINTER(Struct)]
func0.restype = ctypes.c_int
data = Struct(30, 1)
t = threading.Thread(target=thread_func, args=(func0, data,))
t.start()
time.sleep(1)
print("From Python - Monitored value: {:d}".format(data.value))
time.sleep(1)
print("From Python - Monitored value: {:d}".format(data.value))
data.sentinel = 0
time.sleep(0.5)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("\nDone.")
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056197585]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***
[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.11
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
[prompt]> dir /b
code.py
dll.c
[prompt]> cl /nologo /DDLL 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.07.03_test0\Scripts\python.exe" code.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 31
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 32
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 33
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 34
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 35
From Python - Monitored value: 35
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 36
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 37
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 38
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 39
From C - [dll.c] (27) - [func0]: ARG0: 0x00000152EB84CE90, ARG1: 40
From Python - Monitored value: 40
From Python - Function returned 10
Done.