Can I send data to a console app from a win form

G

Guest

Hi,

I need to send data to a running console app. The console app is a kind of
terminal service application. This console app has a input text box and I
need to send a text string to fill in this text box. So the user can hit
enter to run his consle app. How can I do it? It assumed that the console
is always up and running and waiting for input. Thanks in advance.

Steve
 
J

Justin Rogers

There are some .NET features that would help you out, but I'd highly recommend
going over to pinvoke.net and grabbing the Win32 API's for setting window focus
and sending windowed input. You can get the process id information from
System.Diagnostics.Process and you can also use the
System.Windows.Forms.SendKeys
class to send the keystrokes, but does anyone have an out of the box solution
for
setting the focus to the non .NET application?
 
G

Guest

hi, Justin:

Thank you very much for the info of how-to. .After sending several hours, I
have come up the code to let anybody who needs it - Good community ethics.

I still need help to correct the problem described below.

In order to run it, just copy the code to create a file called Form1, in
your project. You need to open a notepad and with default window name as
"Untiled - Notepad" as the other "application".

Now the problem:

It is working fine, except I want it to be handled by Form_Activated event
everytime when the form gets focused. I found the sending is done every
other activatiion, not every time. I have a counter and it shows only even
number and the text ("abc" ) sent twice, it seems that the text get
accumulated and then flushed every other time.

====THE CODE =========

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace Test1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private int _counter = 0;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow( IntPtr hWnd );

[DllImport("User32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint
lpdwProcessId);

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

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
_counter = 0;

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Count:";
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(88, 24);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(292, 271);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Activated += new System.EventHandler(this.Form1_Activated);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void SendSomeText()
{
IntPtr hwndApp = FindWindow( "Notepad", "Untitled - Notepad" );
IntPtr hwndForeground = IntPtr.Zero;
uint threadForeground = 0;


// NOTE: It does not make difference to AttachThread the threadForeground,
so being commented out


// if( !hwndApp.Equals( IntPtr.Zero ) &&
// !hwndApp.Equals( GetForegroundWindow() ) )
// {
// GetWindowThreadProcessId( hwndForeground, out threadForeground );
// AttachThreadInput( threadForeground, 0, true);

SetForegroundWindow( hwndApp );

PressKey( 0x41 ); // "a"
PressKey( 0x42 ); // "b"
PressKey( 0x43 ); // "c"
PressKey( 0x0d ); // VK_RETURN

// AttachThreadInput( threadForeground, 0, false);
// }
}

private void PressKey( byte keyCode )
{
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
keybd_event( keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0 );
keybd_event( keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0 );
}

private void Form1_Activated(object sender, System.EventArgs e)
{
textBox1.Text = (_counter++).ToString();
SendSomeText();
}
}
}

====== END OF CODE ==============

- Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top