1

I have learned about Named Pipe and try to send the message from C++ and C# application and it works fine. But I am not able to send the image from C++ side to C# using Named Pipe. Can anyone please tell me how I can do this.

My C# Code for Named Pipe

static void Main(string[] args)
    {
        Console.WriteLine("Creating Client Pipe");
        NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
        Console.WriteLine("Pipe Created Successfully, Connecting to Server");
        pipe.Connect();
        Console.WriteLine("Successfully, Connected to Server");
        using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
        {
            System.Console.WriteLine("Message from Server: " + rdr.ReadToEnd());
        }

        Console.ReadKey();
    }

and C++ Code

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Server Creating Pipe\n";
    HANDLE hPipe = ::CreateNamedPipe(_T("\\\\.\\pipe\\HyperPipe"),
        PIPE_ACCESS_DUPLEX,
        PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
        PIPE_UNLIMITED_INSTANCES,
        4096,
        4096,
        0,
        NULL);

    cout << "Server Created Succesfully";
    ConnectNamedPipe(hPipe, NULL);

    LPTSTR data = _T("Hello");
    cout << "Sending Message to Client";
    DWORD bytesWritten = 0;
    WriteFile(hPipe, data, _tcslen(data) * sizeof(TCHAR), &bytesWritten, NULL);
    CloseHandle(hPipe);
    return 0;
}

What I want is when the I enter the path on the C# side it will send that to C++ and using OpenCV library I will fetch that image from the memory and send to the C#.

Thanks in Advance

1 Answers1

1

Have you tried converting your image to an array of bytes, and then sending it the same way you did with the text ?

Jakub
  • 125
  • 1
  • 3
  • 9
  • No, but I think I should try this, Thanks. –  May 30 '18 at 08:20
  • Hi Jakub, I have tried this but not able to do this. Can you please provide me the code for that if possible. –  May 30 '18 at 10:33
  • Here's simple solution for doing that in C# https://stackoverflow.com/questions/3801275/how-to-convert-image-to-byte-array?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Jakub May 30 '18 at 10:55
  • For C++ you can use this Library http://www.imagemagick.org/script/magick-core.php – Jakub May 30 '18 at 10:59
  • Great, then use it to convert the image to your array of bytes. You can always use more than 1 library on 1 project. – Jakub May 30 '18 at 11:07
  • The problem is not with converting the image to byte array i am not able to transfer that array to C# –  May 30 '18 at 11:10
  • You said that you tried this but were not able to do this. You are able to send a string, like you did in the snippets above? So transform that array to string, send to C# program and recreate an image from it. – Jakub May 30 '18 at 11:23