
1、Python调用C动态链接库
Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。
(1)C语言文件:pycall.c
[html] view plain copy
/***gcc -o libpycall.so -shared -fPIC pycall.c*/
#include <stdio.h>
#include <stdlib.h>
int foo(int a, int b)
{
printf("you input %d and %d\n", a, b)
return a+b
}
(2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。
(3)Python调用动态库的文件:pycall.py
[html] view plain copy
import ctypes
ll = ctypes.cdll.LoadLibrary
lib = ll("./libpycall.so")
lib.foo(1, 3)
print '***finish***'
(4)运行结果:
2、Python调用C++(类)动态链接库
需要extern "C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern "C",构建后的动态链接库没有这些函数的符号表。
(1)C++类文件:pycallclass.cpp
[html] view plain copy
#include <iostream>
using namespace std
class TestLib
{
public:
void display()
void display(int a)
}
void TestLib::display() {
cout<<"First display"<<endl
}
void TestLib::display(int a) {
cout<<"Second display:"<<a<<endl
}
extern "C" {
TestLib obj
void display() {
obj.display()
}
void display_int() {
obj.display(2)
}
}
(2)g++编译生成动态库libpycall.so:g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp。
(3)Python调用动态库的文件:pycallclass.py
[html] view plain copy
import ctypes
so = ctypes.cdll.LoadLibrary
lib = so("./libpycallclass.so")
print 'display()'
lib.display()
print 'display(100)'
lib.display_int(100)
(4)运行结果:
3、Python调用C/C++可执行程序
(1)C/C++程序:main.cpp
[html] view plain copy
#include <iostream>
using namespace std
int test()
{
int a = 10, b = 5
return a+b
}
int main()
{
cout<<"---begin---"<<endl
int num = test()
cout<<"num="<<num<<endl
cout<<"---end---"<<endl
}
(2)编译成二进制可执行文件:g++ -o testmain main.cpp。
(3)Python调用程序:main.py
[html] view plain copy
import commands
import os
main = "./testmain"
if os.path.exists(main):
rc, out = commands.getstatusoutput(main)
print 'rc = %d, \nout = %s' % (rc, out)
print '*'*10
f = os.popen(main)
data = f.readlines()
f.close()
print data
print '*'*10
os.system(main)
(4)运行结果:
4、扩展Python(C++为Python编写扩展模块)
所有能被整合或导入到其它python脚本的代码,都可以被称为扩展。可以用Python来写扩展,也可以用C和C++之类的编译型的语言来写扩展。Python在设计之初就考虑到要让模块的导入机制足够抽象。抽象到让使用模块的代码无法了解到模块的具体实现细节。Python的可扩展性具有的优点:方便为语言增加新功能、具有可定制性、代码可以实现复用等。
为 Python 创建扩展需要三个主要的步骤:创建应用程序代码、利用样板来包装代码和编译与测试。
(1)创建应用程序代码
[html] view plain copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fac(int n)
{
if (n <2) return(1)/* 0! == 1! == 1 */
return (n)*fac(n-1)/* n! == n*(n-1)! */
}
char *reverse(char *s)
{
register char t,/* tmp */
*p = s, /* fwd */
*q = (s + (strlen(s) - 1))/* bwd */
while (p <q) /* if p <q */
{
t = *p/* swap &move ptrs */
*p++ = *q
*q-- = t
}
return(s)
}
int main()
{
char s[BUFSIZ]
printf("4! == %d\n", fac(4))
printf("8! == %d\n", fac(8))
printf("12! == %d\n", fac(12))
strcpy(s, "abcdef")
printf("reversing 'abcdef', we get '%s'\n", \
reverse(s))
strcpy(s, "madam")
printf("reversing 'madam', we get '%s'\n", \
reverse(s))
return 0
}
上述代码中有两个函数,一个是递归求阶乘的函数fac();另一个reverse()函数实现了一个简单的字符串反转算法,其主要目的是修改传入的字符串,使其内容完全反转,但不需要申请内存后反着复制的方法。
(2)用样板来包装代码
接口的代码被称为“样板”代码,它是应用程序代码与Python解释器之间进行交互所必不可少的一部分。样板主要分为4步:a、包含Python的头文件;b、为每个模块的每一个函数增加一个型如PyObject* Module_func()的包装函数;c、为每个模块增加一个型如PyMethodDef ModuleMethods[]的数组;d、增加模块初始化函数void initModule()。
1 使用os.system函数运行其他程序2 使用ShellExecute函数运行其他程序
3 使用CreateProcess函数运行其他程序
4 使用ctypes调用kernel32.dll中的函数
1 使用os.system函数运行其他程序
os模块中的system()函数可以方便地运行其他程序或者脚本。其函数原型如下所示。
os.system(command) 其参数含义如下所示。
command 要执行的命令,相当于在Windows的cmd窗口中输入的命令。如果要向程序或者脚本传递参数,可以使用空格分隔程序及多个参数。
以下实例实现通过os.system()函数打开系统的记事本程序。
>>>import os # 使用os.system()函数打开记事本程序 >>>os.system('notepad') 0 # 关闭记事本后的返回值 # 向记事本传递参数,打开python.txt文件 >>>os.system('notepad python.txt')
>>>import os # 使用os.system()函数打开记事本程序 >>>os.system('notepad') 0 # 关闭记事本后的返回值 # 向记事本传递参数,打开python.txt文件 >>>os.system('notepad python.txt')
2 使用ShellExecute函数运行其他程序
除了使用os模块中的os.system()函数以外,还可以使用win32api模块中的ShellExecute()函数。其函数如下所示。
ShellExecute(hwnd, op , file , params , dir , bShow )
其参数含义如下所示。
hwnd:父窗口的句柄,如果没有父窗口,则为0。
op:要进行的 *** 作,为“open”、“print”或者为空。
file:要运行的程序,或者打开的脚本。
arams:要向程序传递的参数,如果打开的为文件,则为空。
dir:程序初始化的目录。
Show:是否显示窗口。
以下实例使用ShellExecute函数运行其他程序。
>>>import win32api # 打开记事本程序,在后台运行,即显示记事本程序的窗口 >>>win32api.ShellExecute(0, 'open', 'notepad.exe', '','',0)
# 打开记事本程序,在前台运行 >>>win32api.ShellExecute(0, 'open', 'notepad.exe', '','',1)
# 向记事本传递参数,打开python.txt >>>win32api.ShellExecute(0, 'open', 'notepad.exe', 'python.txt','',1)
# 在默认浏览器中打开http://www.python.org网站 >>>win32api.ShellExecute(0, 'open', 'http://www.python.org', '','',1)
# 在默认的媒体播放器中播放E:\song.wma >>>win32api.ShellExecute(0, 'open', 'E:\\song.wma', '','',1)
# 运行位于E:\book\code目录中的MessageBox.py脚本 >>>win32api.ShellExecute(0, 'open', 'E:\\book\\code\\MessageBox.py', '','',1)
可以看出,使用ShellExecute函数,就相当于在资源管理器中双击文件图标一样,系统会打开相应的应用程序执行 *** 作。
3 使用CreateProcess函数运行其他程序
为了便于控制通过脚本运行的程序,可以使用win32process模块中的CreateProcess()函数。其函数原型如下所示。
CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles , dwCreationFlags , newEnvironment , currentDirectory , startupinfo )
CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles , dwCreationFlags , newEnvironment , currentDirectory , startupinfo )
其参数含义如下。
appName:可执行的文件名。
commandLine:命令行参数。
rocessAttributes:进程安全属性,如果为None,则为默认的安全属性。
threadAttributes:线程安全属性,如果为None,则为默认的安全属性。
InheritHandles:继承标志。
dwCreationFlags:创建标志。
ewEnvironment:创建进程的环境变量。
currentDirectory:进程的当前目录。
tartupinfo :创建进程的属性。
以下实例使用win32process.CreateProcess函数运行记事本程序。
>>>import win32process >>>win32process.CreateProcess('c:\\windows\\notepad.exe', '', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO()) (, , 280, 3076) # 函数返回进程句柄、线程句柄、进程ID,以及线程ID
>>>import win32process >>>win32process.CreateProcess('c:\\windows\\notepad.exe', '', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO()) (<?XML:NAMESPACE PREFIX = PYHANDLE />, , 280, 3076) # 函数返回进程句柄、线程句柄、进程ID,以及线程ID
有了已创建进程的句柄就可以使用win32process.TerminateProcess函数结束进程,或者使用win32event.WaitForSingleObject等待创建的线程结束。其函数原型分别如下。
TerminateProcess(handle, exitCode) WaitForSingleObject(handle, milliseconds )
对于TerminateProcess参数含义分别如下。
handle:要 *** 作的进程句柄。
exitCode:进程退出代码。
对于WaitForSingleObject参数含义分别如下。
handle:要 *** 作的进程句柄。
milliseconds:等待的时间,如果为?1,则一直等待。
以下实例实现创建进程后并对其进行 *** 作。
>>>import win32process # 打开记事本程序,获得其句柄>>>handle = win32process.CreateProcess('c:\\windows\\notepad.exe', '', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO()) # 使用TerminateProcess函数终止记事本程序>>>win32process.TerminateProcess(handle[0],0) # 导入win32event模块>>>import win32event # 创建进程获得句柄>>>handle = win32process.CreateProcess('c:\\windows\\notepad.exe', '', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO()) # 等待进程结束>>>win32event.WaitForSingleObject(handle[0], -1) 0 # 进程结束的返回值
>>>import win32process # 打开记事本程序,获得其句柄
>>>handle = win32process.CreateProcess('c:\\windows\\notepad.exe', '', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO()) # 使用TerminateProcess函数终止记事本程序
>>>win32process.TerminateProcess(handle[0],0) # 导入win32event模块
>>>import win32event # 创建进程获得句柄
>>>handle = win32process.CreateProcess('c:\\windows\\notepad.exe', '', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO()) # 等待进程结束
>>>win32event.WaitForSingleObject(handle[0], -1) 0 # 进程结束的返回值
4 使用ctypes调用kernel32.dll中的函数
使用ctypes模块可以使Python调用位于动态链接库中的函数。在Python 2.5版中已经包含了ctypes模块。如果使用其他版本的Python,可以到http://python.net/crew/theller/ctypes网站下载安装。ctypes适用于Python 2.3版本及以上。
C语言如何调用python,相关步骤如下:
首先,C语言中调用python,要使用头文件Python.h。
2、接着,定义一个调用python的函数。
相关推荐:《Python教程》
3、函数中,设置python库的路径。
4、然后,初始化python。
5、运行一个python代码,输出How are you。
6、最后,释放python。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)