麻烦从c#中的文本框读取到win32 C ++程序?

时间:2011-09-08 19:49:51

标签: c# winapi

我试图从我创建的C#表单中读取并在我的win 32应用程序上传递它。麻烦的是我总是得到一个null wstring而不是从textbox.i创建一个文件从我的c ++程序创建一个C#form dll文件的参考。我知道麻烦在C#部分,因为我通过它进行调试,发现它本身永远不会从文本中获取值。 自从我第一次使用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)
        {
            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;
}

1 个答案:

答案 0 :(得分: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();
            } 
        } 
    } 
}