0

I have a program which needs to send every line of a text file to an application while minimized. I am able to send the text to the application but this causes it to be opened. Is there a way to do this while the application is minimized? I have read that I could use postmessage or sendmessage but am unsure how to do this.

public partial class Form1 : Form
{
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindow(string lp1, string lp2);

    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        IntPtr handle = FindWindow(null, "Application");
        if (!handle.Equals(IntPtr.Zero))
        {
            if (SetForegroundWindow(handle))
            {
                string[] dic = System.IO.File.ReadAllLines(@"text file name");
                foreach (string a in dic)
                {
                    SendKeys.Send(a);
                }
            }
        }
    }
}
Allofthem
  • 9
  • 2
  • Can you change the codes of other application? or it is black box and you can't change it? – Reza Aghaei Sep 19 '15 at 21:41
  • Can you explain a little more what you are trying to achieve, is there a reason you are using sendkeys to pass the text over? (e.g. are you trying to replicate someone typing into the window)? – ajg Sep 19 '15 at 21:41
  • Yes I am trying to simulate typing in an application while it is minimized – Allofthem Sep 19 '15 at 21:44
  • the old days, they trick was to write a file and the other app would listen to that file and read it... nowdays, web sockets if webapps or services (windows service, web service), tcp/ip with long pooling, it's all examples... – balexandre Sep 19 '15 at 21:55

1 Answers1

1

It's not clear from the question what "send the text" means. But, if you want to set the text in a text-box in the target application, then you're in luck.

You can get a handle do the text box control you want the text to go to in the target application. This is going to be the most difficult part because you will have to figure out a way to distinguish that text box from others the application may have (probably using a combination of Spy++ and trial-and-error).

Once you have figured out the text control you want to send the text to, the rest is just sending a WM_SETTEXT message to the other application's text box control.

Here is another SO relevant question for you to reference: SetText of textbox in external app. Win32 API

Community
  • 1
  • 1
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151