Click here to Skip to main content
15,887,683 members
Home / Discussions / C#
   

C#

 
GeneralRe: sending a call to winform Pin
Dave Kreskowiak27-May-13 15:58
mveDave Kreskowiak27-May-13 15:58 
QuestionRedirect standard output of a running process Pin
santosh sk26-May-13 1:12
santosh sk26-May-13 1:12 
AnswerRe: Redirect standard output of a running process Pin
Abhinav S26-May-13 6:31
Abhinav S26-May-13 6:31 
GeneralRe: Redirect standard output of a running process Pin
santosh sk27-May-13 16:05
santosh sk27-May-13 16:05 
AnswerRe: Redirect standard output of a running process Pin
Eddy Vluggen26-May-13 8:15
professionalEddy Vluggen26-May-13 8:15 
GeneralRe: Redirect standard output of a running process Pin
santosh sk27-May-13 16:25
santosh sk27-May-13 16:25 
GeneralRe: Redirect standard output of a running process Pin
Eddy Vluggen28-May-13 9:18
professionalEddy Vluggen28-May-13 9:18 
GeneralRe: Redirect standard output of a running process Pin
santosh sk6-Jun-13 16:24
santosh sk6-Jun-13 16:24 
Following code useful when trying to get the ouput of running console process:
------------------------------------------------------------------------------

Required APIs:

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("kernel32.dll")]
private static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput, [Out] char[] lpCharacter, uint nLength, COORD dwReadCoord, out uint lpNumberOfCharsRead);

[DllImport("kernel32.dll",
EntryPoint = "GetStdHandle",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32", SetLastError = true)]
static extern bool AttachConsole(uint dwProcessId);

[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FreeConsole();

-------------------- METHODS to get the output ---------------------

private const string strProcessName = "Process_Name";

private struct COORD
{
public short X;
public short Y;
public COORD(short X, short Y)
{
this.X = X;
this.Y = Y;
}
};

private string ReadRTConsole()
{
IntPtr rthandle = IntPtr.Zero;
char[] buffer = null;
string output = "";
int processId = 0;

Process[] processlist = Process.GetProcesses();

foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName.ToLower().Equals(strProcessName))
{
processId = theprocess.Id;
break;
}
}

if (AttachConsole((uint)processId))
{

rthandle = GetStdHandle(-12);

buffer = new char[Console.BufferWidth];

StringBuilder textBuilder = new StringBuilder();
for (int i = 0; i < Console.BufferHeight; i++)
{
textBuilder.AppendLine(GetLine(buffer, rthandle, i));
}
output += textBuilder.ToString().TrimEnd();
Console.Clear();
}

FreeConsole();

return output;
}

private string GetLine(char[] buffer, IntPtr rthandle, int line)
{
uint garbage;
if (!ReadConsoleOutputCharacter(rthandle, buffer, (uint)buffer.Length, new COORD(0, (short)line), out garbage))
{
return "";
}
return new string(buffer).TrimEnd();
}
GeneralRe: Redirect standard output of a running process Pin
Eddy Vluggen6-Jun-13 23:02
professionalEddy Vluggen6-Jun-13 23:02 
QuestionCheck for updates Pin
John_Tadros26-May-13 1:04
John_Tadros26-May-13 1:04 
AnswerRe: Check for updates Pin
Member 1007416126-May-13 1:29
Member 1007416126-May-13 1:29 
AnswerRe: Check for updates Pin
Richard MacCutchan26-May-13 2:36
mveRichard MacCutchan26-May-13 2:36 
AnswerRe: Check for updates Pin
Mycroft Holmes26-May-13 3:34
professionalMycroft Holmes26-May-13 3:34 
AnswerRe: Check for updates Pin
MaulikDusara26-May-13 18:25
MaulikDusara26-May-13 18:25 
Questionvery off bug Pin
Member 1007416125-May-13 22:46
Member 1007416125-May-13 22:46 
AnswerRe: very off bug Pin
SoMad25-May-13 23:36
professionalSoMad25-May-13 23:36 
QuestionWant to chat through ip address Pin
Ramans44425-May-13 21:24
Ramans44425-May-13 21:24 
GeneralRe: Want to chat through ip address Pin
harold aptroot25-May-13 21:30
harold aptroot25-May-13 21:30 
GeneralRe: Want to chat through ip address Pin
Ramans44425-May-13 21:40
Ramans44425-May-13 21:40 
AnswerRe: Want to chat through ip address Pin
dusty_dex26-May-13 0:57
dusty_dex26-May-13 0:57 
QuestionAssign Mutex to each Thread Pin
RoyDoron25-May-13 5:28
RoyDoron25-May-13 5:28 
AnswerRe: Assign Mutex to each Thread Pin
Dave Kreskowiak25-May-13 14:08
mveDave Kreskowiak25-May-13 14:08 
Questionc# question about gui standard location on every screen Pin
User349025-May-13 4:55
User349025-May-13 4:55 
AnswerRe: c# question about gui standard location on every screen Pin
Abhinav S25-May-13 5:08
Abhinav S25-May-13 5:08 
Questionint to string Pin
Member 1007416125-May-13 1:17
Member 1007416125-May-13 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.