确定SendMessageTimeout失败原因

时间:2014-06-05 17:02:23

标签: delphi sendmessage getlasterror

如果SendMessageTimeout调用失败(即返回0),但后续的GetLastError返回0(成功),有没有办法确定调用失败的原因?

我正在使用的代码(下面)适用于我们的几十个用户,但对于两个用户,SendMessageTimeout失败,如上面第一句中所述。

function TQBInfo.GetFormCellText(bTestForCaret: boolean = True): string;
const
  BUFSIZE = 4096;
  TIMEOUT_MSECS = 4000;
var
  Wnd: HWnd;
  dwResult: DWORD;
  buf: array of char;
  sLastErrorText: string;
begin
  Result := '';

  Wnd := GetEditControlWindow;          //Custom function to find the target window
  if Wnd = 0 then
    exit;

  SetLastError(0);
  FLastError_GetFormCellText := 0;      //FLastError_GetFormCellText is an object field
  SetLength(buf, BUFSIZE + 1);
  if SendMessageTimeout(Wnd, WM_GETTEXT, BUFSIZE + 1, LongInt(@buf[0]),
      SMTO_ABORTIFHUNG or SMTO_BLOCK,
      TIMEOUT_MSECS,
      dwResult) = 0 then begin                 //0 = Failed or timed out
    FLastError_GetFormCellText := GetLastError;
    //...at this point, FLastError_GetFormCellText is always 0 in the failure case I'm seeing
    if FLastError_GetFormCellText = ERROR_TIMEOUT then
      sLastErrorText := 'ERROR_TIMEOUT'
    else
      sLastErrorText := SysErrorMessage(FLastError_GetFormCellText);
...

0 个答案:

没有答案