java声明公共指针,如何使用Java将环境指针传递给Windows CreateProcess(使用jna)

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.

I first used

Advapi32Util.getEnvironmentBlock(environment)

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;

}

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值