• 0

[C#] Reading/Writing textbox on other program


Question

Good Morning All -

I consider myself an Intermediate Level programmer. I do web design in my current position, but I also have experience with other languages. I am currently working on my 4 year degree in Computer Science. I understand different topics and terms, but this is confusing me.

The Problem: We have a help desk ticket system called ARS Remedy 4. When a ticket is being filled out we have a list of questions that are different for each problem. We need to answer these questions that show up in a textfield. We are still a ways off of implementing Remedy 7 (which contains Magic [the program that does what I am trying to create]).

I am trying to find a find a way to interact with Remedy. I want to pull the text from the textbox and then parse it and popup a window asking the questions in a much easier to read and fill out form. Then once we are done filling it out I want to have it put the information back into Remedy.

I have looked into the Windows API and I can use Interop (is that the right term?) in C#, but I am still a little lost as to which API functions to use. I looked into FindWindow, but I need to see if I can find the actual component in the application, and I don't know where to find this. I have tried to find managed code in C# that can get me the components, but what I did find wouldn't return an instantiated object (it may not really exist or access denied?). I am sure that there may be something out there on Google, but all my searches have not yielded the right results. My hunch is that I am not searching correctly.

Some may say that there might be an API for Remedy that I can use. I cannot find one, or I don't have access to BMC's site to find more information. Besides, Remedy 4 is old and not really supported anymore.

Thank you for any help that you can provide!

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

In Windows, every object on a form is classed as a window, and as such, can send and receive messages.

You could probably achieve the above using a process similar to this pseudo-code:

var windowHandle = FindWindow("WindowClass", "Window Caption")
var textboxHandle = FindWindowEx(windowHandle, 0, "TextBoxClass", null)

string message = "Hello World!"
byte[] buffer = ToBytes(message)
SendMessage(textBoxHandle, WM_SETTEXT, 0, buffer)

So, if I was going to accomplish this in .NET:

public class ExternalWriter 
{
	[DllImport("user32.dll", SetLastError = true)]
	private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

	[DllImport("user32.dll", SetLastError = true)]
	private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string lpszWindow);

	[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
	private static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, string lParam);

	private const int WM_SETTEXT = 12;

	public void DoExternalWrite(string text) 
	{
		IntPtr parent = FindWindow("<window class name>", "<window title">);
		IntPtr child = GetchildHandle(parent, "<class name>");

		SendMessage(child, WM_SETTEXT, IntPtr.Zero, text);
	}

	private IntPtr GetChildHandle(IntPtr parent, string className) 
	{
		/* Here you need to perform some sort of function to obtain the child window handle, perhaps recursively
		 */

		IntPtr child = FindWindowEx(parent, IntPtr.Zero, className, null);
		child = FindWnidowEx(parent, child, className, null);

		/* You can use a tool like Spy++ to discover the hierachy on the Remedy 7 form, to find how many levels you need to search
		 * to get to the textbox you want */

		return child;
	}
}

I haven't tested this so you will probably have to put something together that is similar, make sure you use Spy++ on the BMC Remedy window to find the window class of the textbox control. You'll notice in my .NET implementation I haven't bothered converting the string to a byte array, because the user32.dll function is overloaded to provide a string lParam version. You could of course use Convert.ToByteArray(<string>) to do that for you.

Hope that helps, happy Coding!

Link to comment
Share on other sites

  • 0

I've read this interesting post .

I try to read a labelbox from another application.

I've found it with spy++ but the handle of this label change all the time is it possible to trace it from the start of the mainform ?

Thanks

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.