前提・実現したいこと
Windowsアプリの開発をしたいと考えております。
Visual StudioでGUIを作成し、開発をしており、
C++をDLL化したライブラリをC#で利用することを実装し、
実装中に文字列の受け渡しができていない状態です。
イメージは、GUIで入力した文字列(C#で実装)で、登録中の文字列を検索(C++)し、一致した場合は、一致した文字列がもつデータを返す(IFでポインタでデータを受け渡ししたい)ことをしたいです。
発生している問題・エラーメッセージ
検索して一致したデータをポインタで返却したのですが、C#側でデータが入っていない状態です。
下記、ソースの★(1)の第2引数でデータが入っておらず、
ソースの★(2)の第2引数では、データが入っています。
※★(2)の第2引数には、memcpyでデータをコピーしています。
C#⇒C++コールの文字列の受け渡しで何かあるのでしょうか。
エラーメッセージなし。
該当のソースコード
C#,C++
1◆ソース1 2using System; 3using System.Collections.Generic; 4using System.ComponentModel; 5using System.Data; 6using System.Drawing; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11 12using System.Runtime.InteropServices; 13 14namespace WinFormsApp_Search 15{ 16 public partial class Form1 : Form 17 { 18 public Form1() 19 { 20 InitializeComponent(); 21 } 22 23 private static char[] strArray; 24 25 // 64bit版のC/C++関数の宣言 26 [DllImport("Library64.dll", EntryPoint = "Dll_BinarySearch")] 27 public extern static void _Dll_BinarySearch(int si_inputNo, string p_str); 28 29 // 現在のプロセスにより32bit/64bit版の関数を呼び分けるラッパーメソッド 30 public static void Exec(Int32 si_No) 31 { 32 if (Environment.Is64BitProcess) 33 { 34 strArray = new char[256]; 35 36 //charをstringに変換 37 string text = new string(strArray); 38 39 _Dll_BinarySearch(si_No, text); //★(1) 40 41 } 42 } 43 44 //出力ボタン押下処理. 45 private void button1_Click(object sender, EventArgs e) 46 { 47 Int32 si_No; 48 bool b_Result; 49 50 //入力値は. 51 b_Result = Int32.TryParse(InputNo.Text, out si_No); 52 53 if( true == b_Result) 54 { 55 Exec(si_No); 56 } 57 } 58 } 59} 60 61◆ソース2 62void Dll_BinarySearch( int si_inputNo, char* p_str) //★(2) 63{ 64 ReadFile* pReadFileObj = NULL; 65 pReadFileObj = new ReadFile; 66 67 //ファイル読み込み. 68 bool b_Result = pReadFileObj->ReadFileData(); 69 70 if (true == b_Result) 71 { 72 pReadFileObj->BinarySearch(si_inputNo, p_str); 73 } 74 75 int test = 0; 76 77} 78 79 80 81
試したこと
C#で文字列操作の実装をいくつか試しました。
補足情報(FW/ツールのバージョンなど)
Visual Studio2017