Python calls the dynamic link library tutorial in Windows and Linux, pythonlinux

Source: Internet
Author: User

Python calls the dynamic link library tutorial in Windows and Linux, pythonlinux

Call the dynamic library (. so) in Linux)

1. The linuxany. c code is as follows:

  #include "stdio.h"  void display(char* msg){    printf("%s\n",msg);  }     int add(int a,int b){    return a+b;  }

2. Compile the c code and generate the executable. so file in Python.
(1) gcc-c linuxany. c will generate a linuxany. o file
(2) gcc-shared linuxany. c-o linuxany. so, a linuxany. so file is generated.

3. Call in Python

#! /Usr/bin/python from ctypes import * import OS // The parameter is generated. the absolute path of the so file is libtest = cdll. loadLibrary (OS. getcwd () + '/linuxany. so ') // call print libtest directly with the method name. display ('Hello, I am linuxany.com ') print libtest. add (2,2010)

4. Running result

Hello,I am linuxany.com2012 


Python calls dll in Windows

If you want to call dll in python, you need to use the ctypes module. import the ctypes module at the beginning of the program.

Due to different call conventions, the methods for calling dll in python are also different. There are two main call rules: cdecl and stdcal. There are other call conventions about their differences, other materials available

The following describes how to call stdcal:

Method 1:

import ctypesdll = ctypes.windll.LoadLibrary( 'test.dll' )

Method 2:

import ctypesdll = ctypes.WinDll( 'test.dll' )


Cdecl call method:

1.

Import ctypesdll = ctypes. cdll. loadLibrary ('test. dll ') # Note: Generally, test is used in linux. o file, you can also use the following method: # dll = ctypes. cdll. loadLibrary ('test. O ')

2.

import ctypesdll = ctypes.CDll( 'test.dll' )

Let's look at an example. first compile a dll

The export function is as follows:

# define ADD_EXPORT Q_DECL_EXPORTextern "C" ADD_EXPORT int addnum(int num1,int num2){return num1+num2;}extern "C" ADD_EXPORT void get_path(char *path){memcpy(path,"hello",sizeof("hello"));}

Here cdecl is used

The script is as follows:

Dll = ctypes. CDLL ("add. dll ") add = dll. addnumadd. argtypes = [ctypes. c_int, ctypes. c_int] # parameter type add. restypes = ctypes. c_int # print add (1, 2) get_path = dll. get_pathget_path.argtypes = [ctypes. c_char_p] path = create_string_buffer (100) get_path (path) print path. value

The result is as follows:

We can see two results. The first is calculation, and the second is to bring back a parameter.

Of course, we can also easily use windows dll and provide many interfaces.

GetSystemDirectory = windll.kernel32.GetSystemDirectoryAbuf = create_string_buffer(100)GetSystemDirectory(buf,100)print buf.valueMessageBox = windll.user32.MessageBoxWMessageBox(None, u"Hello World", u"Hi", 0)

The running result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.