Watching a directory for changes

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

mitch1423
Posts: 47
Joined: Sun Sep 26, 2010 6:10 pm UTC

Watching a directory for changes

Postby mitch1423 » Thu Mar 07, 2013 3:56 pm UTC

So I'm trying to watch a directory with C++, and I'm referring to http://msdn.microsoft.com/en-us/library/aa365261(VS.85).aspx on how to do it.

Code: Select all

   DWORD dwWaitStatus;
   LPTSTR lpDir;
   HANDLE dwChangeHandles[2];
   TCHAR lpDrive[4];
   TCHAR lpFile[_MAX_FNAME];
   TCHAR lpExt[_MAX_EXT];

   _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);

   lpDrive[2] = (TCHAR)'\\';
   lpDrive[3] = (TCHAR)'\0';


That's the start of the watchdirectory function that they show, but I have no idea what the function "_tsplitpath_s" is doing and I can't seem to get my program to work without it.

Code: Select all

changeHandle = FindFirstChangeNotification((LPTSTR)"C:\\test\\",false,FILE_NOTIFY_CHANGE_FILE_NAME);


That's my code for creating the handle, but it just fails every time. Can anyone tell me what I've done wrong?
Last edited by phlip on Fri Mar 08, 2013 3:04 am UTC, edited 1 time in total.
Reason: Made the subject line less generic

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Can someone help me with this?

Postby Xenomortis » Thu Mar 07, 2013 4:09 pm UTC

It really helps if you tell people how your code is failing. I don't have any psychic debugging powers.
But I won't become psychic without trying, so here goes:

As far as I can tell, _tsplitpath_s splits a directory path into its components, i.e. lpDir, lpDrive, etc.
Your call to FindFirstChangeNotification could be failing because you're trying to pass through a LPTSTR rather than a TCHAR array as in the example code.
Image

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Can someone help me with this?

Postby phlip » Fri Mar 08, 2013 3:03 am UTC

Xenomortis wrote:Your call to FindFirstChangeNotification could be failing because you're trying to pass through a LPTSTR rather than a TCHAR array as in the example code.

These are the same (well, up to pointers vs arrays, anyway). LPTSTR is a typedef for TCHAR * (and TCHAR is a magic typedef that's either char or wchar_t depending on whether UNICODE is defined).

mitch: Are you compiling with UNICODE turned on? Given you have a cast to LPTSTR there in your call, I'm guessing you do (my guess is you tried it without the cast, got a compiler error 'cause it's expecting a Unicode string, and threw in a cast there 'cause it made the error go away even though it's completely wrong). Try making it a Unicode string, like L"C:\\test\\", or even better:

Code: Select all

changeHandle = FindFirstChangeNotification(TEXT("C:\\test\\"),false,FILE_NOTIFY_CHANGE_FILE_NAME);
(TEXT being a macro that turns a string literal into either a normal string literal or a Unicode string literal depending on whether UNICODE is defined).

Failing that, what's the error message? What's in GetLastError()? Like the docs say:
MSDN wrote:If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

mitch1423
Posts: 47
Joined: Sun Sep 26, 2010 6:10 pm UTC

Re: Watching a directory for changes

Postby mitch1423 » Fri Mar 08, 2013 5:17 am UTC

Sorry I didn't include much info Xeno, thanks for trying to help. The problem was with the LPTSTR cast; I needed to use a Unicode string like phlip said. Thanks guys.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests