CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2012
    Posts
    20

    windows form .call console and give commands

    I cant find solution for that
    Its a windows forum
    on button click open console and readline to assign the value to $tring or int array
    and later show in mbox

    in console app.itss so easy
    but on windows form applcation how to call console? and assign value to $tring or int

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: windows form .call console and give commands

    What you do is launch the [console] application using the Process class and then redirect the stdinput/stdoutput to the launching program.

  3. #3
    Join Date
    Dec 2012
    Posts
    20

    Re: windows form .call console and give commands

    thank you sir AMArjay.but can you give an ex? or some piece of code how to do?

  4. #4
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: windows form .call console and give commands

    You can P/Invoke AllocConsole():
    Code:
    [DllImport("kernel32.dll")]
    static extern bool AllocConsole();
    
    private void MainMethod()
    {
    	if (AllocConsole())
    	{
    		Console.WriteLine("Hello world!");
    	}
    	else
    	{
    		// Error?
    	}
    }
    Shouldn't need to use the Process class here if you just want to open a console window.

    You may want this link as reference: http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    [sigpic][/sigpic]
    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  5. #5
    Join Date
    Dec 2012
    Posts
    20

    Re: windows form .call console and give commands

    still can not solve my problem.please give me some more line of code

  6. #6
    Join Date
    Dec 2012
    Posts
    20

    Re: windows form .call console and give commands

    Quote Originally Posted by AceInfinity View Post
    You can P/Invoke AllocConsole():
    Code:
    [DllImport("kernel32.dll")]
    static extern bool AllocConsole();
    
    private void MainMethod()
    {
    	if (AllocConsole())
    	{
    		Console.WriteLine("Hello world!");
    	}
    	else
    	{
    		// Error?
    	}
    }
    Shouldn't need to use the Process class here if you just want to open a console window.

    You may want this link as reference: http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    I think you shared me c++ code and c++ link.its c#

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: windows form .call console and give commands

    If I understand the problem, you are trying to set the input and read the output of a console app? If so, then AllocConsole isn't going to help.

    AllocConsole only helps if you want to open a console window to your process, and if you are trying to do that there is an even easier way to create a console window for your forms app - that is to simply set the application type of your forms app to 'console app'. A console window will then open along side the forms window.

    But as I said, that isn't going to help if the console window you are interested in is in another process. If you are trying to send and receive text in another console app process, you are going to have to open that process with the Process class and redirect stdin and stdout as I mentioned earlier.

    There is a lot of help available to do this - just search bing or google for "redirect stdinput process C#".

    Here's is one such article...

    http://www.codeproject.com/Articles/...t-of-an-applic

  8. #8
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: windows form .call console and give commands

    Quote Originally Posted by yousufshah View Post
    I think you shared me c++ code and c++ link.its c#
    I know I did, this is why I mentioned "P/Invoke" http://msdn.microsoft.com/en-us/library/aa446536.aspx

    That code I shared with you is not C++ either.

    Quote Originally Posted by Arjay View Post
    If I understand the problem, you are trying to set the input and read the output of a console app? If so, then AllocConsole isn't going to help.

    AllocConsole only helps if you want to open a console window to your process, and if you are trying to do that there is an even easier way to create a console window for your forms app - that is to simply set the application type of your forms app to 'console app'. A console window will then open along side the forms window.

    But as I said, that isn't going to help if the console window you are interested in is in another process. If you are trying to send and receive text in another console app process, you are going to have to open that process with the Process class and redirect stdin and stdout as I mentioned earlier.

    There is a lot of help available to do this - just search bing or google for "redirect stdinput process C#".

    Here's is one such article...

    http://www.codeproject.com/Articles/...t-of-an-applic
    I read this:
    I cant find solution for that
    Its a windows forum
    on button click open console and readline to assign the value to $tring or int array
    and later show in mbox

    in console app.itss so easy
    but on windows form applcation how to call console? and assign value to $tring or int (*array?)
    1. "It's a Windows Form (application)"
    2. "On a button click, I want to open console and readline to assign the value to a string or integer array and later show in a MessageBox."
    3. "In a console app, it is so easy, but on windows form applcation how do I call the console?"

    He didn't mention anything about a console based program, he only mentioned calling the console. If he wanted user input to the console, then he would have to create a program to do this and just act as a variable initializer, which I don't think is what he's doing...

    I could be wrong, but he never mentioned a console program at all. He only asked how to call the console. I would wonder why he doesn't just use a TextBox though? Using the console just for user input to a string variable seems unethical to me.

    I just went by exactly what he said he wanted to do, and no more.

    Last edited by AceInfinity; February 24th, 2013 at 08:23 PM.
    [sigpic][/sigpic]
    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: windows form .call console and give commands

    Quote Originally Posted by AceInfinity View Post
    He didn't mention anything about a console based program, he only mentioned calling the console. If he wanted user input to the console, then he would have to create a program to do this and just act as a variable initializer, which I don't think is what he's doing...

    I could be wrong, but he never mentioned a console program at all. He only asked how to call the console. I would wonder why he doesn't just use a TextBox though? Using the console just for user input to a string variable seems unethical to me.

    I just went by exactly what he said he wanted to do, and no more.
    Yeah, sometimes it's tough trying to figure out what an OP is trying to do.

    Generally, many [beginner] folks confuse a console app with the console window that Windows displays the app in. Because of this, they think the way to input data into a console app is to somehow enter it into the console window. Likewise, to get the output of a console app, they usually think they need to somehow scrape the console window.

    While this can be done, it generally isn't the best approach in terms of robustness, so the more common approach is to use the redirect method I've mentioned.

    Perhaps the OP will post back to clarify?

  10. #10
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: windows form .call console and give commands

    Quote Originally Posted by Arjay View Post
    Yeah, sometimes it's tough trying to figure out what an OP is trying to do.

    Generally, many [beginner] folks confuse a console app with the console window that Windows displays the app in. Because of this, they think the way to input data into a console app is to somehow enter it into the console window. Likewise, to get the output of a console app, they usually think they need to somehow scrape the console window.

    While this can be done, it generally isn't the best approach in terms of robustness, so the more common approach is to use the redirect method I've mentioned.

    Perhaps the OP will post back to clarify?
    You're not wrong though, I'm not saying that. I'll wait and see until OP can clarify what he's willing to do before going further in a specific direction. It's like a forest, you don't start going in any one direction unless you know where you are, and where you want to be headed, otherwise you're more likely to get lost.

    Cheers!
    [sigpic][/sigpic]
    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: windows form .call console and give commands

    Say Ace, were you able to make the MVP Summit in Bellevue last week?

  12. #12
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: windows form .call console and give commands

    Quote Originally Posted by Arjay View Post
    Say Ace, were you able to make the MVP Summit in Bellevue last week?
    Nope... Sadly. I did want to go, but no real way of transportation for me, and I am of the slightly younger audience among MVP's. It would have been a good opportunity to meet usasma and jcgriff, who I know fairly well since I was initially awarded in 2012.
    [sigpic][/sigpic]
    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: windows form .call console and give commands

    If we're all re-awarded next year, we'll have to invite you to the infamous Code Guru Vodka night.

  14. #14
    Join Date
    Dec 2012
    Posts
    20

    Re: windows form .call console and give commands

    thanks dear. Now time to study all comments

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured