Java WINAPI匿名管道无效/未找到?

时间:2018-08-06 22:25:36

标签: java windows winapi pipe anonymous-pipes

Java似乎无法从WinAPI继承匿名管道,我使用的是我自己的库,无法确定问题所在。

Library source on the current commit.

肛门测试:

package net.gudenau.lib.pipes.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import net.gudenau.lib.pipes.AnonymousPipeHandle;
import net.gudenau.lib.pipes.PipeHandle;
import net.gudenau.lib.pipes.Pipes;

public class AnonymousPipe{
    public static void main(String[] args){
        try(PipeHandle client = Pipes.findPipe()){
            InputStream inputStream = client.getInputStream();
            OutputStream outputStream = client.getOutputStream();

            byte[] data = "This is from the anon pipe!".getBytes(StandardCharsets.UTF_16);
            outputStream.write(data.length);
            outputStream.write(data);
            data = new byte[inputStream.read()];
            inputStream.read(data);
            System.out.printf("Server says: %s\n", new String(data, StandardCharsets.UTF_16));
        }catch(IOException e){
            e.printStackTrace();
            System.exit(0);
        }
    }

    static void test(){
        File path = new File(System.getProperty("java.home") + File.separator + "bin");
        File executable;
        if(System.getProperty("os.name").toLowerCase().contains("windows")){
            executable = new File(path, "javaw.exe");
        }else{
            executable = new File(path, "java");
        }

        ProcessBuilder processBuilder = new ProcessBuilder(
            executable.getAbsolutePath(),
            "-Dfile.encoding=UTF-8",
            "-classpath",
            System.getProperty("java.class.path"),
            "net.gudenau.lib.pipes.test.AnonymousPipe"
        );
        processBuilder.inheritIO();

        try(AnonymousPipeHandle pipe = Pipes.createPipe()){
            pipe.setupHandleShare(processBuilder);
            Process process = processBuilder.start();
            pipe.clientConnected();

            InputStream inputStream = pipe.getInputStream();
            OutputStream outputStream = pipe.getOutputStream();

            byte[] data = new byte[inputStream.read()];
            inputStream.read(data);
            String message = new String(data, StandardCharsets.UTF_16);
            System.out.printf("Client says: %s\n", new String(data, StandardCharsets.UTF_16));
            data = message.toUpperCase().getBytes(StandardCharsets.UTF_16);
            outputStream.write(data.length);
            outputStream.write(data);

            try{
                process.waitFor();
            }catch(InterruptedException ignored){}
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

据我所知,我正确地完成了所有这一切。

我正在使用CreatePipeSECURITY_ATTRIBUTES设置为true的bInheritHandle创建匿名管道。

然后在创建客户端之后,我关闭“客户端”句柄。

我想念什么?

编辑:我隔开并错过了这些错误。 至少根据Windows而言,test方法抛出The specified procedure could not be found.,这对我来说毫无意义。

根据Windows,另一个进程再次抛出The handle is invalid.

输出:

Client says: the quick brown fox jumps over the lazy dog
Server says: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
java.io.IOException: The specified procedure could not be found.

    at net.gudenau.lib.pipes.impl.windows.WindowsPipeInputStream.read(WindowsPipeInputStream.java:34)
    at java.base/java.io.InputStream.read(InputStream.java:106)
    at net.gudenau.lib.pipes.impl.windows.WindowsPipeInputStream.read(WindowsPipeInputStream.java:16)
    at net.gudenau.lib.pipes.test.AnonymousPipe.test(AnonymousPipe.java:57)
    at net.gudenau.lib.pipes.test.PipeTest.main(PipeTest.java:6)
java.io.IOException: The handle is invalid.

    at net.gudenau.lib.pipes.impl.windows.WindowsPipeOutputStream.write(WindowsPipeOutputStream.java:35)
    at java.base/java.io.OutputStream.write(OutputStream.java:77)
    at net.gudenau.lib.pipes.impl.windows.WindowsPipeOutputStream.write(WindowsPipeOutputStream.java:15)
    at net.gudenau.lib.pipes.test.AnonymousPipe.main(AnonymousPipe.java:19)

Process finished with exit code -1

1 个答案:

答案 0 :(得分:-1)

我记得读过here,其中一个进程的句柄在另一个进程中无效,并且Java Windows匿名管道仅在线程之间起作用。那可以解释一个无效的句柄。