python调用c的配置文件_python调用c
python一個非常的大的優點就是開發效率高,非常不好的缺點就是執行效率低;然而c語言有個則剛好相反。還有一點python的對源碼的保護做不到,即使你用py2exe,pyinstaller這樣的方法也是很容易被反編譯出來。但是c寫的代碼反編譯的難度就極大地增加。所以如果你寫的代碼里面如果包含了一些敏感的東西,那么你可以把這段代碼使用c來寫。
那么我們是不是可以結合起來使用呢?
答案是完全可以,用c來寫一個動態庫/共享庫,然后在python中調用。
python 的解釋器本身就是用c寫的,所以調用c是很方便的,調用的方法不止一種,我們這里介紹使用ctypes這種方式。
我們會寫一個測試代碼來演示怎么在python中調用c函數,包括python傳整型和字符串參數給c庫,c庫中返回整型和字符串。這個測試將在windows xp平臺+python2.7實施。
接下來我們把要做的事情分成2個階段,用c編寫一個動態庫,在python中調用這個動態庫。
c編寫一個動態庫
使用vc6創建一個空的win32動態庫,添加2個文件
testcdll.c
tsetcdll.def
testcdll.c的內容如下:
#include
#include
int add( int a, int b )
{
return a + b;
}
char* pStr = 0;
int nMax = 128;
char* makestring( )
{
pStr = ( char* )malloc(nMax);
memset( pStr, 0, nMax );
strcpy( pStr, "test is test string" );
return pStr;
}
void delstring( void )
{
if ( 0 != pStr )
{
free( pStr );
pStr = 0;
}
}
char* catstring( char* pinput )
{
if ( 0 == pinput )
{
return 0;
}
if ( 0 == pStr )
{
pStr = ( char* )malloc(nMax);
}
memset( pStr, 0, nMax );
strcpy( pStr, "test is test string--" );
strcat( pStr, pinput );
return pStr;
}
然后編譯,得到后面測試要用到的testcdll.dll
如果你不想用IDE的話, 你可以直接使用命令行來編譯:
cl /LD testcdll.c tsetcdll.def? /link /out:testcdll.dll
調用這個動態庫
因為我們的測試比較簡單所以我們就不寫源碼文件,直接在解釋器中寫測試代碼了。
打開命令行cmd,cd到剛才我們生成dll的文件夾,執行python
到解釋器界面,依次執行下面的命令:
>>> from ctypes import *
>>> hd = cdll.LoadLibrary('FoxLicenseMgr.dll')
>>> hd.add( 2, 3 )
5
>>> hd.makestring.restype = c_char_p
>>> hd.makestring()
'test is test string'
>>> hd.delstring()
1
>>> s1 = 'python'
>>> cs1 = c_char_p( s1 )
>>> hd.catstring(cs1)
12068640
>>> hd.catstring.restype = c_char_p
>>> hd.catstring(cs1)
'test is test string--python'
>>> dir(hd)
['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '_
_getattr__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__modul
e__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__si
zeof__', '__str__', '__subclasshook__', '__weakref__', '_func_flags_', '_func_re
stype_', '_handle', '_name', 'catstring', 'delstring', 'makestring']
>>> exit()
對上面語句稍作解釋,
from ctypes import *
因為我們使用的是ctypes的這種方式,需要導入ctypes。
hd = cdll.LoadLibrary('testcdll.dll')
加載我們之前生成的動態庫,因為我們已經cd到testcdll.dll所在的目錄所以不用加全路徑,否則你需要寫全路徑的。
hd.add( 2, 3 )
直接調用add函數,傳進2個參數,參數不需要額外的轉換,直接傳進去,有些類型是不可以直接傳的,需要做個轉換,這個后面會提到。調用這一句后,就直接輸入結果到控制臺上結果為5
調用返回為字符串的函數的時候,需要設置返回類型,否則就會返回一整型值,這是python調用c函數的默認的返回值,如上面調用catstring的之前沒有設置返回值,結果返回一個數值
>>> hd.catstring(cs1)
12068640
設置返回類型使用下面的語法:
hd.makestring.restype = c_char_p
因為我們的返回類型是char*, 所以使用c_char_p,這個和char*是對應的。其他的python的ctypes的類型和c類型的映射關系如下:
ctypes type
C type
Python type
_Bool
bool (1)
char
1-character string
wchar_t
1-character unicode string
char
int/long
unsigned
char
int/long
short
int/long
unsigned
short
int/long
int
int/long
unsigned
int
int/long
long
int/long
unsigned
long
int/long
__int64 orlonglong
int/long
unsigned
__int64 orunsignedlonglong
int/long
float
float
double
float
long
double
float
char
* (NUL terminated)
string or None
wchar_t
* (NUL terminated)
unicode or None
void
*
int/long or None
調用makestring()來返回一個字符串:
>>> hd.makestring()
'test is test string'
如果傳入的是字符串,也需要做一個轉換
>>> s1 = 'python'
>>> cs1 = c_char_p( s1 )
然后再傳入,因為這個函數的返回的是字符串因此也需要設置返回類型:
>>> hd.catstring.restype = c_char_p
>>> hd.catstring(cs1)
'test is test string--python'
最后使用dir來查看這個hd的都有那些可用的方法。
完。
版權所有,禁止轉載. 如需轉載,請先征得博主的同意,并且表明文章出處,否則按侵權處理.
分享到:
總結
以上是生活随笔為你收集整理的python调用c的配置文件_python调用c的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王者荣耀老夫子怎么玩(如何玩好《王者荣耀
- 下一篇: python文件目录操作方法_Pytho