Article View: borland.public.cppbuilder.nativeapi
Article #10713Re: FindFirstChangeNotification - which File(s) was/were changed
From: Dave
Date: Sat, 27 Mar 2004 08:05
Date: Sat, 27 Mar 2004 08:05
100 lines
4873 bytes
4873 bytes
On Fri, 26 Mar 2004 22:52:41 -0800, Remy Lebeau (TeamB) wrote:
>
> "Dave" <dave_xse@-spam-hotmail.com> wrote in message
> news:1cj24pzpnk6kq.7n0lajrj0cil$.dlg@40tude.net...
>
>> I have done it but it just won't work yet...
>
> It works fine when I use it in my own projects.
>
>> The event is newer trapped by the Form's message
>> handler somehow...
>
> That is because you did not set it up correctly.
>
>> hNotify = SHChangeNotifyRegister(Form1->Handle,
>
> You are calling your register code from within TForm1's constructor. The
> global Form1 pointer has not been assigned yet at that time. You are lucky
> your program did not crash from referencing an invalid pointer. To use the
> Handle from the constructor, you need to use the implicit 'this' pointer
> instead, like you did when calling SHBrowseForFolder().
>
>> WM_USER, cItems, Mine);
>
> The last parameter of SHChangeNotifyRegister() expects a pointer, but you
> are not passing a pointer at all. You need to change your declaration to
> use NOTIFYREGISTER* instead, and then pass your structure instance using the
> '&' operator:
>
> Also, the functions you are importing from Shell32 all use the __stdcall
> calling convention, but your declarations are not specifying any calling
> convention at all. Unless your environment's default convention is
> __stdcall (__cdecl is usually the default), then you will not be able to
> call the functions correctly, and more importantly you will corrupt the
> stack and thus your program will have undefined behavior from that point on.
>
> Use this code instead (I suggest you use typedefs for make your code
> clearer):
>
> typedef HANDLE (__stdcall *LPSHCNR)(HWND, DWORD, ULONG, UINT, DWORD,
> NOTIFYREGISTER*);
> typedef BOOL (__stdcall *LPSHCND)(HWND);
>
> LPSHCNR lpSHChangeNotifyRegister;
> LPSHCND lpSHChangeNotifyDeRegister;
>
> lpSHChangeNotifyRegister = (LPSHCNR) GetProcAddress(hShell,
> MAKEINTRESOURCE(2));
> lpSHChangeNotifyDeRegister = (LPSHCND) GetProcAddress(hShell,
> MAKEINTRESOURCE(4));
>
> Now, with that said, if you run your code under WinNT/2K/XP, then you need
> to use SHChangeNotification_Lock() and SHChangeNotification_Unlock() as well
> in order to get at the event information. Under Win9x/Me, the event info is
> contained inside the wParam and lParam values directly. But under
> WinNT/2K/XP, the OS creates a block of shared memory and then passes the
> memory address to your code rather than the original data. So you have to
> lock the memory and then copy the desired data into your local storage, then
> you can process the same values that Win9x/Me normally give directly.
>
>
> Gambit
Thanks for you prompt feedback,
I did a bit of mess with the declarations, thanks for
fixing it...
Now the event gets trapped however i can't Lock it (using XP).
hLock is always NULL when i call lpSHChangeNotification_Lock.
I declared lpSHChangeNotification_Lock as follows
typedef HANDLE (__stdcall *LPSHCNL)(HWND, DWORD, LPCITEMIDLIST**, LPLONG);
LPSHCNR lpSHChangeNotifyRegister;
and here's the code involved...
void __fastcall TForm1::DirectoryHasChanged(TMessage &Msg)
{
Edit1->Text = "Some stuff happened";
LPCITEMIDLIST *pidls;
LONG wEventId;
if (bRunningWindowsNT) {
HANDLE hLock;
hLock = lpSHChangeNotification_Lock((HANDLE)Msg.WParam,
(DWORD)Msg.LParam, &pidls, &wEventId);
if (hLock){
int i = 0;
// ProcessEvent(wEventId, pidls[0], pidls[1]);
lpSHChangeNotification_Unlock(hLock);
}
}
else {
pidls = (LPCITEMIDLIST*)wParam;
wEventId = (LONG)lParam;
ProcessEvent(wEventId, pidls[0], pidls[1]);
}
}
//-------------------------------------------------------------------------
Technical Details
Message-ID:
<171wt4iapf939.eeolezisshus$.dlg@40tude.net>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<gtsccansk388.1njug0ildrjsr$.dlg@40tude.net> <40648199@newsgroups.borland.com> <1cj24pzpnk6kq.7n0lajrj0cil$.dlg@40tude.net> <40652272$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,255fcfa7b17a92b2 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-03-27 00:09:12 PST From: Dave <dave_xse@-spam-hotmail.com> Subject: Re: FindFirstChangeNotification - which File(s) was/were changed Newsgroups: borland.public.cppbuilder.nativeapi MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit References: <gtsccansk388.1njug0ildrjsr$.dlg@40tude.net> <40648199@newsgroups.borland.com> <1cj24pzpnk6kq.7n0lajrj0cil$.dlg@40tude.net> <40652272$1@newsgroups.borland.com> Date: Sat, 27 Mar 2004 16:05:05 +0800 Message-ID: <171wt4iapf939.eeolezisshus$.dlg@40tude.net> User-Agent: 40tude_Dialog/2.0.5.1 NNTP-Posting-Host: 202.72.161.99 X-Trace: newsgroups.borland.com 1080374708 202.72.161.99 (27 Mar 2004 00:05:08 -0800) Lines: 100 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail