3

私は主に以下に基づいて Namedpipeserver を作成しています:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx
.NET から接続しようとするとこのコードを使用するクライアント:

NamedPipeClientStream clientPipe = new NamedPipeClientStream(".",      
    "\\\\.\\pipe\\TTCUIHELPER_SEND_TTC_RECEIVE",PipeDirection.Out);
try
{        

    if (clientPipe != null)
    {
        clientPipe.Connect(5000);
        if (clientPipe.IsConnected == true)
        {
            byte[] bytes = pm.GetMessageData();
            clientPipe.Write(bytes, 0, bytes.Length);
            clientPipe.Flush(); 
            clientPipe.Dispose();
            clientPipe.Close();
        }
    }
}
catch (Exception Ex)
{
    System.Windows.MessageBox.Show(Ex.Message);
}

その後、接続は常にタイムアウトします。奇妙なことに、CPP を使用してテスト クライアントを作成しようとすると、次のようになります。

LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\TTCUIHELPER_SEND_TTC_RECEIVE");
const TCHAR* lpvMessage=L"QQQQ";

HANDLE hPipe = CreateFile( 
    lpszPipename,   // pipe name 
    GENERIC_READ |  // read and write access 
    GENERIC_WRITE, 
    0,              // no sharing 
    NULL,           // default security attributes
    OPEN_EXISTING,  // opens existing pipe 
    0,              // default attributes 
    NULL);          // no template file 

// Break if the pipe handle is valid. 

DWORD cbWritten;
DWORD cbToWrite = (lstrlen(lpvMessage)+1)*sizeof(TCHAR);
_tprintf( TEXT("Sending %d byte message: \"%s\"\n"), cbToWrite, lpvMessage); 

BOOL fSuccess = WriteFile( 
    hPipe,                  // pipe handle 
    lpvMessage,             // message 
    cbToWrite,              // message length 
    &cbWritten,             // bytes written 
    NULL);                  // not overlapped 

if ( ! fSuccess) 
{
    _tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() ); 
    return -1;
}

その後、問題なく動作します。CPP では動作するが .NET では動作しないというのは、何が間違っているのでしょうか?

4

1 に答える 1

2

パイプ名からパイプ プレフィックスを削除するだけです。つまり、名前自体だけを使用します。

"TTCUIHELPER_SEND_TTC_RECEIVE"

.NETNamedPipeClientStreamクラスは、Windows WriteFile API を内部的に呼び出すときにプレフィックスを追加します。

于 2012-10-08T09:33:33.193 に答える