【问题标题】:CreateProcessAsUser win api is not working on non win7 platformsCreateProcessAsUser win api 在非 win7 平台上不起作用
【发布时间】:2011-08-01 12:24:03
【问题描述】:

我正在使用 win-api 函数: 登录用户 加载配置文件 CreateProcessAsUser

使用 java 和 jniwrapper。

我将我的应用程序作为服务进程(本地系统帐户)运行。

如果我使用 CreateProcessWithLogon - 它仅在我不从服务运行时才有效(只是常规的独立应用程序)。

进程在windows 7上创建成功,win xp和win 2003 server上没有。

你有什么线索吗? win平台如何管理进程/用户权限?

谢谢....

【问题讨论】:

  • 从 Windows 服务创建交互式进程是一项极其繁重的任务。阅读this 了解更多背景信息。

标签: java windows winapi


【解决方案1】:

Windows 处理服务的方式以及它们交互的方式在 NT 6.0(Vista,Server 2003 R2)前后发生了变化。它通常是一个复杂的主题,因为它涉及从一个应该是非交互式的东西创建流程,尤其是 UI。 理解它通常需要很好地了解什么是 Windows 上的 Session、Winsta 和 Desktop。

此处记录了更改(名为“会话 0 隔离”): http://msdn.microsoft.com/en-us/windows/hardware/gg463353。 它指出从服务显示 UI 的正确方法是使用 CreateProcessAsUser 生成一个新进程,但您已经知道,因为您的 Windows 7 机器运行。

如果我没记错的话,CreateProcessAsUser 的行为也发生了变化:在以前的版本中,它不会自动将用户添加到 winsta 和桌面(它们是安全对象,即具有 ACL 的对象,如文件):

"如果应用程序在 lpDesktop 成员中指定了一个桌面,它是 应用程序有责任为 指定的用户帐户到指定的窗口站和桌面。”

这句话摘自这里: http://support.microsoft.com/kb/165194 很有用的文章!包括有关如何执行所有 ACL 的代码示例 东西(不幸的是,非托管代码......)

此外,您还可以在此处找到其他相同的信息: http://msdn.microsoft.com/en-us/library/aa379608%28v=vs.85%29.aspx

【讨论】:

  • 这很有趣。您是否有一种在 java 中使用 GUI 创建包装服务的教程?
【解决方案2】:

使用 Java,您现在可以使用 JNA-API 在 Windows 系统上执行 CreateProcess 函数 -

这会在当前用户桌面(从会话 0 上运行的 windows 服务)上启动一个 cmd 控制台,该控制台运行一个程序 - 经过测试的 windows 7-11

使用喜欢

new CreateProcessAsUser("notepad.exe");
new CreateProcessAsUser("start PowerShell ");
import com.sun.jna.*;
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinBase.*;

public class CreateProcessAsUser
{
    public CreateProcessAsUser(String toExecute)
    {
        WString nullW = null;
        PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION();
        STARTUPINFO startupInfo = new STARTUPINFO();
        boolean result = MoreAdvApi32.INSTANCE.CreateProcessWithLogonW
                                                      (new WString("username"),                         // user
                                                              nullW,                                           // domain , null if local
                                                              new WString("password"),                         // password
                                                              MoreAdvApi32.LOGON_WITH_PROFILE,                 // dwLogonFlags
                                                              nullW,                                           // lpApplicationName
                                                              new WString("c:\\windows\\system32\\cmd.exe /c " + toExecute),   // command line
                                                              MoreAdvApi32.CREATE_NEW_CONSOLE,                 // dwCreationFlags
                                                              null,                                            // lpEnvironment
                                                              nullW,                   // directory
                                                              startupInfo,
                                                              processInformation);
        
        if (!result)
        {
            int error = Kernel32.INSTANCE.GetLastError();
            System.out.println("OS error #" + error);
            System.out.println(Kernel32Util.formatMessageFromLastErrorCode(error));
        }
    }
}

interface MoreAdvApi32 extends Advapi32
{
    MoreAdvApi32 INSTANCE =
            (MoreAdvApi32) Native.loadLibrary("AdvApi32", MoreAdvApi32.class);
    
    /*
     * BOOL WINAPI CreateProcessWithLogonW( __in LPCWSTR lpUsername,
     * __in_opt LPCWSTR lpDomain, __in LPCWSTR lpPassword, __in DWORD
     * dwLogonFlags, __in_opt LPCWSTR lpApplicationName, __inout_opt LPWSTR
     * lpCommandLine, __in DWORD dwCreationFlags, __in_opt LPVOID
     * lpEnvironment, __in_opt LPCWSTR lpCurrentDirectory, __in
     * LPSTARTUPINFOW lpStartupInfo, __out LPPROCESS_INFORMATION
     * lpProcessInfo );
     */
    
    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682431%28v=vs.85%29.aspx
    boolean CreateProcessWithLogonW
    (WString lpUsername,
     WString lpDomain,
     WString lpPassword,
     int dwLogonFlags,
     WString lpApplicationName,
     WString lpCommandLine,
     int dwCreationFlags,
     Pointer lpEnvironment,
     WString lpCurrentDirectory,
     STARTUPINFO lpStartupInfo,
     PROCESS_INFORMATION lpProcessInfo);
    
    public static final int LOGON_WITH_PROFILE = 0x00000001;
    public static final int LOGON_NETCREDENTIALS_ONLY = 0x00000002;
    
    
    int CREATE_NO_WINDOW = 0x08000000;
    int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
    int CREATE_NEW_CONSOLE = 0x00000010;
    int DETACHED_PROCESS = 0x00000008;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多