locked
CreateProcessWithLogonW fails and GetLastError returns 0 RRS feed

  • Question

  • Hello,

    I need to spawn a process under a certain user, i use the CreateProcessWithLogonW() function, it creates the process then i use TerminateProcess() function to destroy the process, it works but sometimes CreateProcessWithLogonW returns 0 and GetLastError returns 0.

    Is there a way to have any information on what failed ?

    Thank you

    Wednesday, September 7, 2011 1:01 PM

All replies

  • Just to be sure: how does your source code look like? Do you call GetLastError() immediately after CreateProcessWithLogonW() fails? No I/O in between?

     

    Wednesday, September 7, 2011 9:33 PM
  • Nothing between CreateProcessWithLogonW and GetLastError.

    The code is Java with JNA (https://github.com/twall/jna), the problem disappeared when i removed the lpDesktop value, it must be NULL to work.

     

    WinBase.STARTUPINFO si = new WinBase.STARTUPINFO();
    si.dwFlags = WinBase.STARTF_USESHOWWINDOW | WinBase.STARTF_USESTDHANDLES;
    si.hStdInput = this.inRead.getValue();
    si.hStdOutput = this.outWrite.getValue();
    si.hStdError = this.errWrite.getValue();
    // si.lpDesktop = ""; // <== THIS SEEMS TO BE THE PROBLEM
    si.wShowWindow = new WinDef.WORD(0); // SW_HIDE
    
    boolean result = CreateProcessWithLogonW(
    /* String */ this.user, this.domain, this.password,
    /* int */ MyAdvapi.LOGON_WITH_PROFILE, // load user profile
    /* String */ null, // The name of the module to be executed
    /* String */ lpCommandLine, // The command line to be executed 
    /* int */ WinBase.CREATE_NO_WINDOW | WinBase.CREATE_UNICODE_ENVIRONMENT, // creation flags
    /* String */ null, // the new process uses an environment created from the profile of the user
    /* String */ null, // the new process has the same current drive and directory as the calling process
    /* WinBase.STARTUPINFO */  si, // pointer to STARTUPINFO or STARTUPINFOEX structure
    /* WinBase.PROCESS_INFORMATION */ pi ); // pointer to PROCESS_FORMATION structure
    
    if (!result) {
      win32Error("CreateProcessWithLogon"); // calls GetLastError
    }
    
    /* ... */
    
    private static void win32Error(final String functionName){
      int err = Kernel32.INSTANCE.GetLastError();
      String mess = Kernel32Util.formatMessageFromLastErrorCode(err);
      IOException e = new IOException(functionName+" error="+err+", "+mess);
      throw new RuntimeException(e);
    }
    

    It works when the lpDesktop member of the STARTUPINFO structure is not affected with a desktop name.

     

    Friday, September 9, 2011 1:06 PM