2011-09-08 9 views
0

私は作成したC#フォームから読み込み、私のwin32アプリケーションに渡そうとしています。問題は、私は常にテキストボックスからテキストを取得する代わりにnull wstringを取得しています。私のC + +プログラムからC#フォームのDLLファイルへの参照を作成します。私はそれを介してデバッグし、それ自体がテキストから値を取得しないことがわかったので、C#の部分に問題があることを知っています。 C#を使用して初めて私は何が間違っているのかわからないので、私は以下のC#コードを貼り付けています。C#のテキストボックスからの読み込みで、Win32のC++プログラムに問題がありますか?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace UpdaterForm 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Close(); 
     } 
     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
      this.Text=textBox1.Text; 
     } 

     public String textbox1 
     { 
      get{ 
        return textBox1.Text; 
      } 
     } 


     public String textbox2 
     { 
      get 
      { 
       return textBox2.Text; 
      } 
     } 

     public String textbox3 
     { 
      get 
      { 
       return textBox3.Text; 
      } 
     } 

    } 
} 





using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Windows.Forms; 

namespace UpdaterForm 
{ 
    public class UpdaterForm 
    { 
     public string PickText() 
     { 
      Form1 form = new Form1(); 
      String text1; 
      String text2; 
      String text3; 

      Application.Run(form); 
      text1 = form.textbox1; 
      text2 = form.textbox2; 
      text3 = form.textbox3; 
      form.Dispose(); 
      string text = text1 + "." + text2 + "." + text3; 
      return text; 
     } 
    } 
} 

は、ここでは、C++のコード

#include <string> 

class UpdaterFormClient 
{ 
private: 

    void* ref; 

    void alloc(); 
    void free(); 
    wchar_t * pick(); 

public: 

    UpdaterFormClient(); 
    ~UpdaterFormClient(); 

    void picker(std::wstring &); 
}; 

あなたが期待するよう

#include <windows.h> 
#include "UpdaterFormClient.h" 
#include <vcclr.h> 

using namespace System; 
using namespace System::Runtime::InteropServices; 

#pragma unmanaged 

UpdaterFormClient::UpdaterFormClient() 
{ 
alloc(); 
} 

UpdaterFormClient::~UpdaterFormClient() 
{ 
free(); 
} 

void UpdaterFormClient::picker(std::wstring &text1) 
{ 
wchar_t *p; 

p = pick(); 
text1 = p; 
delete [] p; 
} 

#pragma managed 
#using <mscorlib.dll> 
#using <..\\..\\UpdaterForm\\UpdaterForm\\bin\\debug\\UpdaterForm.dll> 

void UpdaterFormClient::alloc() 
{ 
GCHandle gch; 
UpdaterForm::UpdaterForm ^obj; 

obj = gcnew UpdaterForm::UpdaterForm(); 
gch = GCHandle::Alloc(obj); 
ref = GCHandle::ToIntPtr(gch).ToPointer(); 

return; 
} 

void UpdaterFormClient::free() 
{ 
IntPtr temp(ref); 
GCHandle gch; 

gch = static_cast<GCHandle>(temp); 
gch.Free(); 
} 

wchar_t * UpdaterFormClient::pick() 
{ 
IntPtr temp(ref); 
String ^text1; 
wchar_t *ret; 
GCHandle gch; 
UpdaterForm::UpdaterForm ^obj; 

gch = static_cast<GCHandle>(temp); 
obj = static_cast<UpdaterForm::UpdaterForm ^>(gch.Target); 
text1 = obj->PickText(); 
ret = new wchar_t[text1->Length + 1]; 

interior_ptr<const wchar_t> p1 = PtrToStringChars(text1); 
pin_ptr<const wchar_t> p2 = p1; 
wcscpy_s(ret, text1->Length + 1, p2); 

return ret; 
} 
+0

何を?どのようにテキストボックス文字列を取得しようとしていますか? –

+0

問題は何ですか?なぜ空の方法のエーカーを歩かなければならないのですか? –

+0

私はちょうど私が私のC++のコードでuはそれがC#テキスト1 = obj-> PickText()のPickText()メソッドを呼び出して表示されますGUI – trailblazer

答えて

0

Windowsプログラムが動作しないのcppファイルです。

あなたは、アプリケーションループApplication.Run(form)を開始し、それが終了した場合、その後、あなたはテキストボックスの値を取得しよう。

これはWindowsプログラムでは実行できません。ウィンドウが閉じ、またはメッセージループが終了するまで

Application.Run(form)がブロックされます。

したがって、テキストボックスを扱うコードをフォームに移動する必要があります。

置きフォーム上のボタン、およびボタンクリックイベントにテキストボックスのコードを置きます。入力された内容を表示するには、MessageBox.Showコールを使用します。

私はここに、この(あなたがC++より深さの一部を説明する必要があります)を証明しようとしました:あなたのC++コードについて

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace UpdaterForm 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      String text1 = textBox1.Text; 
      String text2 = textBox2.Text; 
      String text3 = textBox3.Text; 
      string text = text1 + "." + text2 + "." + text3; 
      MessageBox.Show(text);   
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Windows.Forms; 

namespace UpdaterForm 
{ 
    public class UpdaterForm 
    { 
     public string PickText() 
     { 
      Form1 form = new Form1(); 

      try { 
       Application.Run(form); 
      } finally { 
       form.Dispose(); 
      } 
     } 
    } 
} 
+0

を作ったときに私が得たもののコードを貼り付ける方法について申し訳ありませんが、C++のコードを追加しました;これは公開されており、すべてのテキストを取得するためのすべてのコードが含まれていました。ボタンクリックメソッドに移動したので、テキスト変数をPickTextメソッドに渡すことができます。もし私がそれが問題を解決するだろうと確信できるなら – trailblazer

関連する問題