Sto facendo una .dll injected scritta in C++ e voglio comunicare con un'app C# usando pipe con nome.Come inviare messaggi tra C++. Dll e C# app usando named pipe?
Ora, sto utilizzando le classi System.IO.Pipe .net integrate nell'app C# e sto utilizzando le normali funzioni in C++.
Non ho molta esperienza in C++ (leggi: questo è il mio primo codice C++ ..), anche se ho esperienza in C#.
Sembra che la connessione con il server e il client funzioni, l'unico problema è che i messaggi non vengono inviati. Ho provato a creare il file .dll sul server, l'app C# sul server, rendendo la direzione del tubo InOut (duplex) ma nessuno sembra funzionare.
Quando ho provato a fare la dll al server, che invia messaggi al C# app, il codice che ho usato era come questo:
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);
}
E il C# cliend come questo:
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);
}
}
}
}
Vedo che il client C# è collegato al dll, ma non riceverà alcun messaggio .. Ho provato a farlo viceversa, come ho detto prima, e cercando di fare in modo che il C# invii messaggi a. dll, che li mostrerebbe in una finestra di messaggio. Il file .dll è stato iniettato e collegato al server C#, ma quando ha ricevuto un messaggio ha semplicemente bloccato l'applicazione a cui è stato iniettato.
please help me, o mi guida su come utilizzare named pipe tra C++ e C# app
Oh uomo, si dovrebbe mettere le differenze tra i pipenames in C++ e C# in grassetto. Ho perso ORE per risolvere il problema fino a quando non ho trovato la tua risposta. –
@BrianFairservice - Fatto :). Sono contento che questo post abbia aiutato! –