代码之家  ›  专栏  ›  技术社区  ›  E235

无法为远程创建命名管道:“句柄无效。”

  •  0
  • E235  · 技术社区  · 2 年前

    我正在尝试创建用于远程使用的命名管道服务器 127.0.0.1 但在我跑步之后 CreateNamedPipe 它失败了,出现错误6,这意味着:

    句柄无效

    我还试图补充 PIPE_ACCEPT_REMOTE_CLIENTS 但它不起作用。

    这是我的服务器代码:

    LPCWSTR pwsPipePrefix = L"\\\\127.0.0.1\\pipe\\";
    LPCWSTR pwsPipeName = NULL;
    HANDLE hServerPipe;
    BOOL bSuccess;
    DWORD bytesWritten = 0;
    DWORD messageLenght;
    BOOL bAttemptImpersonation = TRUE;
    
    // Read name pipe name to from STDIN
    wchar_t wcInputPipeName[200];
    wprintf(L"[*] Pipe name to connect to (max 200 chars): ");
    std::wcin.getline(wcInputPipeName, 200);
    int iInputLen = wcslen(wcInputPipeName);
    if (iInputLen > 200) {
        wprintf(L"[-] More than 200 chars (%d).. exiting.\n", iInputLen);
        return 1;
    }
    std::wstring wsConcat = pwsPipePrefix + (std::wstring)wcInputPipeName;
    pwsPipeName = wsConcat.c_str();
    wprintf(L"[*] Creating named pipe: %ls\n", pwsPipeName);
    hServerPipe = CreateNamedPipe(
        pwsPipeName,            // name of our pipe, must be in the form of \\.\pipe\<NAME>
        PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, // open mode, specifying a duplex pipe so server and client can send and receive data
        PIPE_TYPE_MESSAGE | PIPE_ACCEPT_REMOTE_CLIENTS, // MESSAGE mode to send/receive messages in discrete units (instead of a byte stream)
        PIPE_UNLIMITED_INSTANCES,                   // number of instanced for this pipe, 1 is enough for our use case
        2048,               // output buffer size
        2048,               // input buffer size
        0,                  // default timeout value, equal to 50 milliseconds
        NULL                // use default security attributes
    );
    
    wprintf(L"[*] Waiting for incoming connections...");
    bSuccess = ConnectNamedPipe(hServerPipe, NULL);
    if (bSuccess) {
        wprintf(L"Got one.\n");
    }
    else wprintf(L"Error: %d", GetLastError());
    

    *这段代码取自 this project .

    0 回复  |  直到 2 年前