This forum has been archived. All content is frozen. Please use KDE Discuss instead.

C#, Qyoto, Qt - Accessing QTextEdit from another program

Tags: None
(comma "," separated)
robertsmith
Registered Member
Posts
3
Karma
0
I have a program written in C++ using a QTextEdit textbox. I have another program written in C#, which I need to access a QTextEdit that is in the C++ program. Is there a way possibly with using the QProcess class and Qyoto to get the text from this textbox? FindWindow/FindWindowEx windows api did not work.

Thanks Before Hand
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
I would suggest building in a native mechanism into the C++ program to enable the C# program to retrieve the value of the textbox. This could be done via sockets, D-Bus, etc.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
robertsmith
Registered Member
Posts
3
Karma
0
That would be great except for the fact that I do not have access to the source code for the C++ program. I only have access to the C# program's source code. The only way I see this possible is through some type of API. The QTextEdit box does have a CTRL-C(copy) and CTRL-S (select all). I was thinking if I can send the two commands and then get the text from the clipboard. Please let me know if that route is possible or if there is some other way using some type of SendMessage api to retrive the value of the textbox. Thanks before hand.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
I'm afraid there won't be any sort of API - you'll need to rely on mouse/keyboard based interaction to extract the data you need from the C++ application. You could still automate this however.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
robertsmith
Registered Member
Posts
3
Karma
0
I was able to resolve this using SendKeys command, select of all text (CTRL-A) and then a copy (CTRL-C) to the clipboard. I then retrieved the text from the clipboard into my C# program. No need to use Qyoto. Here is the code in case anyone is interested: (The only caveat is that the application cannot be minimized, even then there might be a way to maximize the application before sending the commands) Note: If you have more than one QTextEdit box, you might need to send some tab commands using SendKeys.SendWait("{TAB}") before you send the CTRL commands.

Code: Select all
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

public void getAppData()
        {

            // Get AppName handle
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("AppName"); //search for process
            if (p.Length > 0) //check if window was found
            {
                SetForegroundWindow(p[0].MainWindowHandle); //bring window handle to foreground
            }
            else
            {
                MessageBox.Show("Error: Please make sure AppName is running");
            }

            // Wait 500 milliseconds
            System.Threading.Thread.Sleep(500);

            // Send CTRL-A (Select All) command to AppName to select all text
            SendKeys.SendWait("^(a)");

            // Send CTRL-C (Copy) command to AppName to send data to clipboard
            SendKeys.SendWait("^(c)");

            // Wait 3 seconds before checking clipboard (To give chance to data to move to clipboard)
            System.Threading.Thread.Sleep(3000);

            // Copy data from the clipboard
            string text;
            try
            {
                text = Clipboard.GetText().Trim();
            }
            catch
            {
                text = Clipboard.GetText().Trim(); // sometimes you have to try again...
            }

            // Update Text Data
            if (text != "")
            {
                txtData.Text = text;
                toolStripStatusLabel1.Text = "Data retrieved from AppName.";
            }
            else
            {
                toolStripStatusLabel1.Text = "No data retrieved from AppName.";
            }

        }
vaynenick
Registered Member
Posts
1
Karma
0
Check this one..a detailed tutorial on......C# Textbox

Vayne


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]