0

Inside DLL function I need handle to app active window. The same GetActiveWindow() called from DLL function doesn't always return correct handle. Sometimes yes, sometimes no - handle = 0. I can not find the rules. How can I get it in the right way ?

Edit added code sample

Application:

...
function DllFunction: Boolean; stdcall; external 'MyDLL.dll';
...
procedure Form1.Button1Click(Sender: TObject);
begin
  DllFunction();
end;
...

Library MyDLL:

...
function DllFunction: Boolean; stdcall;
var
  hActiveWindow: HWND;
...
begin
...
  // I need the window handle of app Form1 
  hActiveWindow := GetActiveWindow;
...
end;

exports
  DllFunction;

begin
end.
Branko
  • 1,384
  • 1
  • 16
  • 35
  • `GetActiveWindow` is known to work. You are going to need to specify exactly what your goal is. – David Heffernan Jun 10 '13 at 16:59
  • Have you consulted the [MSDN reference to GetActiveWindow](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646292(v=vs.85).aspx)? It states that it returns `NULL` if the active window does not belong to the calling thread. – Günther the Beautiful Jun 10 '13 at 17:05
  • @GünthertheBeautiful That's not quite it. It returns the *active window attached to the calling thread's message queue*. Different threads can have different active windows. But it's possible that a thread does not have an active window. – David Heffernan Jun 10 '13 at 17:08
  • @GünthertheBeautiful - Does this mean that app and calling DLL not belong to same thread ? And why somitimes I get correct handle ? – Branko Jun 10 '13 at 17:11
  • @DavidHeffernan - I need positioning form created inside DLL function according to position of app active window. – Branko Jun 10 '13 at 17:14
  • @Branko "Does this mean that app and calling DLL are not belong to same thread?" That does not make sense. Applications and DLLs do not belong to threads. Processes own threads, and code is executed in threads. – David Heffernan Jun 10 '13 at 17:15
  • @DavidHeffernan - ok, DLL and app code is not executed in the same thread ? – Branko Jun 10 '13 at 17:20
  • Maybe yes, maybe no. It depends. I suspect you are getting sidetracked by threading. What thread is your code running in? Why are you keeping all the details from us? This game of 20 questions isn't very effective. – David Heffernan Jun 10 '13 at 17:25
  • The calling convention of the DLL function doesn't match the declaration in the program. Always copy and paste real code to avoid distracting typos. You could avoid all this active-window detection if you simply made the window handle be a parameter to the function — let the application explicitly *tell* the DLL which window handle to use instead of expecting the DLL to guess. – Rob Kennedy Jun 10 '13 at 17:52
  • No idea why NULL is returned, but I've also no idea why you don't pass the necessary info to the DLL in parameters. – David Heffernan Jun 10 '13 at 17:53
  • @RobKennedy "The calling convention of the DLL function doesn't match the declaration in the program" - my mistake, code in question is corrected. – Branko Jun 10 '13 at 18:01
  • So, why don't you simply pass `Handle` to the DLL? – David Heffernan Jun 10 '13 at 18:07
  • @RobKennedy and David - thanks for the help. If there is no other solutions I will have to pass window handle into dll. – Branko Jun 10 '13 at 18:10
  • That is, by a distance, the very best possible solution – David Heffernan Jun 10 '13 at 18:12
  • @DavidHeffernan - it seems that problem exists only on Win-7, I have not noticed it on Win-XP !? – Branko Jun 11 '13 at 08:15
  • Are you having problems with parameters? – David Heffernan Jun 11 '13 at 08:17
  • @DavidHeffernan - not with parameters, with GetActiveWindow() as I mentioned in question. I just wanted to inform that it looks like that the problem with GetActiveWindow() on XP not exists. – Branko Jun 11 '13 at 08:33

0 Answers0