Solved

MonitorFromWindow AV

Posted on 2010-09-10
5
769 Views
Last Modified: 2013-11-05
When a user resizes my app I trap the WMSize to make sure the app doesn't grow wider than the workspace.  I need to keep a maximized app from going over the Vista sidebar if it is "stay on top".

We have a customer who is using a laptop.  At work this laptop is used with a docking station.

When I call Screen.MonitorFromWindow it sometimes throws an AV on his laptop.  Can anyone tell me why and how to fix?  I am assuming it has to do with the docking station since have we no other customer reporting this problem.

Thanks.
0
Comment
Question by:alexiat
  • 3
5 Comments
 
LVL 4

Expert Comment

by:cebasso
ID: 33657278
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33660483
does the problem occurs when the user is using the docking station, or when he is out of it ?
Has he changed that state since the application started (docked in or out) ?
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33660584
The code source for the root MonitorFromWindow function is quite complex : it can, or cannot, rely on Windows API, depending on their availability, or on a default stub (Delphi tries his own algo to emulate windows API) if it is not available.
Let's suppose that this AV comes from the stub function : it only support detection of the primary monitor. So if after MonitorFromWindow you call FindMonitor with that result and the monitor is not the primary, it can return a NIL monitor.
If you use that monitor returned from TScreen.MonitorFromWindow without checking for NIL, you have an AV.
function TScreen.MonitorFromWindow(const Handle: THandle;
  MonitorDefault: TMonitorDefaultTo): TMonitor;
begin
  Result := FindMonitor(MultiMon.MonitorFromWindow(Handle,
    MonitorDefaultFlags[MonitorDefault]));
end;

function TScreen.FindMonitor(Handle: HMONITOR): TMonitor;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to MonitorCount - 1 do
    if Monitors[I].Handle = Handle then
    begin
      Result := Monitors[I];
      break;
    end;
end;

// this is the stub. It is called first, as being assigned to MultiMon.MonitorFromWindow variable
// It's first task is to see if the API is available, and if so, 
// replace func global variable with the API func address, and use it
// if not, it is emulated with xMonitorFromRect
function xMonitorFromWindow(hWnd: HWND; dwFlags: DWORD): HMONITOR; stdcall;
var
  wp: TWindowPlacement;
begin
  if not InitApis[mmMonitorFromWindow] then
  begin
    @MonitorFromWindow := InitAnApi(mmMonitorFromWindow, @MonitorFromWindow, 'MonitorFromWindow');
    Result := MonitorFromWindow(hWnd, dwFlags);
    Exit;
  end;

  if Boolean(dwFlags and (MONITOR_DEFAULTTOPRIMARY or MONITOR_DEFAULTTONEAREST)) then
    Result := xPRIMARY_MONITOR
  else
  begin
    if IsIconic(hWnd) then
      GetWindowPlacement(hWnd, @wp)
    else
      GetWindowRect(hWnd, wp.rcNormalPosition);
    Result := xMonitorFromRect(@wp.rcNormalPosition, dwFlags);
  end
end;

// and this one COULD return an invalid handle (0)
function xMonitorFromRect(lprcScreenCoords: PRect; dwFlags: DWORD): HMONITOR; stdcall;
begin
  if not InitApis[mmMonitorFromRect] then
  begin
    @MonitorFromRect := InitAnApi(mmMonitorFromRect, @MonitorFromRect, 'MonitorFromRect');
    Result := MonitorFromRect(lprcScreenCoords, dwFlags);
    Exit;
  end;

  Result := 0;
  if Boolean(dwFlags and (MONITOR_DEFAULTTOPRIMARY or MONITOR_DEFAULTTONEAREST)) or
    (lprcScreenCoords.Right > 0) and
    (lprcScreenCoords.Bottom > 0) and
    (lprcScreenCoords.Left < GetSystemMetrics(SM_CXSCREEN)) and
    (lprcScreenCoords.Top < GetSystemMetrics(SM_CYSCREEN)) then
      Result := xPRIMARY_MONITOR;
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33660597
In short, you'd better gather a lot more intel about your user problem : OS, context of the error, and also context of the times there is no error etc...
Try to locate the exact place in Delphi code where the AV occurs. For this, you can use the "Find Execution Error" delphi function with the hexadecimal error address when the AV occurs in your user laptop.
0
 

Author Closing Comment

by:alexiat
ID: 33666463
Thanks.  Now I have an understanding of the problem and I can work with the user to find out exactly when and where the problem is happening.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 163
Tembedded WB animatid gifs not animated on some pcs 2 79
Dynamically Created Query 3 54
How to build JSON File in Delphi 6 3 32
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now