CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2014
    Posts
    10

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

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

    Your c++ code is managed c++/cli. You might get better answers if you posted this in the managed c++/cli forum (http://forums.codeguru.com/forumdisp...ed-C-and-C-CLI).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured