【问题标题】:Send DWORD through named pipe通过命名管道发送 DWORD
【发布时间】:2014-12-18 12:17:19
【问题描述】:

我试图通过命名管道发送一组 DWORD,但我一直在试图弄清楚如何发送单个 DWORD。 这是我到目前为止所得到的:

// Create a pipe to send data
HANDLE pipe = CreateNamedPipe(
        L"\\\\.\\pipe\\my_pipe",
        PIPE_ACCESS_OUTBOUND,
        PIPE_TYPE_BYTE,
        1,
        0,
        0,
        0,
        NULL
    );

/* Waiting for the other side to connect and some error handling cut out */

//Here I try to send the DWORD  
DWORD msg = 0xDEADBEEF;
DWORD numBytesWritten = 0;
result = WriteFile(
    pipe,
    (LPCVOID)msg, 
    sizeof(msg),
    &numBytesWritten, 
    NULL 
    );

但是WriteFile(...) 调用失败并返回false

接收端:

/* CreateFile(...) */
DWORD msg[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
    pipe,
    msg, 
    127 * sizeof(DWORD), 
    &numBytesRead, 
    NULL 
    );

我是在惨败还是朝着正确的方向前进?

【问题讨论】:

  • 加上一个 msg=0xDEADBEEF,让我咯咯笑……又饿了。 :) 另外,我在快速谷歌后学到了一些东西:en.wikipedia.org/wiki/Hexspeak

标签: c++ windows named-pipes


【解决方案1】:
result = WriteFile(
    pipe,
    &msg, // <---- change this line
    sizeof(msg),
    &numBytesWritten, 
    NULL 
    );

当你施放时,你的脑海中应该会出现危险信号。在 C++(一种类型安全的语言)中,当您尝试手动覆盖类型时,您就处于危险区域。 WriteFile 需要一个指向数据的指针。您提供了数据本身。相反,您应该提供指向数据的指针。

另外,学会在通话失败时使用GetLastError获取更多信息。

【讨论】:

  • 我的英雄和救世主!谢谢,现在我已经设法让它工作了!编辑:讨厌的 5 分钟限制,直到我可以接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2014-09-25
相关资源
最近更新 更多