4

I need some help about the win32 api and especially WindowsFromDc.

I have a application which hook a another application. This two application communicate by a NamedPipe. In the second application, I have hook the DrawTextExW function and I get a HDC from this function.

But when I do a WindowsFromDC with the DC returned by the DrawTextEx function, i got a null return.

So, I have some question about that : -Is it possible a HDC don't have a HDWN with ? -How I can get the HWND of the window which call DrawTextEx ? There are other way do to that ?

Thank you.

Ps : Sorry for my bad english...

  • 1
    How do you hook? Maybe the DC is not valid in the second process? Also note that CreateDC and CreateCompatibleDC create DCs that might be related to a printer or might be a memory DC. These do not have a window associated with them. Are you sure that the hooked application is drawing to a windows's DC? – Werner Henze Apr 23 '13 at 11:50
  • I hook with the Microsoft Detour library and injecting the DLL in the second application. I think the DC is correct but even if I do a WindowFromDC in the DLL injected, i got a NULL return. How can I be sure if the application drawing in a window's DC ? – Florian Cabirol Apr 23 '13 at 17:30

1 Answers1

3

Device context handles are not valid when passed cross-process. So what you are attempting to do is not possible.

As for your other questions:

Is it possible to have an HDC that is not associated with an HWND?

Yes that is perfectly possible. Plenty of device contexts are not associated with windows.

How I can get the HWND of the window which call DrawTextEx?

Windows do not call functions, code calls functions. So, the question does not really mean anything.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Yes but with the Detour Library, I hook the DrawTextEx Function with a DLL injected in the second application. But some times, the second application open a another window, may be 2. So I have the result of the DrawTextEx of the second window too. How can I do to know on which windows DrawTextEx draw ? – Florian Cabirol Apr 23 '13 at 17:36
  • That's not how it came across in the question. In the question, you talked about two processes communicating using IPC. Going to be hard to infer from the DC where the drawing is going to end up. I suspect your solution to the problem is not going to work. But unfortunately you asked a question about your solution rather than asking about the problem. This is what is known as an XY question. – David Heffernan Apr 23 '13 at 17:48
  • 3
    Ok I see. So I have found a solution what I think good. I Hook BeginPaint and EndPaint too. I known that the process of calling in application code is : BeginPaint -> DrawTextEx -> EndPaint. So I save the HWND given by BeginPaint and I associate it with the DrawTextEx next call. When EndPaint is called, I clear the HWND saved. I think is the good solution. Thank you very much for you help and your help for my thinking process. I will try to do better next times. – Florian Cabirol Apr 23 '13 at 20:08
  • @DavidHeffernan function can call code :-), I find this because I have similar question when hooking the DrawTextA function. Unfortunately I got null too. – user1633272 Jul 07 '17 at 09:43
  • @FlorianCabirol that's sounds doable, does it reliable? – user1633272 Jul 07 '17 at 09:46