使用Pipes将C#Application中的字符串传递给非托管C ++ DLL

时间:2017-04-09 05:06:19

标签: c# c++ .net dll

我正在尝试使用Pipes将我的C#应用​​程序中的字符串传递到C ++ DLL中。如何使我的C ++ DLL接受textBox1.Text字符串?

DLL是不受管理的。

我希望每次按下C#应用程序上的按钮时,DLL都可能在MessageBox中发送textBox1.Text。

C#代码

private void Button1_Click(object sender, EventArgs e)
        {
            int num = 0;
            using (NamedPipeClientStream stream = new NamedPipeClientStream(".", "execute", PipeDirection.Out))
            {
                redo:
                try
                {
                    stream.Connect(350);
                }
                catch (TimeoutException)
                {
                    if (this.open)
                    {
                        Thread.Sleep(500);
                        try
                        {
                            stream.Connect(500);
                        }
                        catch (TimeoutException)
                        {
                            num++;
                            if (num < 3)
                            {
                                goto redo;
                            }
                            MessageBox.Show("Something happened.");
                            return;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(this.textBox1.Text))
                {
                    try
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write(textBox1.Text);
                            writer.Dispose();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("welp");
                    }
                }
                stream.Dispose();
            }
        }

C ++代码

#define BUFSIZE 1024

    void main() {

            LPTSTR lpszPipename = "\\\\.\\pipe\\execute";
            HANDLE hPipe;
            BOOL flg;
            DWORD dwWrite, dwRead;
            char szServerUpdate[200];
            char szClientUpdate[200];

            hPipe = CreateNamedPipe(
                lpszPipename,             // pipe name 
                PIPE_ACCESS_DUPLEX,       // read/write access 
                PIPE_TYPE_MESSAGE |       // message type pipe 
                PIPE_READMODE_MESSAGE |   // message-read mode 
                PIPE_WAIT,                // blocking mode 
                PIPE_UNLIMITED_INSTANCES, // max. instances  
                BUFSIZE,                  // output buffer size 
                BUFSIZE,                  // input buffer size 
                0,                        // client time-out 
                NULL);                    // default security attribute 



            ConnectNamedPipe(hPipe, NULL);

            //Read from client
            flg = ReadFile(hPipe, szClientUpdate, strlen(szClientUpdate), &dwRead, NULL);

            if (flg) //Read something from the client!!!!
            {

                CString csMsg, csTmp;

                for (int i = 0; i<dwRead; i++) {
                    csTmp.Format("%c", szClientUpdate[i]);
                    csMsg += csTmp;
                }

                string strX(csMsg.operator LPCSTR());

                MessageBox(0, strX, "MessageBox caption", MB_OK);
            }

0 个答案:

没有答案