如何将环境指针传递给Java中的Windows CreateProcess(使用jna) [英] How to pass a Environment Pointer to Windows CreateProcess in Java (using jna)

查看:252
本文介绍了如何将环境指针传递给Java中的Windows CreateProcess(使用jna)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在呼叫Kernel32.Instance.CreateProcess以启动分离的进程.我面临的一个问题是,每次我没有启动该过程时,都试图将环境块传递给CreateProcess.

I am calling Kernel32.Instance.CreateProcess to start a detached process. One issue I am facing is attempting to pass a environment block to CreateProcess each time I do the process does not start.

我第一次使用

Advapi32Util.getEnvironmentBlock(environment)

创建块,然后创建一个指针(CreateProcess需要(我用过:

to create the block, then to make a Pointer (needed by CreateProcess( I used:

public static Pointer asPointer(String string) {
    byte[] data;
    try {
        data = Native.toByteArray(string, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    Pointer pointer = new Memory(data.length + 1);
    pointer.write(0, data, 0, data.length);
    pointer.setByte(data.length, (byte) 0);
    return pointer;
}

我认为最终会导致需要双重null.我确实设置了CREATE_UNICODE_ENVIRONMENT,所以我尝试在Memory的末尾添加两个额外的null.仍然导致CreateProcess返回false而不启动命令.

That I think results in the double null needed at the end. I did actually set CREATE_UNICODE_ENVIRONMENT so I tried add two extra nulls to the end of the Memory. That still resulted in CreateProcess returning false and not starting the command.

我不明白我在做什么错.我应该检查可能提示问题的日志文件吗?

I don't understand what I am doing wrong. Should I be checking a log file that might hint at the problem?

推荐答案

啊,看来在使用CREATE_UNICODE_ENVIRONMENT时,它需要使用UTF-16LE byte[].确保每个key=value后面都跟着两个空值,又名(byte) 0).最后再添加两个空字节.指针可以如上制作.

Ah So it seems while using CREATE_UNICODE_ENVIRONMENT it needs to take UTF-16LE byte[]. Ensure that each key=value is followed by two nulls aka (byte) 0). Finally add two more null bytes. The pointer can be made as above.

这篇关于如何将环境指针传递给Java中的Windows CreateProcess(使用jna)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