Programming/Etc

Communication between C# and C++ through named pipe

역시인생한방 2015. 5. 20. 16:05

[Server - C#]


using System;

using System.IO;

using System.IO.Pipes;


namespace NamedPipeCs

{

class Program

{

static void Main(string[] args)

{

while (true)

{

using (var server = new NamedPipeServerStream("DavidPipe"))

{

server.WaitForConnection();


using (var reader = new StreamReader(server))

{

string temp;

while ((temp = reader.ReadLine()) != null)

{

Console.WriteLine("{0}", temp);

}

}

}

}

}

}

}


----------------------------------------------------------------------------


[Client - C++]


#include <iostream>

using namespace std;

#include <windows.h>


int main()

{

HANDLE hPipe = CreateFile("\\\\.\\pipe\\DavidPipe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (hPipe == NULL || hPipe == INVALID_HANDLE_VALUE)

return -1;


LPTSTR lpMessage = TEXT("FUCK THAT SHIT");

DWORD dwNumberOfBytesToWrite = (lstrlen(lpMessage) + 1) * sizeof(TCHAR);

DWORD dwNumberOfBytesWritten;


BOOL bSuccess = WriteFile(hPipe, lpMessage, dwNumberOfBytesToWrite, &dwNumberOfBytesWritten, NULL);

if (bSuccess == FALSE)

return -1;


CloseHandle(hPipe);


return 0;

}