回答編集履歴
7
TestData構造体をclassの外に出した
test
CHANGED
@@ -48,23 +48,23 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
+
public struct TestData
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
public int X;
|
56
|
+
|
57
|
+
public int Y;
|
58
|
+
|
59
|
+
public int Z;
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
51
65
|
class Program
|
52
66
|
|
53
67
|
{
|
54
|
-
|
55
|
-
struct TestData
|
56
|
-
|
57
|
-
{
|
58
|
-
|
59
|
-
public int X;
|
60
|
-
|
61
|
-
public int Y;
|
62
|
-
|
63
|
-
public int Z;
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
68
|
|
69
69
|
[SupportedOSPlatform("windows")]
|
70
70
|
|
6
不要コード削除
test
CHANGED
@@ -45,8 +45,6 @@
|
|
45
45
|
using System.IO.MemoryMappedFiles;
|
46
46
|
|
47
47
|
using System.Runtime.Versioning;
|
48
|
-
|
49
|
-
using System.Text;
|
50
48
|
|
51
49
|
|
52
50
|
|
5
C#側もstructにした
test
CHANGED
@@ -54,6 +54,20 @@
|
|
54
54
|
|
55
55
|
{
|
56
56
|
|
57
|
+
struct TestData
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
public int X;
|
62
|
+
|
63
|
+
public int Y;
|
64
|
+
|
65
|
+
public int Z;
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
57
71
|
[SupportedOSPlatform("windows")]
|
58
72
|
|
59
73
|
static void Main(string[] args)
|
@@ -70,11 +84,15 @@
|
|
70
84
|
|
71
85
|
{
|
72
86
|
|
73
|
-
|
87
|
+
var testData = new TestData();
|
74
88
|
|
75
|
-
|
89
|
+
accessor.Read<TestData>(0, out testData);
|
76
90
|
|
77
|
-
Console.WriteLine(
|
91
|
+
Console.WriteLine(testData.X);
|
92
|
+
|
93
|
+
Console.WriteLine(testData.Y);
|
94
|
+
|
95
|
+
Console.WriteLine(testData.Z);
|
78
96
|
|
79
97
|
}
|
80
98
|
|
4
リンク追加
test
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
[mmap --- メモリマップファイル](https://docs.python.org/ja/3/library/mmap.html)
|
6
|
+
|
7
|
+
[PythonでバイナリをあつかうためのTips](https://qiita.com/pashango2/items/5075cb2d9248c7d3b5d4)
|
6
8
|
|
7
9
|
|
8
10
|
|
3
もっと簡単なサンプルにした
test
CHANGED
@@ -10,13 +10,19 @@
|
|
10
10
|
|
11
11
|
import mmap
|
12
12
|
|
13
|
+
import struct
|
14
|
+
|
13
15
|
|
14
16
|
|
15
17
|
length = 0x100
|
16
18
|
|
17
19
|
mm = mmap.mmap(-1, length, "test_memory")
|
18
20
|
|
21
|
+
|
22
|
+
|
19
|
-
|
23
|
+
xyz = struct.Struct("iii")
|
24
|
+
|
25
|
+
mm.write(xyz.pack(1,2,3))
|
20
26
|
|
21
27
|
|
22
28
|
|
@@ -62,21 +68,11 @@
|
|
62
68
|
|
63
69
|
{
|
64
70
|
|
65
|
-
|
71
|
+
Console.WriteLine(accessor.ReadInt32(0));
|
66
72
|
|
67
|
-
|
73
|
+
Console.WriteLine(accessor.ReadInt32(4));
|
68
74
|
|
69
|
-
byte* pMemory = null;
|
70
|
-
|
71
|
-
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pMemory);
|
72
|
-
|
73
|
-
var memory = new Span<byte>(pMemory, length);
|
74
|
-
|
75
|
-
var str = Encoding.UTF8.GetString(memory);
|
76
|
-
|
77
|
-
|
75
|
+
Console.WriteLine(accessor.ReadInt32(8));
|
78
|
-
|
79
|
-
}
|
80
76
|
|
81
77
|
}
|
82
78
|
|
@@ -88,11 +84,11 @@
|
|
88
84
|
|
89
85
|
(実行結果)
|
90
86
|
|
91
|
-
|
87
|
+
1
|
92
88
|
|
89
|
+
2
|
93
90
|
|
94
|
-
|
95
|
-
|
91
|
+
3
|
96
92
|
|
97
93
|
|
98
94
|
|
2
修正
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
```
|
30
30
|
|
31
|
-
上記スクリプトを起動しておいて、次のC#プログラムを実行すると、
|
31
|
+
上記スクリプトを起動しておいて、次のようなC#プログラムを実行すると、
|
32
32
|
|
33
33
|
```cs
|
34
34
|
|
1
修正
test
CHANGED
@@ -52,11 +52,11 @@
|
|
52
52
|
|
53
53
|
{
|
54
54
|
|
55
|
-
var
|
55
|
+
var length = 0x100;
|
56
56
|
|
57
57
|
|
58
58
|
|
59
|
-
using (var mmf = MemoryMappedFile.CreateOrOpen("test_memory",
|
59
|
+
using (var mmf = MemoryMappedFile.CreateOrOpen("test_memory", length))
|
60
60
|
|
61
61
|
using (var accessor = mmf.CreateViewAccessor())
|
62
62
|
|
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pMemory);
|
72
72
|
|
73
|
-
var memory = new Span<byte>(pMemory,
|
73
|
+
var memory = new Span<byte>(pMemory, length);
|
74
74
|
|
75
75
|
var str = Encoding.UTF8.GetString(memory);
|
76
76
|
|
@@ -79,10 +79,6 @@
|
|
79
79
|
}
|
80
80
|
|
81
81
|
}
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
Console.ReadKey();
|
86
82
|
|
87
83
|
}
|
88
84
|
|