1

I would like to send a user name and a password to another application, and process the input. so I have Application A which has a window that requires a username and a password. and we have Aplication B that is running. Application B needs to search for application A, login window, and send the user name to a textbox in it and the password, and then process those unputs through the Ok button.

Are there any libraries that can handle those sorts of requirements? Any help weather it be website or dll references or examples would be great

NOTE:- APPLICATION A is not something I built, or have access to its code or anything, I can start it, thats about it.

here is the process just to make things clear since some are confused:- Application B is an EXE application, when clicked, it does some logic, then it starts Application A.

As soon as Application A starts, the user will prompted with a dialog box to enter user name, and password This is not something I made, it is what the application does. My question is can I access this dialog window, and send inputs to it.

FORM CODE

   public partial class Form1 : Form {
    [DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);
    public Form1() {
        InitializeComponent();
        var process = new ProcessStartInfo(@"arcmap.exe");
        var pr=Process.Start(process);
        SetForegroundWindow(pr.Handle);
        SendKeys.Send("ne{TAB}ne{ENTER}");
    }
}

}

ZZZ
  • 285
  • 2
  • 15
  • 2
    Shouldn't the login window just pop up before you open 'application b' as a dialog or something? Why would you use 2 different applications – EpicKip Apr 19 '17 at 07:25
  • is there a reason why this cant be done as 2 windows in the same application? – WhatsThePoint Apr 19 '17 at 07:27
  • The thing is, Application B starts Application A. but I want the whole process to be automative, so when Application A starts a pop up window does appear but I want to fill those values automatically without human interaction. – ZZZ Apr 19 '17 at 07:28
  • Can't you pass the credentials to the other application as a command line argument? – ProgrammingLlama Apr 19 '17 at 07:32
  • Unfortunatly no I cannot do that, Application A happens to be Arcmap xD I dont think I could use command line argument – ZZZ Apr 19 '17 at 07:34

2 Answers2

3

This doesn't sound like a proper design strategy to me. Why not merging the two applications to one and passing the requested values between different application forms?

If for some reason you need to use two different applications, simply open application B AFTER the user has entered his login credentials in application A, and pass those values as parameters to your second app.

You can also consider using a TCP class in order to virtually connect the two of your apps using sockets.

Tommy Naidich
  • 752
  • 1
  • 5
  • 23
  • Reason why I cant merge, is because appliaction A is not something I made, it is no a project, it is an application Arcmap to be specific – ZZZ Apr 19 '17 at 07:35
  • @ZZZ You can't just send something to other application if it has no way to accept it. – Akash Kava Apr 19 '17 at 07:37
  • Explain No way to accept it, since I can input into a dialog window, doesnt it mean it accepts values ? – ZZZ Apr 19 '17 at 07:37
  • As @AkashKava mentioned you need to have control of the main application in order to retrieve the values you're expecting from your second app. – Tommy Naidich Apr 19 '17 at 07:38
  • @ZZZ Ideally and legally you cannot send anything unless application has exposed API in terms of using app the way you want to. You can use `UI Automation` to send username/password to visible elements in Dialog. But you have to read terms and comply. – Akash Kava Apr 19 '17 at 07:41
  • I will look into it, I added to the question since it seems a lot of users are confused on what I was asking – ZZZ Apr 19 '17 at 07:52
2

You'll need to start Arcmap (this is a fairly easy task using Process.Start(string path);, then give it some time to boot using Thread.Sleep(int miliseconds), and then it gets tricky.

You'd have to get the Arcmap process (probably by name) and set it as foreground window by importing this method:

[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);

And then calling it this way:

Process process = Process.GetProcessesByName("arcmapProcessName").FirstOrDefault();
SetForegroundWindow(process);

And later you just send keys using

SendKeys.Send("login{TAB}password{ENTER}");

Then you would have to reference the SendInput function to programatically inject keyboard keypresses into the input stream. Perhaps, since it's a lot of hassle to reinvent the wheel and you're asking for an external library anyway, you could use C# Input Simulator.

  • I tried this code, and it seems that the SendKeys.Send is throwing an exception: SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method. So I tried SendWait but no resuts – ZZZ Apr 19 '17 at 08:53
  • Are you writing a console or a form-less application? – schroedingersKat Apr 19 '17 at 09:38
  • I am writing a formless exe application to automate a process using arcmap. What it does is simply it starts arcmap, calls a command, the command does stuff, then I save the edits. and job is done :). the dialog window beats the (automation) point. so I need to find a way to get rid of it or input username and password automatically – ZZZ Apr 19 '17 at 09:44
  • I'm not 100% sure about this and can't test it right now, but you could try creating a simple, empty form and running whole `SendKeys` part from its constructor. And then simply exiting the application. – schroedingersKat Apr 19 '17 at 09:47
  • That also did no good, It seems t.hat the Arcmap will not accept the keys, and SendKets.Send only work if the login screen was made using windows forms – ZZZ Apr 19 '17 at 10:28
  • You'd have to reference the user32.dll's `SendInput` function, which allows you to programatically insert keystrokes to the input stream - see here: http://stackoverflow.com/questions/12761169/send-keys-through-sendinput-in-user32-dll – schroedingersKat Apr 19 '17 at 10:37
  • Take a look at the form I created, I have referenced the user32.dll but I keep getting the same exception – ZZZ Apr 19 '17 at 10:45
  • I've updated my answer, you might have to simulate keypress events, and since we have a library for that, it might be easier than trying get SendKeys to work – schroedingersKat Apr 19 '17 at 11:23
  • Thanks, I'll give it a try – ZZZ Apr 19 '17 at 11:25