回答編集履歴
2
DLL環境を追記
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
以下の修正で、こちらの環境(Win10x64, python=3.5.2, DLL=64bitANSIビルド)では正しく動作しました。
|
1
|
+
以下の修正で、こちらの環境(Win10x64, python=3.5.2, DLL=VS2010にて64bitANSIビルド)では正しく動作しました。
|
2
2
|
|
3
3
|
- `class`ではなく`object`を得る
|
4
4
|
|
1
DLL例を追記
test
CHANGED
@@ -5,6 +5,18 @@
|
|
5
5
|
- `cast`する
|
6
6
|
|
7
7
|
```Python
|
8
|
+
|
9
|
+
userdll = windll.LoadLibrary('MyDLL.dll')
|
10
|
+
|
11
|
+
userdll.Sum.restype = c_int
|
12
|
+
|
13
|
+
userdll.Sum.argtypes = [POINTER(c_int), (c_int)]
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
length = c_int(16)
|
18
|
+
|
19
|
+
|
8
20
|
|
9
21
|
#buffer = c_int * 16
|
10
22
|
|
@@ -24,6 +36,64 @@
|
|
24
36
|
|
25
37
|
#p_buffer = <__main__.LP_c_long object at 0x0000016931FBC248>
|
26
38
|
|
39
|
+
|
40
|
+
|
41
|
+
result = userdll.Sum( p_buffer, length)
|
42
|
+
|
43
|
+
print(result)
|
44
|
+
|
27
45
|
```
|
28
46
|
|
47
|
+
|
48
|
+
|
49
|
+
MyDLL.h
|
50
|
+
|
51
|
+
```C++
|
52
|
+
|
53
|
+
#ifdef MYDLL_EXPORTS
|
54
|
+
|
55
|
+
#define MYDLL_API __declspec(dllexport)
|
56
|
+
|
57
|
+
#else
|
58
|
+
|
59
|
+
#define MYDLL_API __declspec(dllimport)
|
60
|
+
|
61
|
+
#endif
|
62
|
+
|
63
|
+
MYDLL_API int WINAPI Sum( int *pn, int count);
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
MyDLL.cpp
|
70
|
+
|
71
|
+
```C++
|
72
|
+
|
73
|
+
#define MYDLL_EXPORTS
|
74
|
+
|
75
|
+
#include "MyDLL.h"
|
76
|
+
|
77
|
+
MYDLL_API int WINAPI Sum( int *pn, int count)
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
int ret = 0;
|
82
|
+
|
83
|
+
for( int i = 0; i < count; i++){
|
84
|
+
|
85
|
+
pn[i] = i+1;
|
86
|
+
|
87
|
+
ret += pn[i];
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
return ret;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
|
98
|
+
|
29
99
|
参考:[Mutate an integer array using ctypes](http://stackoverflow.com/questions/1325518/mutate-an-integer-array-using-ctypes)
|