C# – Send data to other application

When I surfed on forum Updatesoft for reading some news about Tet holidays in Vietnam I found an interesting disscussion about Yahoo Messenger. In this thread one asked about how to brute force password of Yahoo Messenger because he knows how long it is but can not remember which one is correct. After trying entering some possible passwords, his account was banned. It is completely clear that man can not recover password from Yahoo Messenger. What makes me interested is Yahoo simply blocks account after some false passwords. Let’s consider this case when I would like to send someone in trouble, I just take his ID and enter some false passwords and he will be banned from Yahoo Messenger in 24h. He can not contact with his friends anymore.

Based on this idea, I would like to code a small tool to test my idea. That means this tool should automatically enter the ID, random password into Yahoo Messenger , click button OK to login, close error window of false password, click button OK to login again,…. repeat these steps until Yahoo ID is locked. However this tool can be “dangerous” one if I publish it to internet. Therefore in this small blog, instead of sending data to Yahoo Messenger, I will send to MySQL Login Dialog.

MySQL Login Dialog

In the login dialog of MySQL above you can see there are 4 fields we need to fill out so that we can connect to MySQL Server. These fields are child components of a groupbox. This groupbox is again a child of dialog. To fill out these fields we must find out the handle of each control and set text for them. To locate the controls we can use 2 Windows API functions FindWindow and FindWindowEx.

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

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

These WinAPIs will find control with given class name and give its handle back. The class name of each control can be got through Spy++, a free Visual Studio tool.

Spy++

After getting the class name of each control, let’s start to get handle of all of controls on login dialog

IntPtr hwndMainWindow = WinAPI.FindWindow("TConnectToInstanceForm.UnicodeClass", "MySQL Administrator 1.2.17 ");
IntPtr hwndGroupBox = WinAPI.FindWindowEx(hwndMainWindow, IntPtr.Zero, "TTntGroupBox.UnicodeClass", "Mit MySQL-Serverinstanz verbinden");
IntPtr hwndUserName = WinAPI.FindWindowEx(hwndGroupBox, IntPtr.Zero, "TTntComboBox.UnicodeClass", IntPtr.Zero);
IntPtr hwndConnection = WinAPI.FindWindowEx(hwndGroupBox, hwndUserName, "TTntComboBox.UnicodeClass", IntPtr.Zero);
IntPtr hwndServerHost = WinAPI.FindWindowEx(hwndGroupBox, hwndConnection, "TTntComboBox.UnicodeClass", IntPtr.Zero);
IntPtr hwndPort = WinAPI.FindWindowEx(hwndGroupBox, IntPtr.Zero, "TTntEdit.UnicodeClass", IntPtr.Zero);
IntPtr hwndPassword = WinAPI.FindWindowEx(hwndGroupBox, hwndPort, "TTntEdit.UnicodeClass", IntPtr.Zero);
IntPtr hwndPanel = WinAPI.FindWindowEx(hwndMainWindow, IntPtr.Zero, "TTntPanel.UnicodeClass", IntPtr.Zero);
IntPtr hwndOKButton = WinAPI.FindWindowEx(hwndPanel, IntPtr.Zero, "TTntButton.UnicodeClass", "&OK");

In the code block above, you can see that the reading sequences is not same as the orders of controls in form, for example I read the handle of textbox user name before the handle of combo box “Gesp. Verbindung” although the combo box lies above the text box in form. Why? Because with the function FindWindowEx I can get the handle of next child control of given control, but the next child control is not determined through position on form but through initilization of controls in source code . So I can not say which control has current handle given back from function FindWindowEx. To solve this problem, I must combine the result of FindWindowEx and Spy++ to find out which control has the handle .

After getting the handle I can write the neccessary info into this dialog with SendMessage API and click button OK to make a connection.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam);

...

Console.WriteLine("WRITING INFO TO MAIN WINDOW");
Console.WriteLine("----------------------------------");
//Set server host
Console.WriteLine("Writing server host...");
WinAPI.SendMessage(hwndServerHost, WM_SETTEXT, 0, new StringBuilder("192.168.200.185"));
//Set port
Console.WriteLine("Writing port...");
WinAPI.SendMessage(hwndPort, WM_SETTEXT, 0, new StringBuilder("4136"));
//Set username
Console.WriteLine("Writing username...");
WinAPI.SendMessage(hwndUserName, WM_SETTEXT, 0, new StringBuilder("nurlesen"));
//Set password
Console.WriteLine("Writing wrong password...");
WinAPI.SendMessage(hwndPassword, WM_SETTEXT, 0, new StringBuilder("123456"));
//Click OK
Console.WriteLine("Click OK to connect...");
WinAPI.SendMessage(hwndOKButton, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
Console.WriteLine("----------------------------------");
Console.WriteLine("\n");

The code above is just for outlining my idea that one can make an auto-tool for bruforcing YM passwords by entering ID and password automatically. In addition, we can learn how to send data to other applications through Windows API like FindWindow, FindWindowEx and SendMessage. The complete source code of this example you can download here “Send data to other application

5 thoughts on “C# – Send data to other application”

  1. Lão có làm được với thằng iTunes bằng cách này ko ? Có thời gian lão thử bấm vô menu Store bên trái, rồi Login thử coi :))

    Hình như cách này áp dụng để gọi Menu, tree-item hơi bị khó thì phải ?

    À, bên .Net có cái White framework gì đó cũng dùng để automation soft mà chưa nghiên cứu nữa 😀

  2. Chưa thử với iTunes lão ah. Để khi nào rãnh mò thử xem có được không. Cái White Framework tui không biết, để từ từ ngâm cứu xem sao.

  3. uhm. Tại có 1 số soft mình ko dễ gì lấy item control dễ dàng để mà thao tác được 😀

    Chắc để khi nào rãnh thử xem. Mà nếu lão rãnh thử trước thì cho kết quả nghe 😀

Leave a Reply

Your email address will not be published. Required fields are marked *