PYTHON directly calls DLL to achieve interaction with C++

Recently, I am using python to call C++ programs, because C++ programs are ready-made and efficient. It's not difficult to listen to, and calling the dll directly should be enough. When I started to do it, I really hit a wall everywhere.

At the beginning, some information from Baidu said that the interaction between python and C++ is divided into the following categories:

1. Write an interface program by yourself to realize the mapping from C++ to python, so as to realize the extension of python.

2. Using the toolkit SWIG, this method is really simple and won my heart. It took a long time on this method and finally abandoned it.

3. Generate a dll file for the C++ program, and python uses its own package ctypes to realize data transfer, and finally succeeds.

I tried the first two methods and ended in failure, because when C++ interacts with python, I need to pass pointers. I don't understand how to pass data in the above two methods. There are other methods such as using Boost, numpy, etc. I really can't wait and give up at the beginning. Now let's talk about the effective method of pro-testing, directly on the code:

my.cpp

#define EXPORT_MY_DLL
#include "my.h"

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

MY_API void Add(int *L, int *R)
{
	for (int i = 0; i < 2; i++)
	{
		L[i] += 5 * R[i];
		R[i] += 1;
	}  
}
my.h
#ifdef EXPORT_MY_DLL
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
extern "C"
{  
	MY_API void Add(int *, int *);
}

About how to create a DLL file, I won't go into details, and it is very convenient to directly Baidu.

I want to call the Add function with python, and I can create it by writing the interface in the program. See http://www.jb51.net/article/52513.htm for details

Next write a Python program:

import numpy as np
import ctypes
from ctypes import *

a = np.ones(2).astype(np.int)
b = np.ones(2).astype(np.int)

a_ctypes_ptr = cast(a.ctypes.data, POINTER(c_int)) #Convert to ctypes, where the converted can be directly converted to int* in c language using ctypes, and then used in c
b_ctypes_ptr = cast(b.ctypes.data, POINTER(c_int)) #Convert to ctypes, where the converted can be directly converted to int* in c language using ctypes, and then used in c


dll = cdll.LoadLibrary ('mys.dll')
dll.Add(a_ctypes_ptr,b_ctypes_ptr)

for i in range(2):
    print(a_ctypes_ptr[i])

Yes, that's it! You can directly use ctypes to convert python data into pointers in C, and then pass them to C for use in C, and this method does not need to pass the pointers back, the array is modified directly in the program, and you want to observe the matrix Element, operate directly on the converted data!

So exciting!


Learning at SWIG :

https://blog.csdn.net/u014689510/article/details/50393341

https://zhuanlan.zhihu.com/p/27851893

https://blog.csdn.net/ustczhang/article/details/78147215


Learning of Ctypes :

https://blog.csdn.net/thesby/article/details/76283807

https://docs.python.org/3/library/ctypes.html

https://blog.csdn.net/linda1000/article/details/12623527


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685866&siteId=291194637