나는 .dll을 C++로 작성 했으므로 명명 된 파이프를 사용하여 C# 응용 프로그램과 통신하려고합니다.명명 된 파이프를 사용하여 C++ .dll과 C# 응용 프로그램간에 메시지를 보내는 방법은 무엇입니까?
이제 C# 응용 프로그램에 내장 된 System.IO.Pipe .net 클래스를 사용하고 있으며 C++의 일반 함수를 사용하고 있습니다.
나는 C++에서 많은 경험을하지 못했다. (읽기 : 이것은 나의 첫 번째 C++ 코드이다.), 나는 C#에서 경험했다.
서버와 클라이언트와의 연결이 작동하고있는 것 같지만 메시지가 전송되지 않는 유일한 문제입니다. .dll 서버, C# 응용 프로그램 서버, 파이프 방향 InOut (이중)을 만들려고했으나 아무 것도 작동하지 않는 것 같습니다.
DWORD ServerCreate() // function to create the server and wait till it successfully creates it to return.
{
hPipe = CreateNamedPipe(pipename,//The unique pipe name. This string must have the following form: \\.\pipe\pipename
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT, //write and read and return right away
PIPE_UNLIMITED_INSTANCES,//The maximum number of instances that can be created for this pipe
4096 , // output time-out
4096 , // input time-out
0,//client time-out
NULL);
if(hPipe== INVALID_HANDLE_VALUE)
{
return 1;//failed
}
else
return 0;//success
}
void SendMsg(string msg)
{
DWORD cbWritten;
WriteFile(hPipe,msg.c_str(), msg.length()+1, &cbWritten,NULL);
}
void ProccesingPipeInstance()
{
while(ServerCreate() == 1)//if failed
{
Sleep(1000);
}
//if created success, wait to connect
ConnectNamedPipe(hPipe, NULL);
for(;;)
{
SendMsg("HI!");
if(ConnectNamedPipe(hPipe, NULL)==0)
if(GetLastError()==ERROR_NO_DATA)
{
DebugPrintA("previous closed,ERROR_NO_DATA");
DisconnectNamedPipe(hPipe);
ConnectNamedPipe(hPipe, NULL);
}
Sleep(1000);
}
그리고이 같은 C#을 cliend :
static void Main(string[] args)
{
Console.WriteLine("Hello!");
using (var pipe = new NamedPipeClientStream(".", "HyprPipe", PipeDirection.In))
{
Console.WriteLine("Created Client!");
Console.Write("Connecting to pipe server in the .dll ...");
pipe.Connect();
Console.WriteLine("DONE!");
using (var pr = new StreamReader(pipe))
{
string t;
while ((t = pr.ReadLine()) != null)
{
Console.WriteLine("Message: {0}",t);
}
}
}
}
나는 .DLL는 C# 응용 프로그램에 메시지를 보내는 서버를 만들려고 할 때
는, 내가 사용하는 코드는 다음과 같았다
C# 클라이언트가 .dll에 연결되어 있지만 메시지를받지 못한다는 것을 알 수 있습니다. 이전에 말했던 것처럼 다른 방법으로 시도해 보았습니다. dll은 메시지 상자에 표시합니다. .dll이 삽입되어 C# 서버에 연결되었지만 메시지를 받았을 때 삽입 된 응용 프로그램이 손상되었습니다.
나를 도와 또는 C++과 C
오 남자, 당신은 굵게 C++에서 pipenames의 차이와 C#을 넣어해야합니다. 나는 당신의 답을 찾을 때까지이 문제를 해결하는데 시간 낭비했다. –
@BrianFairservice - 완료 :). 이 게시물이 도움이되어 기쁩니다! –