I gave ReadDirectoryChangesW() a shot to no avil. Here is the source code for the method. Although it does detect changes, it will NOT detect if a file has been opened.
Code:
wstring s2ws(const string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
void CALLBACK ChDirCompletionRoutine( DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
Console::WriteLine("stylo");
}
void ripCord(){
//open the directory to watch....
std::wstring stemp = s2ws("c:\\");
LPCWSTR results = stemp.c_str();
HANDLE hDir = CreateFile(results,
FILE_LIST_DIRECTORY | GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE ,NULL, //security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,NULL);
if( hDir == INVALID_HANDLE_VALUE )
{
DWORD dwError = GetLastError();
Console::WriteLine("problem");
}
OVERLAPPED overlapped;
//overlapped.Internal =
// overlapped.InternalHigh =
// overlapped.Offset =
overlapped.OffsetHigh = 0;
overlapped.hEvent = NULL;
DWORD byteRerurned = 0;
DWORD dwRecSize = sizeof(FILE_NOTIFY_INFORMATION) + MAX_PATH * 2;
DWORD dwCount = 32;
FILE_NOTIFY_INFORMATION* pNI =
(FILE_NOTIFY_INFORMATION*)malloc(dwRecSize * dwCount);
while(true) {
//LPCOMPETION cb = ChDirCompletionRoutine;
if( ReadDirectoryChangesW( hDir,
pNI,//<--FILE_NOTIFY_INFORMATION records are put into this buffer
dwRecSize * dwCount,
FALSE,
FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_LAST_ACCESS
| FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_CREATION
| FILE_NOTIFY_CHANGE_FILE_NAME,
&byteRerurned,
&overlapped,
&ChDirCompletionRoutine) )
{
Console::WriteLine("Do something");
}
Sleep(3000);
Console::WriteLine("do nothing");
}
}