To use CreateProcessWithTokenW
from java with JNA you need to bind the function. JNA is just a layer, that makes it possible to call directly native library functions. For this to work JNA uses java descriptions of the native interface, which are then used to do the actual call.
The jna-platform contrib project (released together with the main project) contains a big number of already bound win32 functions and indeed in Advapi32.java
there are already bindings for CreateProcessAsUser
and CreateProcessWithLogonW
. Based on that I would try this (UNTESTED!):
public interface Advapi32Ext extends StdCallLibrary {
Advapi32Ext INSTANCE = Native.load("Advapi32", Advapi32Ext.class, W32APIOptions.DEFAULT_OPTIONS);
boolean CreateProcessWithToken(
HANDLE hToken,
int dwLogonFlags,
String lpApplicationName,
String lpCommandLine,
int dwCreationFlags,
Pointer lpEnvironment,
String lpCurrentDirectory,
STARTUPINFO lpStartupInfo,
PROCESS_INFORMATION lpProcessInfo
);
}
This assumes that you run with the system property w32.ascii
set to false
or unset, which is the recommend setup. In that case the W32APIFunctionMapper.UNICODE
is used, which appends the "W" suffix automatically. The then also selected W32APITypeMapper.UNICODE
ensures, that java String
objects are mapped to wchars
or in case of a function call to LP*WSTR
.