Mar 28, 2007 5:51 PM
Redirect output & error streams of a console app to log files
Hello,
I`m trying to create a process of a console application with other user`s AccessToken from LogonUserW.
Also there is a need to redirects that processes output and error to log files.
Handles to files are created with the 'inherit' flag in SecurityAttributes set to 'true', the flag 'stdHandles' in StartupInfo is set to 'true' because I`ve improved StartupInfo to have setUseStdHandles() setter.
At first attempts I got a "not enought privileges " error but that was solved temporary by adding my user to "Replace a process level token" in Local Policies
User Rights Assi... (need to do that from the code in future)
Well, the problem is that the files that are created, remain zero length and i know that the console app writes output and error (cause i wrote it too in C#) .
How can i correctly redirect the console output to these files??
Here`s the code:
AccessToken accToken = new AccessToken(userName, domain, password,
AccessToken.LOGON32_LOGON_INTERACTIVE
);
// Creating a new StartupInfo
StartupInfo.Options startInfoOpts = new StartupInfo.Options();
startInfoOpts.setUseStdHandles(true);
// These security attributes have 'true' on the ability to inherit handles
SecurityAttributes secAtt = new SecurityAttributes();
secAtt.setInheritHandle(true);
StartupInfo startInfo = new StartupInfo(startInfoOpts);
Handle h_stdOut = new Handle();
Function createF = Kernel32.getInstance().getFunction("CreateFileW");
createF .invoke(fileHandle, new Parameter[] {
new Str(filename),
new UInt32(GENERIC_READ ), //int GENERIC_READ = 0x80000000;
new UInt32(0),
new Pointer(secAtt),
new UInt32(CREATE_ALWAYS), // int CREATE_ALWAYS = 2;
new UInt32(Normal | Overlapped), //int Normal = 0x00000080 int Overlapped = 0x40000000
new Pointer(null, true)
});
startInfo.setStdOutput(h_stdOut);
Handle h_stdErr = new Handle();
createF .invoke(stdErr , new Parameter[] {
new Str(filename),
new UInt32(GENERIC_READ ), //int GENERIC_READ = 0x80000000;
new UInt32(0),
new Pointer(secAtt),
new UInt32(CREATE_ALWAYS), // int CREATE_ALWAYS = 2;
new UInt32(Normal | Overlapped), //int Normal = 0x00000080 int Overlapped = 0x40000000
new Pointer(null, true)
});
startInfo.setStdError(h_stdErr);
// Process options
ProcessOptions procOptions = new ProcessOptions();
procOptions.setUnicodeEnvironment(true);
// Environment keys and values arrays to structure
ProcessVariables procvars = new ProcessVariables();
for(int i = 0 ; i < evironmentKeys.length; i++){
procvars.setValue(evironmentKeys, evironmentValues);
}
// Invoking
ut.loginfo("Process invocation start", _log);
com.jniwrapper.win32.process.Process proc = new com.jniwrapper.win32.process.Process(
accToken,
null,
getCommandLine(),
secAtt1,
secAtt1,
true,
procOptions,
procvars,
workDir,
startInfo
);
Thank you.
- accesstoken.java (12.9 K)
- securityattributes.java (1.3 K)