teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

7

TestData構造体をclassの外に出した

2021/09/29 14:10

投稿

退会済みユーザー
answer CHANGED
@@ -23,15 +23,15 @@
23
23
  using System.IO.MemoryMappedFiles;
24
24
  using System.Runtime.Versioning;
25
25
 
26
+ public struct TestData
27
+ {
28
+ public int X;
29
+ public int Y;
30
+ public int Z;
31
+ }
32
+
26
33
  class Program
27
34
  {
28
- struct TestData
29
- {
30
- public int X;
31
- public int Y;
32
- public int Z;
33
- }
34
-
35
35
  [SupportedOSPlatform("windows")]
36
36
  static void Main(string[] args)
37
37
  {

6

不要コード削除

2021/09/29 14:10

投稿

退会済みユーザー
answer CHANGED
@@ -22,7 +22,6 @@
22
22
  using System;
23
23
  using System.IO.MemoryMappedFiles;
24
24
  using System.Runtime.Versioning;
25
- using System.Text;
26
25
 
27
26
  class Program
28
27
  {

5

C#側もstructにした

2021/09/29 14:04

投稿

退会済みユーザー
answer CHANGED
@@ -26,6 +26,13 @@
26
26
 
27
27
  class Program
28
28
  {
29
+ struct TestData
30
+ {
31
+ public int X;
32
+ public int Y;
33
+ public int Z;
34
+ }
35
+
29
36
  [SupportedOSPlatform("windows")]
30
37
  static void Main(string[] args)
31
38
  {
@@ -34,9 +41,11 @@
34
41
  using (var mmf = MemoryMappedFile.CreateOrOpen("test_memory", length))
35
42
  using (var accessor = mmf.CreateViewAccessor())
36
43
  {
44
+ var testData = new TestData();
45
+ accessor.Read<TestData>(0, out testData);
37
- Console.WriteLine(accessor.ReadInt32(0));
46
+ Console.WriteLine(testData.X);
38
- Console.WriteLine(accessor.ReadInt32(4));
47
+ Console.WriteLine(testData.Y);
39
- Console.WriteLine(accessor.ReadInt32(8));
48
+ Console.WriteLine(testData.Z);
40
49
  }
41
50
  }
42
51
  }

4

リンク追加

2021/09/29 14:02

投稿

退会済みユーザー
answer CHANGED
@@ -1,6 +1,7 @@
1
1
  mmapを使用すれば、APIを使わなくてもメモリマップトファイルを使えます。
2
2
 
3
3
  [mmap --- メモリマップファイル](https://docs.python.org/ja/3/library/mmap.html)
4
+ [PythonでバイナリをあつかうためのTips](https://qiita.com/pashango2/items/5075cb2d9248c7d3b5d4)
4
5
 
5
6
  ```python
6
7
  import mmap

3

もっと簡単なサンプルにした

2021/09/29 13:56

投稿

退会済みユーザー
answer CHANGED
@@ -4,11 +4,14 @@
4
4
 
5
5
  ```python
6
6
  import mmap
7
+ import struct
7
8
 
8
9
  length = 0x100
9
10
  mm = mmap.mmap(-1, length, "test_memory")
10
- mm.write(b"Hello world!")
11
11
 
12
+ xyz = struct.Struct("iii")
13
+ mm.write(xyz.pack(1,2,3))
14
+
12
15
  input('push enter to exit.')
13
16
 
14
17
  mm.close()
@@ -30,21 +33,16 @@
30
33
  using (var mmf = MemoryMappedFile.CreateOrOpen("test_memory", length))
31
34
  using (var accessor = mmf.CreateViewAccessor())
32
35
  {
33
- unsafe
34
- {
35
- byte* pMemory = null;
36
- accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pMemory);
37
- var memory = new Span<byte>(pMemory, length);
38
- var str = Encoding.UTF8.GetString(memory);
39
- Console.WriteLine(str.Trim());
36
+ Console.WriteLine(accessor.ReadInt32(0));
40
- }
37
+ Console.WriteLine(accessor.ReadInt32(4));
38
+ Console.WriteLine(accessor.ReadInt32(8));
41
39
  }
42
40
  }
43
41
  }
44
42
  ```
45
43
  (実行結果)
46
- Hello world!
44
+ 1
45
+ 2
46
+ 3
47
47
 
48
- ![実行結果](cd36fda8a87a219492821e944297bbb2.png)
49
-
50
48
  こうなります。

2

修正

2021/09/29 13:53

投稿

退会済みユーザー
answer CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  mm.close()
15
15
  ```
16
- 上記スクリプトを起動しておいて、次のC#プログラムを実行すると、
16
+ 上記スクリプトを起動しておいて、次のようなC#プログラムを実行すると、
17
17
  ```cs
18
18
  using System;
19
19
  using System.IO.MemoryMappedFiles;

1

修正

2021/09/29 13:38

投稿

退会済みユーザー
answer CHANGED
@@ -25,22 +25,20 @@
25
25
  [SupportedOSPlatform("windows")]
26
26
  static void Main(string[] args)
27
27
  {
28
- var capacity = 0x100;
28
+ var length = 0x100;
29
29
 
30
- using (var mmf = MemoryMappedFile.CreateOrOpen("test_memory", capacity))
30
+ using (var mmf = MemoryMappedFile.CreateOrOpen("test_memory", length))
31
31
  using (var accessor = mmf.CreateViewAccessor())
32
32
  {
33
33
  unsafe
34
34
  {
35
35
  byte* pMemory = null;
36
36
  accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pMemory);
37
- var memory = new Span<byte>(pMemory, capacity);
37
+ var memory = new Span<byte>(pMemory, length);
38
38
  var str = Encoding.UTF8.GetString(memory);
39
39
  Console.WriteLine(str.Trim());
40
40
  }
41
41
  }
42
-
43
- Console.ReadKey();
44
42
  }
45
43
  }
46
44
  ```