|
C语言文件如下:
============================
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/hdreg.h>
#include <cstring>
int main()
{
unsigned char* str1;
unsigned char* sdasn2();
str1=sdasn2();
printf("%s\n",str1);
}
unsigned char* sdasn2()
{
int fd;
unsigned char* str;
//unsigned char* str;
struct hd_driveid hdinfo;
fd = open("/dev/sda" , O_RDONLY);
printf("%d %p\n", fd, &hdinfo);
if(ioctl(fd, HDIO_GET_IDENTITY , &hdinfo) == -1){
perror("Found error when get hdinfo:");
}
else{
str=hdinfo.serial_no;
}
return str;
}
============================
然后,自然是把这个C文件编成动态链接库:
============================
g++ -c -fPIC sdasn2.cpp
g++ -shared sdasn2.o -o sdasn2.so
============================
进入python:
============================
>>> from ctypes import *
>>> import os
>>> sdasn2 = cdll.LoadLibrary(os.getcwd() + '/sdasn2.so')
>>> print sdasn2.main()
3 0xbfeee198
VB20e5d89d-3b72775e
0
============================
问题如下:print sdasn2.main()这句换成print sdasn2.sdasn2()出错,提示:
============================
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/ctypes/__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
File "/usr/lib/python2.6/ctypes/__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /home/sqsowen/sdasn2.so: undefined symbol: sdasn2
=============================
还有,最后一行输出0,怎么消除掉阿?
参考资料:http://coolshell.cn/?p=671 |
|