From: Rod on
Hello.

How do I implement the function "ReadDirectoryChangesW"
asynchronously?

(Reference:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/readdirectorychangesw.asp
)

MSDN says there are three ways to do it:
(1) GetOverlappedResult
(2) GetQueuedCompletionStatus using
"CreateIoCompletionPort".
(3) Using a completion routine.

I don't know how to implement any of them.

Below is what I'm doing at the moment --- which isn't much.

char *chFile1 = "C:\\TMP1";
DWORD dwBytesReturned = 0;
FILE_NOTIFY_INFORMATION fni[1024];
HANDLE hDir;

hDir = CreateFile (chFile1, // pointer to the file name
FILE_LIST_DIRECTORY, // access mode
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS, // file attributes
NULL
);

if (hDir == INVALID_HANDLE_VALUE)
{
.... handle error and exit.
}


while(1)
{ //Non zero if function succeeds.
Sleep(2000);
if(!ReadDirectoryChangesW( hDir,&fni[0], (sizeof(FILE_NOTIFY_INFORMATION) * 1024), FALSE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE, &dwBytesReturned, NULL, NULL ))
{
.... handle error and exit.
break;
}
else
{
.... handle returned data.
}
}//End of while.

=================end code snippet==========================

If anyone can get me over the line I'd be most grateful.

Regards,

Rod.


__ Rod.
From: Arkady Frenkel on
Do it on different thread and use sync object ( Critical section ) to
read/write results
Arkady

"Rod" <Siegen052006(a)end.of.message> wrote in message
news:ub0e72hhq8njfhq964kno69on5evdvd5ig(a)4ax.com...
> Hello.
>
> How do I implement the function "ReadDirectoryChangesW"
> asynchronously?
>
> (Reference:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/readdirectorychangesw.asp
> )
>
> MSDN says there are three ways to do it:
> (1) GetOverlappedResult
> (2) GetQueuedCompletionStatus using
> "CreateIoCompletionPort".
> (3) Using a completion routine.
>
> I don't know how to implement any of them.
>
> Below is what I'm doing at the moment --- which isn't much.
>
> char *chFile1 = "C:\\TMP1";
> DWORD dwBytesReturned = 0;
> FILE_NOTIFY_INFORMATION fni[1024];
> HANDLE hDir;
>
> hDir = CreateFile (chFile1, // pointer to the file name
> FILE_LIST_DIRECTORY, // access mode
> FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
> NULL, // security descriptor
> OPEN_EXISTING, // how to create
> FILE_FLAG_BACKUP_SEMANTICS, // file attributes
> NULL
> );
>
> if (hDir == INVALID_HANDLE_VALUE)
> {
> .... handle error and exit.
> }
>
>
> while(1)
> { //Non zero if function succeeds.
> Sleep(2000);
> if(!ReadDirectoryChangesW( hDir,&fni[0],
> (sizeof(FILE_NOTIFY_INFORMATION) * 1024),
> FALSE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
> &dwBytesReturned, NULL, NULL ))
> {
> .... handle error and exit.
> break;
> }
> else
> {
> .... handle returned data.
> }
> }//End of while.
>
> =================end code snippet==========================
>
> If anyone can get me over the line I'd be most grateful.
>
> Regards,
>
> Rod.
>
>
> __ Rod.


From: Rod on
On Sat, 27 May 2006 08:19:10 +0200 "Arkady Frenkel"
<arkadyf(a)hotmailxdotx.com> wrote:

Hello arkady.

I know to do it in a separate thread. What I don't
understand is how to use either an "overlapped" result,
"completion port" or "completion routine" in the context of
this API call. I know what they mean I don't know how to
implement them.

Also, sample code, some of which I can get to, at least,
run, is sprinkled with TCHAR. I've never had a need for this
type before so I don't understand its relevance to this code
and I am uncertain of how to convert a TCHAR back into a
"normal" data type.

A few lines of explained example code should be enough to
get me on my way again, if you or someone else could provide
some.

THanks for responding.

Regards,

Rod.

:> Do it on different thread and use sync object ( Critical section ) to
:> read/write results
:> Arkady
:>
:> "Rod" <Siegen052006(a)end.of.message> wrote in message
:> news:ub0e72hhq8njfhq964kno69on5evdvd5ig(a)4ax.com...
:> > Hello.
:> >
:> > How do I implement the function "ReadDirectoryChangesW"
:> > asynchronously?
:> >
:> > (Reference:
:> > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/readdirectorychangesw.asp
:> > )
:> >
:> > MSDN says there are three ways to do it:
:> > (1) GetOverlappedResult
:> > (2) GetQueuedCompletionStatus using
:> > "CreateIoCompletionPort".
:> > (3) Using a completion routine.
:> >
:> > I don't know how to implement any of them.
:> >
:> > Below is what I'm doing at the moment --- which isn't much.
:> >
:> > char *chFile1 = "C:\\TMP1";
:> > DWORD dwBytesReturned = 0;
:> > FILE_NOTIFY_INFORMATION fni[1024];
:> > HANDLE hDir;
:> >
:> > hDir = CreateFile (chFile1, // pointer to the file name
:> > FILE_LIST_DIRECTORY, // access mode
:> > FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
:> > NULL, // security descriptor
:> > OPEN_EXISTING, // how to create
:> > FILE_FLAG_BACKUP_SEMANTICS, // file attributes
:> > NULL
:> > );
:> >
:> > if (hDir == INVALID_HANDLE_VALUE)
:> > {
:> > .... handle error and exit.
:> > }
:> >
:> >
:> > while(1)
:> > { //Non zero if function succeeds.
:> > Sleep(2000);
:> > if(!ReadDirectoryChangesW( hDir,&fni[0],
:> > (sizeof(FILE_NOTIFY_INFORMATION) * 1024),
:> > FALSE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
:> > &dwBytesReturned, NULL, NULL ))
:> > {
:> > .... handle error and exit.
:> > break;
:> > }
:> > else
:> > {
:> > .... handle returned data.
:> > }
:> > }//End of while.
:> >
:> > =================end code snippet==========================
:> >
:> > If anyone can get me over the line I'd be most grateful.
:> >
:> > Regards,
:> >
:> > Rod.
:> >
:> >
:> > __ Rod.
:>

