2

Ok I have an app named Sharknadoo that I created,what this app does it reads the value from a combobox from 1 to whatever number and creates that number of textboxes in the right of it.enter image description here

enter image description here

Now let us presume I do not have the code for this sharknadoo app just the app installed on my desktop.My question is how can I send my listbox.items from "My amazing app" to the sharknadoo textboxes? Presuming I have the same number of items in my listbox as I have textboxes in my other app.I am sorry but I really want to learn how to do this ,someone told me it is possible to achieve but I have no idea on how to achieve it was thinking about using coordinates or something like that,but from what I understood you can even hang on to the fact that the sharknadoo app is using textboxes without even having access to its source code.Thank you in advance friends :D. enter image description here

  Process[] processes = Process.GetProcessesByName("Sharknadoo.exe");
            int i = 0;
            foreach (Process p in processes)
            {
                IntPtr windowHandle = p.MainWindowHandle;
                string item = listBox1.Items[i].ToString();
                listBox1.Items.Add(item);
                i++;         
            }

I realize the logic of my code is not good but it's all I could come up with.

John Pietrar
  • 513
  • 7
  • 19
  • 3
    You're looking for "_interprocess communication_". – cbr Jun 08 '16 at 08:42
  • I tried reading about this until now ,from what I read there is a way to achieve this using FindWindow and WM_COPYDATA but I cannot understand how... – John Pietrar Jun 08 '16 at 10:18
  • 2
    You could, but that's a bit archaic. I'd stick to using IPC, it's worth learning, and there are lots of options. For something this simple, app-to-app on a machine, I'd look into [IPC using Named Pipes](https://msdn.microsoft.com/en-us/library/bb546085(v=vs.110).aspx). – DonBoitnott Jun 08 '16 at 11:10
  • @DonBoitnott If it is that simple,can you show me a direct example to my problem on how to achieve this? – John Pietrar Jun 08 '16 at 13:09
  • @JohnPietrar Do you only have source code for `My Amazing App`? If you have source code of both applications, you can use `WCF` to communicate between applications. But if you don't have source code of `Sharknadoo`, then you should find those `TextBox` handles and send a `WM_SETTEXT` to those handles. Let me know what's the case? – Reza Aghaei Jun 11 '16 at 22:00
  • I got the source code only for my amazing app and not for sharknadoo if I had for both it wouldn't be that hard :D – John Pietrar Jun 12 '16 at 22:16

1 Answers1

2

This answer follows similar logic to your code, but instead simulates keyboard strokes and relies on using TAB to navigate boxes, but it should work in your case.

First add some code that we will use later to grab a link to your Sharknadoo application:

// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

Now assuming you have not touched anything in the app (Very dangerous assumption, it would be better to launch Sharknadoo from your code before doing any of this), the tab index should be at 0 so we can do something like the following when you click the "Send to Sharknadoo" button:

// Send a your array of names to the Sharknadoo application.
public void sendToSharknadoo(String[] detailsToSend)
{
    // Get a handle to the Sharknadoo application. The window class
    // and window name can be obtained from Sharknadoo using the
    // Spy++ tool.
    IntPtr windowHandle = FindWindow("SharknadooFrame","Sharknadoo");

    // Verify that Sharknadoo is a running process.
    if (windowHandle == IntPtr.Zero)
    {
        MessageBox.Show("Sharknadoo is not running.");
        return;
    }

    // Make Sharknadoo the foreground application and set the number 
    // of text boxes for your info
    SetForegroundWindow(windowHandle);
    // Get to first box
    SendKeys.SendWait("{TAB}");
    // enter number of boxes
    SendKeys.SendWait("{DOWN}");
    SendKeys.SendWait((string)detailsToSend.Length);

    // Now enter your details into each of those boxes
    foreach (String s in detailsToSend)
    {
        // Get next textbox box
        SendKeys.SendWait("{TAB}");
        // enter text into box
        SendKeys.SendWait(s);
    }
}

With any luck that will do the trick. However you will probably need to mess with the order a bit put some checks in place.

Note: if you want a faster more aggressive approach that should execute before the user can interfere then try SendKeys.Send() instead of SendKeys.SendWait()


Source:

https://msdn.microsoft.com/en-us/library/ms171548(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys(v=vs.110).aspx

Additional Stack Overflow questions like this one:

Insert text into the textbox of another application

Community
  • 1
  • 1
sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • SendKeys.SendWait((string)detailsToSend.Length); it tells me the name does not exist in current context. – John Pietrar Jun 13 '16 at 09:32
  • I dont actually have a C# IDE installed to test my code, but it should work correctly as long as you actually declare the method like so: `public void sendToSharknadoo(String[] detailsToSend)` Obviously you will need to write some code so you can send an array of strings to the method in my answer. Additionally if that was not the issue then it may be an issue with pointers since you are passing a sting to an external application, so maybe this will make a difference: `SendKeys.SendWait("" +detailsToSend.Length);` – sorifiend Jun 13 '16 at 12:58
  • 1
    @JohnPietrar What name does not exist? I feel like you would get that error if you have a spelling mistake somewhere in that line `SendKeys.SendWait((string)detailsToSend.Length);` – Zack Jun 16 '16 at 15:18