createprocessasuserw错误代码6无效句柄jna

llycmphe  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(392)

我正在使用jna调用windows api。我想作为一个特定的用户启动一个进程(不管是哪个)。我使用的两个api调用是:
logonuserw
createprocessasuserw
logonuserw成功,但createprocessasuserw失败,出现错误6。根据windows系统错误代码文档,这对应于“错误\无效\句柄”。
据我所知,我传入的唯一句柄是用户句柄。我看不出这有什么不对。根据logonuserw文件,
在大多数情况下,返回的句柄是一个主令牌,您可以在调用createprocessasuser函数时使用它。但是,如果指定logon32\u logon\u网络标志,logonuser将返回一个模拟令牌,除非调用duplicatetokenex将其转换为主令牌,否则无法在createprocessasuser中使用该令牌。
但是,我不使用logon32\u logon\u网络。
有些结构参数有句柄,但我要么传递null,要么由api调用而不是我填充。
以下是我的代码:

final PointerByReference userPrimaryToken =
    new PointerByReference();
System.out.printf(
    "ptr.peer = %d\n",
    Pointer.nativeValue(userPrimaryToken.getValue())
);

final boolean logonOk = MyWinBase.INSTANCE.LogonUserW(
    toCString(<my-username>), // hidden
    toCString("ANT"),
    toCString(<my-password>), // hidden
    /* This logon type is intended for batch servers, where
    processes may be executing on behalf of a user without their
    direct intervention. This type is also for higher
    performance servers that process many plaintext
    authentication attempts at a time, such as mail or web
    servers.*/
    WinBase.LOGON32_LOGON_BATCH,
    WinBase.LOGON32_PROVIDER_DEFAULT,
    userPrimaryToken
);
System.out.printf("ok = %b\n", logonOk);
System.out.printf(
    "ptr.peer = %d\n",
    Pointer.nativeValue(userPrimaryToken.getValue())
);

final STARTUPINFOW.ByReference startupInfoW =
    new STARTUPINFOW.ByReference();
startupInfoW.cb = startupInfoW.size();
startupInfoW.lpReserved = Pointer.NULL;
startupInfoW.lpDesktop = Pointer.NULL;
startupInfoW.lpTitle = Pointer.NULL;
startupInfoW.dwFlags
    = startupInfoW.dwX = startupInfoW.dwY
    = startupInfoW.dwXSize = startupInfoW.dwYSize
    = startupInfoW.dwXCountChars = startupInfoW.dwYCountChars
    = startupInfoW.dwFillAttribute
    = startupInfoW.wShowWindow
    = 0;
startupInfoW.cbReserved2  = 0;
startupInfoW.lpReserved2 = Pointer.NULL;
startupInfoW.hStdInput = startupInfoW.hStdOutput
    = startupInfoW.hStdError
    = Pointer.NULL;

final PROCESS_INFORMATION.ByReference processInformation =
    new PROCESS_INFORMATION.ByReference();
processInformation.hProcess = processInformation.hThread
    = Pointer.NULL;
processInformation.dwProcessId = processInformation.dwThreadId
    = 0;

final boolean createProcessOk = MyProcessThreadsApi.INSTANCE
    .CreateProcessAsUserW(
        userPrimaryToken.getPointer(),
        toCString("C:\\Windows\\System32\\cmd.exe"),
        // execute and terminate
        toCString("/c whoami > whoami.txt"),
        Pointer.NULL,
        Pointer.NULL,
        false,
        WinBase.CREATE_UNICODE_ENVIRONMENT,
        new PointerByReference(),
        Pointer.NULL,
        startupInfoW,
        processInformation
    );
System.out.printf("ok = %b\n", createProcessOk);
System.out.printf(
    "dwProcessId = %d\n", processInformation.dwProcessId
);
System.out.printf(
    "last err code = %d\n",
    ErrHandlingApi.INSTANCE.GetLastError()
);

以下是我的输出:

ptr.peer = 0
ok = true
ptr.peer = 1040
ok = false
dwProcessId = 0
last err code = 6

有什么建议吗?

brqmpdu1

brqmpdu11#

看看这段代码:

final PointerByReference userPrimaryToken = ...;

在线文档说它代表一个指向指针的指针,c表示法 void** https://java-native-access.github.io/jna/4.2.1/com/sun/jna/ptr/pointerbyreference.html
在logonuser的文档中,它需要一个指向句柄的Phallle指针,它类似于指向指针的指针,因为句柄类似于指针(它被声明为 typedef void *HANDLE; ).
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-logonuserw

BOOL LogonUserW(
  ....
  DWORD   dwLogonProvider,
  PHANDLE phToken
);

但在createprocessasuser的文档中指定此函数接受句柄,而不是幻影
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessasuserw

BOOL CreateProcessAsUserW(
  HANDLE                hToken,
  LPCWSTR               lpApplicationName,
  ....
);

所以我希望您传递的是getvalue而不是getpointer。通过使用getpointer,您可以得到指针本身,在您的例子中,它很可能是指向指针的指针。我不知道jna,但是期望值来自winapi的知识

final boolean createProcessOk = MyProcessThreadsApi.INSTANCE
    .CreateProcessAsUserW(
        userPrimaryToken.getValue(),
        ....
    );

相关问题