PipeClient c# to PipeServer c++ : Deserializing confusion (need help)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: PipeClient c# to PipeServer c++ : Deserializing confusion (need help)

  1. #1
    Join Date
    Mar 2014
    Posts
    4

    PipeClient c# to PipeServer c++ : Deserializing confusion (need help)

    C#

    Code:
        MacroTempKey MTP = new MacroTempKey();
        string[] Line = new string[3];
        object[] TempKey = new object[5];
    
        private void button2_Click(object sender, EventArgs e)
            {
                label1.Focus();
        
                if (null != Line)
                {
                    for(int i = 0; i < 3; ++i)
                    {
                        if (Line[i] != null && Line[i] != String.Empty)
                        {
                            Pipes.SendString(Line[i]);
                        }
                    }
                }
        
                if (null != TempKey)
                {
                    for (int i = 0; i < 5; ++i)
                    {
                        if (TempKey[i] != null)
                        {
                            switch(i)
                            {
                                case 0:
                                    MTP.TempKey0 = TempKey[i];
                                    break;
        
                                case 1:
                                    MTP.TempKey1 = TempKey[i];
                                    break;
        
                                case 2:
                                    MTP.TempKey2 = TempKey[i];
                                    break;
        
                                case 3:
                                    MTP.TempKey3 = TempKey[i];
                                    break;
        
                                case 4:
                                    MTP.TempKey4 = TempKey[i];
                                    break;
        
                                case 5:
                                    MTP.TempKey5 = TempKey[i];
                                    break;
                            }
                        }
                    }
                    Pipes.SendObject(MTP);
                }
            }
    ------------------------------------------------------------

    Code:
    [Serializable]
        class MacroTempKey
        {
            public object TempKey0
            {
                get;
                set;
            }
        
            public object TempKey1
            {
                get;
                set;
            }
        
            public object TempKey2
            {
                get;
                set;
            }
        
            public object TempKey3
            {
                get;
                set;
            }
        
            public object TempKey4
            {
                get;
                set;
            }
        
            public object TempKey5
            {
                get;
                set;
            }
        }
    ------------------------------------------------------------

    Code:
     static NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "EXAMPLE", PipeDirection.InOut);
        
            public static void SendBytes(byte[] data, int bytes) // This works , bottom 2 functions are not tested/a work in progress related to question.
            {
                try
                {
                    pipeClient.Write(data, 0, bytes);
                    pipeClient.Flush();
                }
                catch
                {
    
                }
            }
    
                public static void SendString(string s)
                {
                    try
                    {
                        using (var binWriter = new BinaryWriter(pipeClient))
                        {
                            binWriter.Write(s);
                            binWriter.Flush();
                        }
                    }
                    catch
                    {
        
                    }
                }
        
                public static void SendObject(MacroTempKey message)
                {
                    try
                    {
                        IFormatter formatWriter = new BinaryFormatter();
                        formatWriter.Serialize(pipeClient, message);
                    }
                    catch
                    {
        
                    }
                }
    ------------------------------------------------------------

    C++

    Code:
    HANDLE hPipe;
        
        LPCTSTR lpszPipename = TEXT("\\\\.\\pipe\\EXAMPLE");
        
        	hPipe = CreateNamedPipe(lpszPipename,
        		PIPE_ACCESS_DUPLEX, 
        		PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT, 
        		PIPE_UNLIMITED_INSTANCES,
        		BUFSIZE ,
        		BUFSIZE ,
        		0, 
        		NULL);
        
            [Serializable]
            ref class MacroTempKey
            {
            public:
            	property Object ^TempKey0;
            
            	property Object ^TempKey1;
            
            	property Object ^TempKey2;
            
            	property Object ^TempKey3;
            
            	property Object ^TempKey4;
            
            	property Object ^TempKey5;
            };
        
            DWORD ReadClient()
            {
            	while(!loopstop)
            	{
            		if(startread)
            		{
            			while(ReadFile(hPipe, chRequest, BUFSIZE, &cbBytesRead, NULL) >0)
            			{
            				GetAnswerToRequest(chRequest); 
            				Sleep(20); 
            			}
                                GetAnswerToOtherRequest();
            			Sleep(100);
            			SendToPipe(0, 0);
            		}
            		else
            			Sleep(500);
            	}
            	return 0;
            }
        
        void __stdcall GetAnswerToRequest(__in char* szRequest)
        {
        	switch (szRequest[0])
        	{
        	case TEST:
        		if (szRequest[1]  == 1)
        		{
        			
        		}
        		else
        		{
        			
        		}
        		break;
        	}
        }
        
        void GetAnswerToOtherRequest()
        {
        	IFormatter^ f = gcnew BinaryFormatter();
        	MacroTempKey ^messageReceived = safe_cast<MacroTempKey^>(f->Deserialize(hPipe));
        }
    ------------------------------------------------------------

    As you can see, I am attempting to send string[] Line and object[] TempKey from c# to c++ through a namedPipe.

    I am positive I did this correctly in c#, however the same cannot be said for the c++ part.

    Can anyone help me edit the code above to properly receive the string[] Line and put it into its respective c++ string[] Line & to also receive the object[] TempKey and put it into its respective c++ class.

    Here is the following error I get at build.

    I need help as I am confused.

    Error1 error C2664: System::Runtime::Serialization::IFormatter:eserialize': cannot convert parameter 1 from 'HANDLE' to 'System::IO::Stream ^'
    The error above is for the HANDLE hPipe in the deserialize function.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,595

    Re: PipeClient c# to PipeServer c++ : Deserializing confusion (need help)

    Why do you think the .NET framework binary formatter could de-serialize a native pipe handle (or any other native type, for that matter)? Its Deserialize() member accepts a System::IO::Stream object as its first parameter and nothing else. Why not simply use one of the classes offered by the .NET framework for pipe communication (one of the PipeStream derivates)?

    Even though possible, it's usually not a good idea to needlessly mix managed and native stuff in C++/CLI.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center