質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

1回答

915閲覧

[string型配列の内容をMarshall、GCHandleの機能を用いて構造体に格納したい]

Godzilla_null

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2022/05/30 11:48

やりたいこと

String型配列の内容をMarshall、GCHandleを用いてstringだけで構成されている構造体に格納したいです。

以下のようなTest構造体、string型配列inputがあります。このstring型配列inputの内容をMarshall、GCHandleを用いて構造体に格納し、最終的な動作のようになりたいです。

c#

1//Test構造体 2public struct Test 3 { 4 public string x; 5 public string y; 6 public string z; 7 //配列の大きさは2 8 public test1[] test1s; 9 } 10 public struct test1 11 { 12 public string e; 13 } 14//input 15string[] input= new string[5] { "1", "2", "3","4","5" }; 16 17//最終的な動作 18Test.x = "1"; 19Test.y = "2"; 20Test.z = "3"; 21Test.test1[0].e = "4"; 22Test.test1[1].e = "5"; 23

現在ためしたこと

以下のようなソースを作成し、実行しました。結果として、Main関数内の
GCHandle gch = GCHandle.Alloc(a, GCHandleType.Pinned)でエラーが発生しました
(エラー内容はソースの下にあります)。

c#

1 using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.IO; 7using System.Runtime.InteropServices; 8 9 10namespace ConsoleApp2 11{ 12 class Program 13 { 14 [StructLayout(LayoutKind.Sequential)] 15 public struct Test 16 { 17 [MarshalAs(UnmanagedType.BStr)] 18 public string x; 19 [MarshalAs(UnmanagedType.BStr)] 20 public string y; 21 [MarshalAs(UnmanagedType.BStr)] 22 public string z; 23 //配列の大きさは2 24 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] 25 public test1[] test1s; 26 } 27 public struct test1 28 { 29 [MarshalAs(UnmanagedType.BStr)] 30 public string e; 31 } 32 33 static void Main(string[] args) 34 { 35 try 36 { 37 //input,Testの実体生成 38 string[] input = new string[5] { "1", "2", "3","4","5"}; 39 Test test = new Test(); 40 test.test1s = new test1[2]; 41 42 //inputを構造体に格納 43 GCHandle gch = GCHandle.Alloc(input, GCHandleType.Pinned); 44 Test result = (Test)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(Test)); 45 gch.Free(); 46 } 47 catch (Exception e) 48 { 49 Console.WriteLine(e.Message); 50 } 51 52 53 } 54 } 55} 56
//エラー内容 Object contains non-primitive or non-blittable data. (Parameter 'value')

上記のエラーを調べ、UnmanagedTypeを変更するという対処を実施しても解決しなかったです。
例で示した、最終的な動作と同様の結果を得るには、どのように修正すればよろしいでしょうか?
お忙しい中恐縮ですが、どなたかご助言いただけますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KOZ6.0

2022/05/30 12:58 編集

構造体うんぬんは関係ありません。どこでエラーが発生しているか、デバッグしてみましょう。 構造体のコンストラクタに配列を渡すようにすれば事足りるはずです。
guest

回答1

0

無理やりやってみました。

C#

1using System; 2using System.Runtime.InteropServices; 3 4class Program 5{ 6 [StructLayout(LayoutKind.Sequential)] 7 public struct Test 8 { 9 [MarshalAs(UnmanagedType.BStr)] 10 public string x; 11 [MarshalAs(UnmanagedType.BStr)] 12 public string y; 13 [MarshalAs(UnmanagedType.BStr)] 14 public string z; 15 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] 16 public test1[] test1s; 17 } 18 19 [StructLayout(LayoutKind.Sequential)] 20 public struct test1 21 { 22 [MarshalAs(UnmanagedType.BStr)] 23 public string e; 24 } 25 26 static void Main(string[] args) { 27 // BSTR 文字列のアドレスを格納する配列を作成 28 IntPtr[] input = new IntPtr[5]; 29 for (int i = 0; i < input.Length; i++) { 30 input[i] = Marshal.StringToBSTR((i + 1).ToString()); 31 } 32 // 配列を Test 構造体にコピー 33 GCHandle gch = GCHandle.Alloc(input, GCHandleType.Pinned); 34 Test result = (Test)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(Test)); 35 gch.Free(); 36 // 不要になった BSTR 文字列を解放 37 for (int i = 0; i < input.Length; i++) { 38 Marshal.FreeBSTR(input[i]); 39 } 40 // 結果確認 41 Console.WriteLine(result.x); 42 Console.WriteLine(result.y); 43 Console.WriteLine(result.z); 44 Console.WriteLine(result.test1s[0].e); 45 Console.WriteLine(result.test1s[1].e); 46 Console.ReadKey(); 47 48 } 49}

投稿2022/05/30 13:28

編集2022/05/30 19:34
KOZ6.0

総合スコア2622

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問