__ Rod.
From: Arkady Frenkel on
Hi, Rod!
But you haven't IO operations in the contect you shown so IMHO you don't
need either overlapped nor IOCP there
Overlapped alow you to ask read/write to/from different buffers and IOCP
used be server to serve multiple clients in scalable way. Do you have those
demands in your code?
Arkady

"Rod" <Siegen032005(a)end.of.message> wrote in message
news:4prf72pkemuocd5bucua493207fm12d9qe(a)4ax.com...
> On Sat, 27 May 2006 08:19:10 +0200 "Arkady Frenkel"
> <arkadyf(a)hotmailxdotx.com> wrote:
>
> Hello arkady.
>
> I know to do it in a separate thread. What I don't
> understand is how to use either an "overlapped" result,
> "completion port" or "completion routine" in the context of
> this API call. I know what they mean I don't know how to
> implement them.
>
> Also, sample code, some of which I can get to, at least,
> run, is sprinkled with TCHAR. I've never had a need for this
> type before so I don't understand its relevance to this code
> and I am uncertain of how to convert a TCHAR back into a
> "normal" data type.
>
> A few lines of explained example code should be enough to
> get me on my way again, if you or someone else could provide
> some.
>
> THanks for responding.
>
> Regards,
>
> Rod.
>
> :> Do it on different thread and use sync object ( Critical section ) to
> :> read/write results
> :> Arkady
> :>
> :> "Rod" <Siegen052006(a)end.of.message> wrote in message
> :> news:ub0e72hhq8njfhq964kno69on5evdvd5ig(a)4ax.com...
> :> > Hello.
> :> >
> :> > How do I implement the function "ReadDirectoryChangesW"
> :> > asynchronously?
> :> >
> :> > (Reference:
> :> >
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/readdirectorychangesw.asp
> :> > )
> :> >
> :> > MSDN says there are three ways to do it:
> :> > (1) GetOverlappedResult
> :> > (2) GetQueuedCompletionStatus using
> :> > "CreateIoCompletionPort".
> :> > (3) Using a completion routine.
> :> >
> :> > I don't know how to implement any of them.
> :> >
> :> > Below is what I'm doing at the moment --- which isn't much.
> :> >
> :> > char *chFile1 = "C:\\TMP1";
> :> > DWORD dwBytesReturned = 0;
> :> > FILE_NOTIFY_INFORMATION fni[1024];
> :> > HANDLE hDir;
> :> >
> :> > hDir = CreateFile (chFile1, // pointer to the file name
> :> > FILE_LIST_DIRECTORY, // access mode
> :> > FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
> :> > NULL, // security
> descriptor
> :> > OPEN_EXISTING, // how to create
> :> > FILE_FLAG_BACKUP_SEMANTICS, // file attributes
> :> > NULL
> :> > );
> :> >
> :> > if (hDir == INVALID_HANDLE_VALUE)
> :> > {
> :> > .... handle error and exit.
> :> > }
> :> >
> :> >
> :> > while(1)
> :> > { //Non zero if function succeeds.
> :> > Sleep(2000);
> :> > if(!ReadDirectoryChangesW( hDir,&fni[0],
> :> > (sizeof(FILE_NOTIFY_INFORMATION) * 1024),
> :> > FALSE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
> :> > &dwBytesReturned, NULL, NULL ))
> :> > {
> :> > .... handle error and exit.
> :> > break;
> :> > }
> :> > else
> :> > {
> :> > .... handle returned data.
> :> > }
> :> > }//End of while.
> :> >
> :> > =================end code snippet==========================
> :> >
> :> > If anyone can get me over the line I'd be most grateful.
> :> >
> :> > Regards,
> :> >
> :> > Rod.
> :> >
> :> >
> :> > __ Rod.
> :>
>
> __ Rod.


From: Tim Roberts on
Rod <Siegen032005(a)end.of.message> wrote:
>
>I know to do it in a separate thread. What I don't
>understand is how to use either an "overlapped" result,

If you use a separate thread, it is not NECESSARY to use a second thread.
Just let the call block. That's the beauty of using another thread.

On the other hand, using an overlapped result is not hard. Create an
OVERLAPPED structure on the stack. Create an event using CreateEvent, and
shove its handle into the hEvent. Pass the structure to
ReadDirectoryChangesW. The function will return immediately.

Now you can go do something else, and when you run out of things to do,
call WaitForSingleEvent on the event in the overlap structure. When it
finally finishes, use GetOverlappedResult to get the results.

Be sure to open the directory with FILE_FLAG_OVERLAPPED.
--
- Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.