Class StandardHandleInfo
This static class provides type-safe managed methods to determine the true redirection state of a standard console handle, including, if applicable, learning the name of the file to which it is redirected.
Inheritance
Namespace: WizardWrx.ConsoleStreams
Assembly: WizardWrx.ConsoleStreams.dll
Syntax
public static class StandardHandleInfo : object
Remarks
The standard properties on the Process (all versions) and Console (version 4.5 and newer) objects report based solely on redirections brought about by the Base Class Library. They completely ignore redirection that happens in the shell, before the process started. Hence, to learn the true redirection state requires a couple of Platform Invoke calls into the Windows API, which this class provides through type-safe wrappers.
Fields
| Improve this Doc View SourceINVALID_HANDLE_VALUE
When a routine, such as GetStandardHandle cannot return a usable file handle, this is the return value, cast to IntPtr, as is the expected file handle.
Declaration
public static readonly IntPtr INVALID_HANDLE_VALUE
Field Value
Type | Description |
---|---|
IntPtr |
Remarks
Were it not that IntPtr is a structure, INVALID_HANDLE_VALUE would be defined as a public constant.
INVALID_MODULE_HANDLE
When a routine, such as GetModuleHandle cannot return a usable module handle, this is the return value, cast to IntPtr, as is the expected module handle.
Declaration
public static readonly IntPtr INVALID_MODULE_HANDLE
Field Value
Type | Description |
---|---|
IntPtr |
Remarks
Were it not that IntPtr is a structure, INVALID_HANDLE_VALUE would be defined as a public constant.
Methods
| Improve this Doc View SourceGetRedirectedFileName(StandardHandleInfo.StandardConsoleHandle)
Get the name of the file to which a specified standard handle is redirected.
Declaration
public static string GetRedirectedFileName(StandardHandleInfo.StandardConsoleHandle penmStdHandleID)
Parameters
Type | Name | Description |
---|---|---|
StandardHandleInfo.StandardConsoleHandle | penmStdHandleID | Use a member of the StandardConsoleHandle enumeration to designate the desired standard console stream. |
Returns
Type | Description |
---|---|
System.String | If the file is redirected, the returned string contains the fully qualified name of the file to which it is redirected. If the handle is attached to its console (that is, not redirected), the return value is the empty string. |
GetStandardHandleState(StandardHandleInfo.StandardConsoleHandle)
Get the redirection state of a specified standard console stream.
Declaration
public static StandardHandleInfo.StandardHandleState GetStandardHandleState(StandardHandleInfo.StandardConsoleHandle penmStdHandleID)
Parameters
Type | Name | Description |
---|---|---|
StandardHandleInfo.StandardConsoleHandle | penmStdHandleID | Use a member of the StandardConsoleHandle enumeration to designate the desired standard console stream. |
Returns
Type | Description |
---|---|
StandardHandleInfo.StandardHandleState | The return value is one of the following. StandardHandleState.Attached means that the specified stream is attached to its console. StandardHandleState.Redirected means that the specified stream is redirected. Call GetRedirectedFileName to get the fully qualified name of the file to which it is redirected. |
Remarks
In all versions of the framework, the following properties report inaccurately on the true state of the console stream.
- System.Diagnostics.Process.StartInfo.RedirectStandardInput ,
- System.Diagnostics.Process.StartInfo.RedirectStandardOutput ,
- System.Diagnostics.Process.StartInfo.RedirectStandardError ,
The following properties, added in version 4.5, also report incorrectly.
- Console.IsErrorRedirected
- Console.IsInputRedirected
- Console.IsOutputRedirected
The above properties report inaccurately because their report is based entirely on changes made by calls to methods on the Console class. They exhibit no knowledge, whatsoever, of what the underlying operating system does in response to shell commands to redirect one or more of the standard console streams.
On the other hand, since StandardHandleState gets its information from GetConsoleMode, a native Windows API routine, its report takes into account console stream redirections that are the result of shell commands.
GetWin32StatusCode()
Get the last Win32 status code logged by Marshal.GetLastWin32Error.
Declaration
public static Int32 GetWin32StatusCode()
Returns
Type | Description |
---|---|
Int32 | The return value is the value returned by the last call to Marshal.GetLastWin32Error. |
Remarks
As is true of the other methods defined by this class, this method is not yet thread-safe.