Skip to content

Commit

Permalink
example of python ctypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tlming16 committed Aug 25, 2020
1 parent b8f5c50 commit 65d9316
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
20 changes: 20 additions & 0 deletions config/call_stack.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* dot -Tpng call_stack.dot -o call_stack.png
* tlming16
* all wrongs reserved
*/
digraph compile {
edge [fontname="FangSong"];
node [shape=box, fontname="FangSong" size="20,20"];
a[label="函数参数,\n rdi,rsi,rdx,rcx,r8,r9 \n 参数个数多余6个 \n 顺序\n nth\n ...\n 8th\n 7th"];
b[label="函数返回地址\n "];
c[label="上一帧栈%rbp地址\n push %rbp \n movq %rsp,%rbp"];
d[label="局部变量地址\n 起始地址为-n(%rbp) n为第一个变量的size\n 注意内存对齐"];
e[label="---"];
f[label="%esp 地址"];
a->b;
b->c;
c->d;
d->e;
e->f;

}
Binary file added config/call_stack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions config/mysql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# mysql (https://www.cnblogs.com/sfencs-hcy/p/9769505.html)

- my.ini

```
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
bind-address = 0.0.0.0
port = 3306
skip_grant_tables
# 设置mysql的安装目录
basedir=D:\\Applications\\mysql-8.0.21-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:\\Applications\\mysql-8.0.21-winx64\\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
```

- mysqld --initialize --console

- 记住密码
- mysqld --install mysql
- net start mysql
- mysql -uroot -p

- 密码
- 改密码
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
5 changes: 5 additions & 0 deletions python/util/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// gcc -shared -fPIC -o libexample.dll example.c

int add(int x,int y){
return x+y;
}
7 changes: 7 additions & 0 deletions python/util/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
# coding:utf-8
from ctypes import *
if __name__ =='__main__':
lib_name ="./libexample.dll"
lib= cdll.LoadLibrary(lib_name)
print(lib.add(c_int(10),c_int(10)));
Binary file added python/util/libexample.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions python/util/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# python 调用 C语言动态库

- c语言编写基本函数,打包成动态链接库
- 依赖 ctypes 引入动态库

# c代码

```c
// example.c
//gcc -shared -fPIC -o libexample.dll example.c
int add(int x,int y){
return x+y;
}
```
# 编译
```
gcc -shared -fPIC -o libexample.dll example.c
```
# python 调用
```python
#!/usr/bin/env python3
# coding:utf-8
from ctypes import *
if __name__ =='__main__':
lib_name ="./libexample.dll"
lib= cdll.LoadLibrary(lib_name)
print(lib.add(c_int(10),c_int(10)));
```

0 comments on commit 65d9316

Please sign in to comment.