2011-09-08 31 views
0

我想從我創建的C#表單讀取,並將其傳遞給我的win 32應用程序。麻煩是我總是得到一個空的wstring而不是從textbox.text創建文本從我的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 &); 
}; 

cpp文件如您所願

#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

那你的C++代碼?你如何試圖檢索文本框字符串? –

+0

很難看出問題是什麼?爲什麼我們必須通過幾英畝的空方法? –

+0

我添加了C++代碼,對於我剛剛粘貼代碼的方法感到抱歉,當我在我的C++代碼中創建gui – trailblazer

回答

0

Windows程序不起作用。

您啓動一個應用程序循環Application.Run(form),然後,當它結束時,您嘗試獲取文本框值。

您不能在Windows程序中執行此操作。

Application.Run(form)將阻塞,直到窗口關閉或消息循環結束。

因此,您需要將處理您的文本框的代碼移動到您的表單中。

在窗體上放置一個按鈕,並將文本框代碼放入按鈕單擊事件中。使用MessageBox.Show調用來顯示輸入內容。

我試圖在這裏展示這個(你需要解釋更深入的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#text1 = obj-> PickText()的PickText()方法。 ;這是公開的,幷包含檢索所有文本的所有代碼。由於您將其移至按鈕單擊方法,我們可以將文本變量傳遞給PickText方法。如果我能確定它會解決問題 – trailblazer