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



Java似乎无法从WinAPI继承匿名管道,我正在使用自己的库,无法弄清楚问题是什么。

当前提交的库源。

阴离子测试:

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: %sn", 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: %sn", 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);
        }
    }
}

据我所知,我正确地完成了所有这些工作。

我正在使用bInheritHandle设置为 true 的SECURITY_ATTRIBUTES创建带有 CreatePipe 的阴离子管道。

然后在创建客户端后,我关闭"客户端"端句柄。

我错过了什么?

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

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

输出:

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

我记得在这里读到一个进程的句柄在另一个进程中无效,Java 窗口匿名管道仅在线程之间工作。 这可以解释无效的句柄。

最新更新