回答編集履歴
3
修正
test
CHANGED
@@ -46,15 +46,7 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
-
/// <summary>
|
50
|
-
|
51
|
-
//
|
49
|
+
//Dataを逆並びにするだけの関数
|
52
|
-
|
53
|
-
/// </summary>
|
54
|
-
|
55
|
-
/// <param name="stIn">入力側構造体</param>
|
56
|
-
|
57
|
-
/// <param name="stOut">出力側構造体</param>
|
58
50
|
|
59
51
|
[DllImport("DllTest.dll", CallingConvention = CallingConvention.StdCall)]
|
60
52
|
|
2
サンプル追記
test
CHANGED
@@ -5,3 +5,179 @@
|
|
5
5
|
[uint配列を含む構造体の共有メモリへのマッピング](https://teratail.com/questions/302213)
|
6
6
|
|
7
7
|
[ネイティブ相互運用性](https://docs.microsoft.com/ja-jp/dotnet/standard/native-interop/)
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
苦戦されているようなので、簡単なサンプルを追記します。
|
16
|
+
|
17
|
+
```CS
|
18
|
+
|
19
|
+
using System;
|
20
|
+
|
21
|
+
using System.Diagnostics;
|
22
|
+
|
23
|
+
using System.Runtime.InteropServices;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
namespace ConsoleApp1
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
class Program
|
32
|
+
|
33
|
+
{
|
34
|
+
|
35
|
+
[StructLayout(LayoutKind.Sequential)]
|
36
|
+
|
37
|
+
public struct STRUCT_CS
|
38
|
+
|
39
|
+
{
|
40
|
+
|
41
|
+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
|
42
|
+
|
43
|
+
public byte[] Data;
|
44
|
+
|
45
|
+
};
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
/// <summary>
|
50
|
+
|
51
|
+
/// Dataを逆並びにするだけの関数。
|
52
|
+
|
53
|
+
/// </summary>
|
54
|
+
|
55
|
+
/// <param name="stIn">入力側構造体</param>
|
56
|
+
|
57
|
+
/// <param name="stOut">出力側構造体</param>
|
58
|
+
|
59
|
+
[DllImport("DllTest.dll", CallingConvention = CallingConvention.StdCall)]
|
60
|
+
|
61
|
+
static extern void Test([In]ref STRUCT_CS stIn, [Out]out STRUCT_CS stOut);
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
static void Main(string[] args)
|
66
|
+
|
67
|
+
{
|
68
|
+
|
69
|
+
try
|
70
|
+
|
71
|
+
{
|
72
|
+
|
73
|
+
var stIn = new STRUCT_CS();
|
74
|
+
|
75
|
+
var stOut = new STRUCT_CS();
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
//入力側構造体に0-9をセット
|
80
|
+
|
81
|
+
stIn.Data = new byte[10];
|
82
|
+
|
83
|
+
for (var i = 0; i < stIn.Data.Length; i++)
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
stIn.Data[i] = (byte)i;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
//入力側の内容を逆並びにする
|
94
|
+
|
95
|
+
Test(ref stIn, out stOut);
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
//出力側構造体の中身を表示
|
100
|
+
|
101
|
+
for (var i = 0; i < stOut.Data.Length; i++)
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
Console.WriteLine(stOut.Data[i]);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
catch (Exception ex)
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
Debug.WriteLine(ex.ToString());
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
Console.ReadLine();
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
(実行結果)
|
132
|
+
|
133
|
+
9
|
134
|
+
|
135
|
+
8
|
136
|
+
|
137
|
+
7
|
138
|
+
|
139
|
+
6
|
140
|
+
|
141
|
+
5
|
142
|
+
|
143
|
+
4
|
144
|
+
|
145
|
+
3
|
146
|
+
|
147
|
+
2
|
148
|
+
|
149
|
+
1
|
150
|
+
|
151
|
+
0
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
一応DLL側も
|
158
|
+
|
159
|
+
```C++
|
160
|
+
|
161
|
+
typedef struct {
|
162
|
+
|
163
|
+
unsigned char Data[10];
|
164
|
+
|
165
|
+
} STRUCT_CPP;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
extern void __stdcall Test(STRUCT_CPP* pstIn, STRUCT_CPP* pstOut)
|
170
|
+
|
171
|
+
{
|
172
|
+
|
173
|
+
for (int i=0; i<10; i++)
|
174
|
+
|
175
|
+
{
|
176
|
+
|
177
|
+
pstOut->Data[9-i] = pstIn->Data[i];
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
```
|
1
追記
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
構造体の宣言がC++側と合っていないですね。
|
2
|
+
|
1
3
|
こちらが参考になるかもしれません。
|
2
4
|
|
3
5
|
[uint配列を含む構造体の共有メモリへのマッピング](https://teratail.com/questions/302213)
|
6
|
+
|
7
|
+
[ネイティブ相互運用性](https://docs.microsoft.com/ja-jp/dotnet/standard/native-interop/)
|