質問編集履歴

4

ソースコードを修正しました。

2021/03/19 13:20

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -46,9 +46,9 @@
46
46
 
47
47
 
48
48
 
49
- 修正版
50
49
 
50
+
51
- ### C++ dll 側の宣言
51
+ ### C++ dll 側の宣言 ★修正版★
52
52
 
53
53
  extern "C" __declspec(dllexport) long __stdcall func(const char *pStr);
54
54
 

3

解決策を公開

2021/03/19 13:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,45 @@
43
43
 
44
44
 
45
45
  これでなぜ文字列が渡らないのか?
46
+
47
+
48
+
49
+ 修正版
50
+
51
+ ### C++ dll 側の宣言
52
+
53
+ extern "C" __declspec(dllexport) long __stdcall func(const char *pStr);
54
+
55
+
56
+
57
+ ### VBA 側
58
+
59
+ Private Declare PtrSafe Function func Lib "test.dll" (ByVal param As String) As Long
60
+
61
+
62
+
63
+ Sub sub1()
64
+
65
+ Dim long1 As Long
66
+
67
+ Dim str1 As string
68
+
69
+ Dim param As Variant
70
+
71
+
72
+
73
+ long1 = 10000
74
+
75
+ str1 = "abc"
76
+
77
+ param = str1 + "," + str(long1)
78
+
79
+
80
+
81
+ func(param)
82
+
83
+ End Sub
84
+
85
+
86
+
87
+ わけわからんけどこれで解決した。

2

ソースコード追加

2021/03/19 13:18

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,39 @@
7
7
  少しわかってきました。
8
8
 
9
9
  一つ目と二つ目の引数を入れ替えたら、一つ目の引数はちゃんと渡るのに二つ目の引数が壊れるようです。
10
+
11
+
12
+
13
+ ### C++ dll 側の宣言
14
+
15
+ extern "C" __declspec(dllexport) long __stdcall func(const long l1, const char *pStr);
16
+
17
+
18
+
19
+ ### VBA 側
20
+
21
+ Private Declare PtrSafe Function func Lib "test.dll" (ByVal long1 As Long, ByVal str As String) As Long
22
+
23
+
24
+
25
+ Sub sub1()
26
+
27
+ Dim long1 As Long
28
+
29
+ Dim str As Variant
30
+
31
+
32
+
33
+ long1 = 10000
34
+
35
+ str = "abc"
36
+
37
+
38
+
39
+ func(long1, str)
40
+
41
+ End Sub
42
+
43
+
44
+
45
+ これでなぜ文字列が渡らないのか?

1

修正しました。

2021/03/19 12:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,9 @@
1
1
  VBAのLongLongからdllの__int64へ値を渡すと数字が壊れます。
2
2
 
3
3
  なぜでしょうか?
4
+
5
+
6
+
7
+ 少しわかってきました。
8
+
9
+ 一つ目と二つ目の引数を入れ替えたら、一つ目の引数はちゃんと渡るのに二つ目の引数が壊れるようです。