Python calls a dynamic link library (.dll) (vs2019+pycharm)

tags: C language  c++  pycharm

Because the experiment needs to send a fixed byte size data, and Python is not suitable for the bottom layer, use C/C ++ as the underlying processing data to record the entire process. It should be noted that the DLL file is just a dynamic link library under the Windows system. Under the Linux or Mac system is another type of file. This article also uses VS2019 to compile and generate a dynamic link library under the Windows system.

1. Package the C/C ++ function into a dynamic library (.dll)

First give the teaching document given by VS official website (I refer to the official website, but there is no encapsulation function in the DLL file after generating DLL files according to the process. Maybe it is me. You can try it according to the teaching):
https://docs.microsoft.com/zh-cn/cpp/build/dlls-in-visual-cpp?view=msvc-170

The whole process is introduced below:
1. Create a project to compile DLL files, introduce more details on the official website above, so I will not introduce it here. Note to create a new project in VS, the project selects a dynamic link library (DLL)

2. Add a CPP file to the source file and write a function. For example, I simply wrote a plus method (ADD) function:

#include "pch.h"      // Import this file is a compilation reminder, no error will be reported without adding
#include<iostream>   

#define MATHLIBRARY_API extern "C" __declspec(dllexport)   // The main is to join this macro definition

 MATHLIBRARY_API int add(int x, int y)   // Add the defined macro name in front of your function, you can add the function to the dynamic link library when compiled into a DLL file
{
	return x + y;
}

3. Select the platform according to your own system and need to generate a solution.My Python is 64 -bit, so choose X64. If not match, call the DLL file on the Python side to report an error.

4. If it is successful, a folder of the X64 will be generated under the path of the project. There will be a DLL file in the debug of the folder. Let's teach you how to check whether the DLL file has our function.
Open VS2019 Developer PowerShell

Enter the following command to see what functions in the DLL file

Dumpbin /Exports Absolute Path (C:\Users\source\repos\Mathlibrary\x64\Debug\Mathlibrary.dll)

2.Python calls dynamic link library (DLL)

Python calls the DLL file to use the CTYPES library. This library does not need to be downloaded, and Python3 has already brought it. Copy the generated DLL file to the Python project, be sure to be in the same directory as the Python file called the dll file.

import ctypes
from ctypes import *

mydll = CDLL("./demo.dll")    # Must be there ./ Otherwise
result = mydll.add(1, 2)      
print(result)

The above code realizes the ADD function in the C/C ++ dynamic link library. It is necessary to note that the variable transmitted by the ADD function in C ++ needs to be defined as an INT type. There is no need to convert the type in Python. However, if the variable that is transmitted by the ADD function is Float type, then the Python terminal needs to convert the value to C_FLOAT type:

import ctypes
from ctypes import *

mydll = CDLL("./demo.dll")    # Must be there ./ Otherwise
result = mydll.add(ctypes.c_float(1.345), ctypes.c_float(2.232))
print(result)

  So use CTYPES to call the DLL dynamic link library. Pay attention to the data type of the C/C ++ function to pass the variableEssence Here are the data types corresponding to Ctypes, C/C ++, and Python.

If C/C ++ returns is the type of pointer, it is necessary to tell the type of data returned by python before calling the function

import ctypes
from ctypes import *

mydll = CDLL("./demo.dll")    # Must be there ./ Otherwise
mydll.couculate.restype = POINTER(c_int)   #Stream data type returned is an integer pointer
result = mydll.add(ctypes.c_float(1.345), ctypes.c_float(2.232))
# If the returned number of pointers, it is transformed into List, [0: 8] indicates 8 data in the array
# result = list(mydll.add(ctypes.c_float(1.345), ctypes.c_float(2.232))[0:8])
print(result)

For the time being, I am also very big in the conversion of the data type, and I may not be clear, wrong or omitted. I hope everyone guidance.

Intelligent Recommendation

C++ dynamic link library and static link library lib and dll generated in VS description python calls dll

C++ dynamic link library and static link library What are dynamic link libraries and static link libraries? They are all ways to share code. Dynamic linking uses dynamic link library. Dynamic linking ...

VS2019 creates a dynamic link library and calls

Click on the new project, select the dynamic link library, and click Next Take a name, select the path, click to create Declare the function in the PCH.H file, add the following code Implement functio...

[Python calls DLL library] Python uses CTypes library to call DLL dynamic link library

Python can call C ++ DLL dynamic link libraries to achieve the purpose of interacting with C ++ language. Python calls C ++ compiled DLL dynamic link libraries, we need to use the CTypes library. Ctyp...

Labview calls C# dynamic link library dll

One, VS2017 making dll New -> Project   Select "Class Library (.NET Framework)" and the project is named | "MyLib"   determine Create an add function in the Class1 cla...

Java JNI calls Dll dynamic link library

Since I recently read the information on the reader in the project and never touched these things using java to call the underlying dynamic link library Dll, I searched the Internet for a lot of infor...

More Recommendation

Node calls dynamic link library (.dll)

First, the system windows10 Second, the application environment 1. Install node 2. Install node-gyp 3. Install windows-build-tools 4. Set the python environment variable, find the python2.7 installati...

Ctypes calls dynamic link library (.dll/.so)

Windows In the system, create a new dynamic link library project in Visual Studio. This article named itlib4pyAnd then newlib4py.h with lib4py.cpp Two files. Linux In the system, create a new folder a...

C# calls C++ dynamic link library dll

In the process, two methods were found to solve the problem: one is the dll library created by unmanaged C++, which needs to be called by static method. This method can't be directly referenced in the...

Unity calls dynamic link library dll and so

[001] Unity calls the dynamic link library dll and so  ApolloAR 1. PC platform (Note: Environment VS2017+Unity2017.15f1) 1)C# DLL 1. Create a class library file, pay attention to choose .NET can ...

LabVIEW calls DLL dynamic link library

LabVIEW calls DLL dynamic link library Some data operations such as processing between multiple arrays, rain flow counting, Kalman filtering using a graphical programming language such as labview are ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top