Windows Develop Bookmark and Share   
 index > Windows Forms General > Write to process??
 

Write to process??

Hi all.

How can i write to a process window (Console)?
Lets say i got a chat server running, if i whant to ban a user i need to write (.ban (username) from (room) done) on the console window and press Enter.

I whant to make a winform app that gots 2 texboxes texbox1(Username) and textbox2(Room) and a button name Enter to make banning user more faster, just type the name to textbox1 and room to texbox2 and press the enter button and your ban.

how can i make my app write what i type in my textboxes to my chat server console?

thx in advances.

James0886  Wednesday, June 27, 2007 10:46 PM

Hi, James0886,

You can send your string to the console with windows API.

Please check the following codes.

Code Snippet

public Form1()

{

InitializeComponent();

}

[DllImport("user32.dll")]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]

public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

const int WM_CHAR = 0x0102;

IntPtr hwnd_win;

private void Form1_Load(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

hwnd_win = FindWindow(null, @"C:\WINDOWS\system32\cmd.exe");//Your console name

string username = "abcdefg";

string password = "bcdefgh";

string enter = "\r";

WriteToConsole(username);

WriteToConsole(password);

WriteToConsole(enter);

}

private void WriteToConsole(string str)

{

char[] chars = str.ToCharArray();

Message msg;

foreach (char c in chars)

{

msg = Message.Create(hwnd_win, WM_CHAR, new IntPtr(c), new IntPtr(0));

PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);

}

}

Hopes this helps,

Regards

Yu Guo â€?MSFT  Friday, July 13, 2007 1:26 AM

Hi, James0886,

You can write to a console window in this way,

Code Snippet

//.......

using System.Runtime.InteropServices;

namespace Test
{
public class Win32
{
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();


[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
}


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Win32.AllocConsole();
Console.WriteLine("abc");
}

private void button2_Click(object sender, EventArgs e)
{
Win32.FreeConsole();
}

}
}

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, June 29, 2007 2:00 AM
Thx but i whant to write to a running process not to the same app.
i whant to write to the process window.(console) not on the program console window.
James0886  Thursday, July 12, 2007 4:56 PM

Hi, James0886,

You can send your string to the console with windows API.

Please check the following codes.

Code Snippet

public Form1()

{

InitializeComponent();

}

[DllImport("user32.dll")]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]

public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

const int WM_CHAR = 0x0102;

IntPtr hwnd_win;

private void Form1_Load(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

hwnd_win = FindWindow(null, @"C:\WINDOWS\system32\cmd.exe");//Your console name

string username = "abcdefg";

string password = "bcdefgh";

string enter = "\r";

WriteToConsole(username);

WriteToConsole(password);

WriteToConsole(enter);

}

private void WriteToConsole(string str)

{

char[] chars = str.ToCharArray();

Message msg;

foreach (char c in chars)

{

msg = Message.Create(hwnd_win, WM_CHAR, new IntPtr(c), new IntPtr(0));

PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);

}

}

Hopes this helps,

Regards

Yu Guo â€?MSFT  Friday, July 13, 2007 1:26 AM
thx alot man.
James0886  Friday, July 13, 2007 6:49 PM
I can only write to cmd.exe.
if i change to myconsole.exe it dosent works.
why you think this happens?
Thx in advance.
James0886  Saturday, July 14, 2007 8:56 PM

Hi, James0886,

Please change the console name to thecaption of your application window.

for example,

Code Snippet

null, @"C:\WINDOWS\system32\cmd.exe");//Your console name

to

Code Snippet

null, @"file:///E:\AppDev\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe");//Your console name

Hopes this helps,

Regards

Yu Guo â€?MSFT  Monday, July 16, 2007 1:17 AM
i did. but it not doing nothing to the myconsoleapp.exe, it only works with cmd.exe
James0886  Monday, July 16, 2007 7:16 PM
Hi, James0886,

I have tested the codes with a simple project, and it works fine.
Please check the window title carefully,
it should be exactly the same with the console name in your program.
If it won't work, please post your codes.

Regards.
Yu Guo â€?MSFT  Tuesday, July 17, 2007 12:59 AM
thx alot i suck at typing.
You safe my life.l
Bless you Yu Guo.
James0886  Thursday, July 19, 2007 10:04 PM

You can use google to search for other answers

Custom Search

More Threads

• Draw pie or bar graphs???
• Transparency "flashes" (not flicker) issue during form load
• ListView control setting height with no scrolling
• Background Worker Problem
• FTP via .NET windows form? How to?
• Clear Datagrid past records
• How do I simultaneously display a container and its child controls?
• color datagrid view cell
• C# 3.0 - How to replace "this" when calling the property from someother class
• How do I design controls with my own design events?