|
DelphiDabbler Wiki |
FAQs /
ConsoleAppClassesConsole Application Runner Classes FAQThis page has some frequently asked questions about the DelphiDabbler Console Application Runner Classes. You can also try the class' documentation. If you still can't find your answer add your question to the Unanswered Questions page. Contents
1: Why am I getting an access violation when running code that uses TPJConsoleApp compiled with with Delphi 2009 (and later)? This is because Delphi 2009 and later (the "Unicode Delphis") use the Unicode Windows API (earlier versions used the ANSI API). It is a peculiarity of the Unicode implementation of the CreateProcess API function (which TPJConsoleApp uses) that causes this access violation. This peculiarity wasn't present in the ANSI version of the function. Check this post for an explanation of the issue. The classes have been updated to work round this problem. You need to update to release 1.0.2 or later.
2: Does TPJConsoleApp handle Unicode output from console applications correctly? There is no straightforward answer to this. Because TPJConsoleApp can run any console app it can know nothing about the output of programs it is used to run. In the case where the program's output is displayed in the console window the question is irrelevant. If the output is being redirected to a file then the output is a faithful byte by byte copy of the console program output. If the program writes Unicode text then that's what the file will contain. If output is redirected to the pipe then, again, the actual bytes output by the program are written to the pipe. The programmer is responsible for reading the pipe and must make sure that any structured data is re-assembled correctly. Each time the pipe is read it is possible it may contain an odd number of bytes, which is not valid Unicode. But I stress, it is up to the programmer to handle this - it is not a function of TPJConsoleApp. New in release 2.0 of the classes are some pipe filter classes, one of which can read Unicode from a pipe correctly. See this example.
3: In the command shell running "MyApp.exe >out.txt" redirects MyApp's output to out.txt. Why doesn't this command redirect when run with TPJConsoleApp? The ">" redirection operator in The operator has no meaning outside the command shell. To see this try executing a command line containing the ">" operator via the Windows Run dialog box [Windows Key + R]. It won't work. Using the Windows API to execute such a command also fails: this is what TPJConsoleApp does. TPJConsoleApp can do redirection, but you have to do a lot of the work yourself that the command shell does behind the scenes. To understand this let's take a look at what the command shell does when it encounters the ">" operator. First the shell creates a new empty file named We have to do the same to use TPJConsoleApp for redirection. First create the file ensuring its handle is inheritable (FAQ 5 explains how). Assign the file handle to TPJConsoleApp's StdOut property. Finally call Execute with a comamnd line parameter of Redirection using the "2>" (redirect standard error output) and "<" (redirect standard input) operators is similar. Open the required files for output (standard error) or input (standard input) and assign the (inheritable) handles to TPJConsoleApp's StdErr or StdIn properties respectively. 4: In the command shell I can pipe data between applications. How can I do this with TPJConsoleApp? Awaiting answer edit page 5: How do I create or open a file with an inheritable handle? This question is answered in Appendix 1 of the Console Application Runner Classes documentation. |