1、运行collect工程中的Main.c,得到collect.dll;
2、运行collect.py,调用Main.c中的数组;
Main.c代码如下:
#include <stdio.h>
#include <time.h>
#define DLLEXPORT extern _declspec(dllexport)
DLLEXPORT int main(unsigned short* buffer, unsigned short row, unsigned short col)
{
unsigned short n, i, row_, col_;
for (n=0;n<12;n++)
{
for (i=0;i<row*col;i++)
{
row_ = i / col;
col_ = i % col;
if (row_ == 1 && col_ > 0 && col_ < 3)
{
buffer[i] = n;
}
else
{
buffer[i] = 0;
}
}
Sleep(1000);
}
return 0;
}
collect.py代码如下:
import time
import numpy as np
from threading import Thread
from ctypes import *
row = 3
col = 4
buffer = np.ones(row*col, dtype="uint16")
dll = CDLL('x64\\Debug\\collect.dll')
main = dll.main
main.argtype = [POINTER(c_ushort), c_ushort, c_ushort]
long_run_thread = Thread(target = main, args = (buffer.ctypes.data_as(POINTER(c_ushort)), row, col))
long_run_thread.start()
time.sleep(0.1)
for i in range(12):
print(buffer)
time.sleep(1)
运行结果: