Search code examples
delphimessagedelphi-2006

Getting WPARAM in TWndMethod to return 4 bytes


I'm using AllocateHWnd in a class I'm writing to receive system messages with a TWndMethod and the messages I'm receiving need to handle a 4-byte WPARAM, which specifically references a pointer. But I'm only getting 2 bytes in return. How do I set up things so I can correctly receive these messages within the class?

Edit: Specific code. I'm setting a message event up using SHChangeNotifyRegister, based on a Microsoft sample I downloaded. The proc works enough to pull back events (in lEvent) that I can buy off on, but the code Microsoft used defines WParam to be Thandle and LParam to be DWord. The specific problem I have is that when the function IsItemNotificationEvent is true, SHGetPathFromIDList is AVing or pulling back garbage. I kept looking this over and am not really seeing a problem other than what the docs I have indicate in that WParam is a Word (probably old) and that GetLastError at the point I put in the code returns "The handle is invalid".

function IsItemNotificationevent(lEvent: Longint): boolean;
  var
    flagval: Longint;
  begin
    flagval := (lEvent and (SCHNE_UPDATEIMAGE or SHCNE_ASSOCCHANGED
            or SHCNE_EXTENDED_EVENT or SHCNE_FREESPACE
            or SHCNE_DRIVEADDGUI or SHCNE_SERVERDISCONNECT));
    Result := (flagval > 0);
  end;

procedure TShellNotifyHandler.WindowProc(Var msg: TMessage);
  var
    hNotifyLock: THandle;
    lEvent: Longint;
    pgpidl: PitemIDList;
    psi1: array[1..MAX_PATH] of Char;
  begin
    if Msg.Msg = FShellMsg then
      begin
        hNotifyLock := SHChangeNotification_Lock(THandle(Msg.WParam),DWord(Msg.LParam),
                 pgpidl, lEvent);
        writeln(SysErrorMessage(GetLastError));
        if (hNotifyLock > 0) then              
          begin
            if IsItemNotificationEvent(lEvent) then 
  // this limits events for this to what Microsoft defined in their example
               begin
                 if (pgpidl <> nil) then
                   SHGetPathFromIDList(pgpidl, @psi1);
                 Writeln('Path #1: ', String(psi1));
               end;
            SHChangeNotification_Unlock(hNotifyLock);
          end;
        if Assigned(FOnShellNotify) then
          FOnShellNotify(Self, LEvent);
      end
   else
      FWndProc(Msg);
  end;

Solution

  • Okay, I got this answered. A number of problems all over the board, actually:

    1) I had things wrong when it comes to IsItemNotificationEvent. To have valid PIDLs, I needed to make sure that the event WASN'T one of those, because no PIDL is valid to process against those.

    if IsItemNotificationEvent(lEvent) then 
    

    2) "out" was necessary in the definition to SHChangeNotification_Lock and not "var" or a simple pointer reference. I don't have anything that indicates what "out" does specifically, so if anyone can help, please do. The fixed definition is below.

    function SHChangeNotification_Lock(hChangeNotification: THandle; dwProcessID: DWord;
         out pppidl: PSHNotifyStruct; out plEvent: Longint): THandle; stdcall;
    

    3) In my documentation (including the source samples), it indicates that multiple pidls are possible for some event types. Which makes the suggested correction invalid in the QC report. The problem with using the original definition is probably as suggested. It's not quite right. Reference the definition above, and you'll see a different type. That definition is below. No events have more than two parms, so it would suffice.

    TSHNotifyStruct = packed record
      dw1: PItemIDList;
      dw2: PItemIDList;
    end;
    PSHNotifyStruct = ^TSHNotifyStruct;
    

    Got it working as I expect it to now. I just need to find a valid list of two parm events and code in to make it a little cleaner (i.e. not reference the second pitemid if known to be invalid). Some samples of output from my test program are below to illustrate:

     Event received: $00001000 Parm 1: (C:) Local Disk  // update directory
     Event received: $00000008 Parm 1: ChangeNotifyWatcher // make directory
     Event received: $00000002 Parm 1: ChangeNotifyWatcher // create file
     Event received: $00000010 Parm 1: ChangeNotifyWatcher Parm 2: RECYCLER // remove directory
    

    Thanks all for your help!