如何在c和x2B之间发送消息+。使用命名管道的dll和C#应用程序? 我正在用C++编写一个注入式DLL,我想用命名管道与C语言应用程序通信。 现在,我使用了Cype应用程序中的内置系统.IO.Cube .NET类,并且我使用C++中的正则函数。 我在C++方面没有太多的经验(Read:这是我的第一个C++代码……),我在C语言方面有经验。

如何在c和x2B之间发送消息+。使用命名管道的dll和C#应用程序? 我正在用C++编写一个注入式DLL,我想用命名管道与C语言应用程序通信。 现在,我使用了Cype应用程序中的内置系统.IO.Cube .NET类,并且我使用C++中的正则函数。 我在C++方面没有太多的经验(Read:这是我的第一个C++代码……),我在C语言方面有经验。,c#,c++,pipe,C#,C++,Pipe,似乎与服务器和客户端的连接正在工作,唯一的问题是没有发送消息。我试着将.dll设置为服务器,将C#app设置为服务器,将管道方向设置为InOut(双工),但似乎都不起作用 当我试图将.dll设置为服务器,向C#app发送消息时,我使用的代码如下: DWORD ServerCreate() // function to create the server and wait till it successfully creates it to return. { hPipe = Create

似乎与服务器和客户端的连接正在工作,唯一的问题是没有发送消息。我试着将.dll设置为服务器,将C#app设置为服务器,将管道方向设置为InOut(双工),但似乎都不起作用

当我试图将.dll设置为服务器,向C#app发送消息时,我使用的代码如下:

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);

}
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);
                }
            }
        }
    }
而C#cliend是这样的:

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);

}
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);
                }
            }
        }
    }
我看到C#客户端已连接到.dll,但它不会收到任何消息。。 正如我之前所说,我尝试了另一种方法,并试图让C#将消息发送到.dll,后者将在消息框中显示它们。 .dll被注入并连接到C#服务器,但当它收到一条消息时,它只是使注入到的应用程序崩溃

请帮助我,或者指导我如何在C++与C语言应用程序

之间使用命名管道。 1-您是否使用相同的管道名称
C++:“\\.\pipe\HyperPipe”
C:“超级管道”

2——我认为在C++方面,最好使用Read toEnter(),我只在C++中使用命名管道,但是我假设Read toEnter()将在使用消息后读取消息。 基础管道

3——在C++方面,您尝试使用非阻塞管道,并为连接和数据等查询管道。我建议三件事情之一。br/>

a - Use a blocking pipe on a separate thread or b - Use non blocking pipes using Overlapped IO c - Use non blocking pipes using IOCompletion ports 更新:因为我没有在C#中使用过这个,所以我想我应该编写一个小测试。只是使用阻塞管道,没有线程或任何东西,只是为了确认基本工作,这里是非常粗略的测试代码

C++服务器

#include <tchar.h>
#include <windows.h>

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


  ConnectNamedPipe(hPipe, NULL);

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

哦,伙计,你应该把C++中的PiPENM与C语言的区别放在粗体中。在找到您的答案之前,我浪费了数小时进行故障排除。@BrianFairservice-Done:)。我很高兴这篇文章有帮助!