Poll visible windows’ positions from C++ and send to a C# client

I’m creating a C# client which shows all Windows visible windows’ bounds at real time. (this is not the main purpose, but it’s a core feature).

At the beginning, I found DLL injection. So I considered a global DLL injection so that every process with an injected DLL sends its bounds to my client. But this was far too complicated.

I quickly turned to polling. It’s important to know that I have a game loop with interpolation in order to have a “real-time display of windows’ bounds”

(I subscribed to CompositionTarget.Rendering event, I’m using WPF)

First, I used (and I still use) EnumWindows in my game loop. This was laggy, and in order to filter all the windows, I was always gathering all the information of a window with P/Invoke calls although it was not always necessary.

Anyway. To optimize this, I put all this logic in an infinite loop in a Dispatcher Thread and for each enumerated window, I get its information one at a time, only if I need it, and in the order of first exclusion.

My want is to have the smoothest polling I can have, so I’d like to switch to C++ for this poll and filter stuff to make it faster.

Note: this duo of C++ and C# “apps” would never be running separately. The C++ side is only useful to the C# side. If C# side stops, the other needs to stop too.

Now that you see the big picture of what I already did, and what I’d like to achieve, here’s my question(s):

  • I’m considering using a C++ service, is it a good idea? If not, what would you advise me?
  • What method would you advise me to send a relatively continuous flow of bounds information to my C# client from C++?

I say send because I think of the “from C++ to C#” way, but maybe it’s better to think the other way. For example, C++ serves a file or something which is continously updated, and I would read it with C#…

The only thing is that all of this has to be fast and look smooth.