Windows DLL in Python

xiaoxiao2021-03-06  111

Use C / C to extend Python on Windows

How to use Windows DLL in Python

First download the CTYPES module

Description at http://starship.python.net/crew/theller/ctypes/reference.html

Here is an example I wrote.

First write a DLL, the source code is as follows

#include

#include

/ / ---------------------------------------

/ / Demo use DLL

/ / ---------------------------------------

INT apientry __declspec (dllexport) addnum1 (int A) {

Return (A 1);

}

Int apientry __declspec (dllexport) AddNum10 (INT A, CHAR * CA) {

Sprintf (CA, "% D", A 10);

Return (A 10);

}

Compile with BCC 5.5

BCC -TWD DLL.C

This is called in Python as follows:

From CTypes IMPORT *

Filename = "dll.dll"

Func = WINDLL.LOADLIBRARY (FileName)

A = c_int (2) #Convert to C Type

Ret = c_int ()

Ret = func.addnum1 (a)

Print "Value of A:", RET

B = C_INT (20)

Str = c_char_p ("") #char point

RET = Func.AddNum10 (B, STR)

Print "Return Value IS:", RET

Print "The string is:", Str.Value

# WINDLL.FREELIBRARY (FUNC)

Output result

Value of A: 3

Return Value IS: 30

THE STRING IS: 30

转载请注明原文地址:https://www.9cbs.com/read-103249.html

New Post(0)