简体   繁体   中英

Read a non C# Apps textbox using windows API from a C# Windows forms App

We have an old Windows 32 bit app written in C++ which does some stuff and displays the results in what resembles a textbox.

I have been ask to write an application in C# that reads the data out of the old app and then further process the data.

The issue is how do I read the textbox in the old application?

Someone told me I can get the “handle “of the application using windows API and step through the controls and then read each ones data? Is this true and if so how would I do it from C#?

This is to be a.Net 4 Windows forms Application.

Many thanks

You're probably going to have to use some Interop calls to accomplish this, specifically using a combination of FindWindow / FindWindowEx and SendMessage & WM_GETTEXT / WM_GETTEXTLENGTH .

Here is an article on the subject (in c++, however the same concepts will just need to be ported to use P/Invoke ), it's a bit dated but I believe is should be very relevant to your situation.

You can use Spy++ application (comes with VisualStudio) to inspect your native application and find class name of control you are looking for.

Having that, you can get your native application's main window handle, which is easy if you are responsible for launching that application from your c# app:

...
var proc = Process.Start();
var mainWndHandle = proc.MainWindowHandle;

Otherwise, you will have to find other means of finding that window, fe. you can use function that I've described below to look for that window on your desktop (see msdn page for more info)

If you have all that, you can then get handle to the textbox control, using FindWindowEx function (as well as you can use it to find main window, by passing NULL as a hwndParent).

And when you have handle to this textbox, you can read it contents, by sending message of WM_GETTEXT .

If you haven't been PInvoke'ing much, you can check http://pinvoke.net/ for reference on how to call WINAPI function from your c# program.

Do you really need the UI of the C++ application? Because it will be very unconfortable to have 2 separate UIs with different message pumps. You'll need to syncronize this mess on the update of the value.

Why don't you properly strip the logic code from the C++ application and expose it in a Interop CLR project (C++/CLI)? If you don't need to add more features to the C++ app, it seems very straightforward to me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM