Generate Python callable dll from source code

1. Functions and calls without parameters

1.1. Create a project

  • First create a c language or C++ project
  • Run hello world
    [External link picture transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the picture and upload it directly (img-oGHEycon-1686290398095)(null)]

1.2. Modify the source code

  • Create a class myDLL
    The class exported by the dynamic link library needs to add the keyword __declspec(dllexport), indicating that the function will be exported , otherwise the lib file cannot be generated.
    Create a header file and a source file
  • [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-MzcIXNrb-1686290398108)(null)]
  • myDLL.h
class Mymath {
	static void print_hello();
};

  • myDLL.cpp
#define DLLEXPORT extern "C" __declspec(dllexport)

#include "myDLL.h"
#include <iostream>

//打印hello world
DLLEXPORT void print_hello() {
	std::cout << "Hello World!" << std::endl;
}

1.3. Compile

  • Then modify the build type, change it to dll
    project properties - general - configuration type - dynamic library, change it to dll
  • [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-9lkZghWw-1686290398164)(null)]
  • then generate again
  • [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-DGVCpVeL-1686290398151)(null)]
  • It can be seen that the dll file has been generated in the release folder
    [the transfer of the external link image failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-fSvi8byp-1686290398069)(null)]
  • This dll is a 64-bit dll, which can be used for subsequent calls of other programs

1.4. Call

  • We use Python to call
from ctypes import *
lib = CDLL("Project8.dll")
result = lib.print_hello()

As you can see, this is already a complete project that calls

hello world normally

2. Function compilation and calling with parameters

2.1. Create a new .h header file

pycall.h

### //test.h
#pragma once
class Mymath {
	int sum(int, int);
	int sub(int, int);
};
#pragma once

2.2. Create a new cpp file

pycall.cpp

### #define DLLEXPORT extern "C" __declspec(dllexport)
#include"pycall.h"
//两数相加
DLLEXPORT int  sum(int a, int b) {
	return a + b;
}
//两数相减
DLLEXPORT int sub(int a, int b) {
	return a - b;
}

2.3. Compile

  • [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-rIYOSULi-1686290398190)(null)]

2.4. Call

  • [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-8VPmX5yQ-1686290398082)(null)]

3. Error reporting

  • AttributeError: function ‘printHelloWorld’ not found

  • The dll file to be called does not contain this function

  • It should be that when defining the function in the .cpp file, DLLEXPORT was not added before to declare it for external calls, so the generated dll file does not have this function, so an error will occur [External link picture transfer failed, the source site may have anti-leech
    links mechanism, it is recommended to save the picture and upload it directly (img-llhpeK49-1686290398137)(null)]

  • OSError: [WinError 126] The specified module could not be found.

  • [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-eRTB3MQ2-1686290398177)(null)]

  • The problem that the specified module cannot be found is usually that the program wants to load a certain DLL file, but if the DLL file does not exist, an error will be reported.

  • You can hit a breakpoint in PyCharm to see which dll file is missing

  • The number of digits of the dll file does not match.
    If the dll is 32-bit and your own Python is 64-bit, then it must not work normally.

  • Try loading dx.dll with ctypes.windll.dx instead of CDLL

  • DLL depends on other DLLs
    If dx.dll depends on other DLLs, and the other DLLs are not found, this error will also be generated

Match
If the dll is 32-bit and your own Python is 64-bit, then it must not work normally

  • Try loading dx.dll with ctypes.windll.dx instead of CDLL

  • DLL depends on other DLLs
    If dx.dll depends on other DLLs, and the other DLLs are not found, this error will also be generated

Guess you like

Origin blog.csdn.net/u014723479/article/details/131126189