2

I have several questions all related to a windows application that also runs as a console application.

In my programs entry point I have logic to determine if command line parameters are being passed in and, if so, I call AllocConsole(). This works well enough but I am curious if it is possible to not pop a new console window. So the expectation is that a user will be calling this from a command window so I would like to have the output from the application go into the window the user is currently working in, not in some other screen. Additionally the console window that is popped via AllocConsole() is less than user friendly. For example if the user miskeys an argument the new window will open, inform the user of their mistake, then the user has to go back to their original window to fix the mistake, and run the command again. Basically I am looking for a more traditional command line experience. Is this possible in my scenario?

The next question is related to formatting Console output. I have read several articles here on SO and on the MSDN but I can't seem to find a good answer. I am currently doing something like this:

 Console.WriteLine("{0,0}\t\t{1,10}", item.Key, item.Value);

This works well enough but it is entirely possible that the value in item.Value will be several lines long thus wrapping several lines. I am looking for a more traditional 'table' layout where the text for item.Value would still wrap lines but be confined to its own column and not wrap underneath the value of item.Key. Is this possible and, if it is, what is the best way to go about it?

Thanks!

2
  • Rather than a forms application that opens a console conditionally why not just have a console application that opens a window conditionally? Commented Nov 12, 2012 at 15:28
  • That is a possibility although it would require a bit of restructuring of the solution I am working with. Commented Nov 12, 2012 at 16:14

1 Answer 1

1

I think AttachConsole is what you are looking for. Pinvoke - AttachConsole contains correct signature and sample. The signature copied below from www.pinvoke.net:

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

...(!AttachConsole(ATTACH_PARENT_PROCESS)...
Sign up to request clarification or add additional context in comments.

3 Comments

That notoriously works poorly, the output of cmd.exe gets intermingled with yours and both programs will be fighting over console input. Btw, make the argument int so you can pass -1.
@HansPassant, thanks for comment - never needed this function in real life... As for int/uint - matches signature of native on and you still can pass "-1" (which is "not possible" even with native one that takes DWORD): (uint)(0xFFFFFFFF) i.e. by defining constants as PInvoke's version.
@HansPassant Can you explain that a bit more in depth, namely what you mean by 'programs will be fighting over console input' ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.