Click here to Skip to main content
15,888,401 members
Home / Discussions / C#
   

C#

 
QuestionShow a hidden console again? Pin
Don Rolando15-Sep-11 22:49
Don Rolando15-Sep-11 22:49 
AnswerRe: Show a hidden console again? Pin
V.15-Sep-11 23:16
professionalV.15-Sep-11 23:16 
GeneralRe: Show a hidden console again? Pin
Don Rolando15-Sep-11 23:18
Don Rolando15-Sep-11 23:18 
GeneralRe: Show a hidden console again? Pin
V.15-Sep-11 23:37
professionalV.15-Sep-11 23:37 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 0:23
Don Rolando16-Sep-11 0:23 
GeneralRe: Show a hidden console again? Pin
V.16-Sep-11 0:36
professionalV.16-Sep-11 0:36 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 0:47
Don Rolando16-Sep-11 0:47 
AnswerRe: Show a hidden console again? Pin
Alan N16-Sep-11 3:04
Alan N16-Sep-11 3:04 
There was a post on MSDN about this a while ago, How to obtain a Console Window Handle[^] which uses FindWindow to look for a known window title. It seems ok for Windows XP and I wrote a quick test (with no error checking on the Windows API calls).


C#
public partial class ConsoleControllerForm : Form {
  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);

  [DllImport("user32.dll")]
  private static extern IntPtr FindWindowW(
    [In] [MarshalAs(UnmanagedType.LPWStr)] String lpClassName,
    [In] [MarshalAs(UnmanagedType.LPWStr)] String WindowName);


  private const String KnownConsoleTitle = "TestApp";
  private IntPtr handle;

  public ConsoleControllerForm() {
    InitializeComponent();
  }

  private void StartBtn_Click(object sender, EventArgs e) {
    StartProcess();
    FindCmd();
  }

  private void FindCmd() {
    handle = FindWindowW(null, KnownConsoleTitle);
    ShowBtn.Enabled = handle != IntPtr.Zero;
  }

  private void ShowBtn_Click(object sender, EventArgs e) {
    ShowWindow(handle, 1); // ShowNormal = 1
  }

  private static void StartProcess() {
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = @"TestApp.exe";
    psi.UseShellExecute = true;
    psi.CreateNoWindow = false;
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    Process p = Process.Start(psi);
    // Allow process to start. Can't use WaitForIdleInput
    Thread.Sleep(1000);
  }
}

Note the ProcessStartInfo properties are the only combination for which the subsequent ShowWindow actually shows the window and that TestApp.exe explicitly sets it's title to "TestApp" so I know what to look for with FindWindow.

I doubt this approach will solve your problem completely but hopefully it's given you a start.

Alan.
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 3:19
Don Rolando16-Sep-11 3:19 
AnswerRe: Show a hidden console again? Pin
PIEBALDconsult16-Sep-11 3:19
mvePIEBALDconsult16-Sep-11 3:19 
GeneralRe: Show a hidden console again? Pin
Don Rolando16-Sep-11 3:22
Don Rolando16-Sep-11 3:22 
QuestionDataGridView throws Data Pin
beauroak15-Sep-11 15:15
beauroak15-Sep-11 15:15 
AnswerRe: DataGridView throws Data Pin
Luc Pattyn15-Sep-11 15:29
sitebuilderLuc Pattyn15-Sep-11 15:29 
Questionretrive gridview cell value Pin
jashimu15-Sep-11 9:55
jashimu15-Sep-11 9:55 
AnswerRe: retrive gridview cell value Pin
walterhevedeich15-Sep-11 15:56
professionalwalterhevedeich15-Sep-11 15:56 
Questionhow to write regular expression for this code? Pin
SRKSHOME15-Sep-11 6:48
SRKSHOME15-Sep-11 6:48 
AnswerRe: how to write regular expression for this code? Pin
BillWoodruff15-Sep-11 7:33
professionalBillWoodruff15-Sep-11 7:33 
GeneralRe: how to write regular expression for this code? Pin
SRKSHOME15-Sep-11 19:43
SRKSHOME15-Sep-11 19:43 
AnswerRe: how to write regular expression for this code? Pin
OriginalGriff15-Sep-11 8:26
mveOriginalGriff15-Sep-11 8:26 
GeneralRe: how to write regular expression for this code? Pin
PIEBALDconsult15-Sep-11 9:10
mvePIEBALDconsult15-Sep-11 9:10 
GeneralRe: how to write regular expression for this code? Pin
OriginalGriff15-Sep-11 9:15
mveOriginalGriff15-Sep-11 9:15 
GeneralRe: how to write regular expression for this code? Pin
BillWoodruff15-Sep-11 21:42
professionalBillWoodruff15-Sep-11 21:42 
GeneralRe: how to write regular expression for this code? Pin
BillWoodruff15-Sep-11 21:43
professionalBillWoodruff15-Sep-11 21:43 
GeneralRe: how to write regular expression for this code? Pin
SRKSHOME15-Sep-11 20:21
SRKSHOME15-Sep-11 20:21 
GeneralRe: how to write regular expression for this code? Pin
Pete O'Hanlon15-Sep-11 20:32
mvePete O'Hanlon15-Sep-11 20:32 

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.