DWORD WINAPI WatchChanges(LPVOID lpParameter);
typedef struct ThreadParameter
{
LPTSTR in_directory;//the path of monitored data file
FILE_NOTIFY_INFORMATION *in_out_notification;//storage the pointer infomationof monitor method
DWORD in_MemorySize;//storage the byte of returned information's memory
DWORD *in_out_BytesReturned;//storage the byte of return information of monitor method
DWORD *in_out_version;//version
FILE_NOTIFY_INFORMATION *temp_notification;
}ThreadParameter;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
wchar_t *p= L"D:\\temp";
LPCWSTR path = p;
char notify[1024];
memset(notify,'\0',1024);
FILE_NOTIFY_INFORMATION *Notification = (FILE_NOTIFY_INFORMATION *)notify;
FILE_NOTIFY_INFORMATION *TempNotification = NULL;
LPTSTR Directory = p;
DWORD BytesReturned = 0;
DWORD version = 0;
//the monitor of data file
//the define of threadparameter
ThreadParameter ParameterToThread={Directory,Notification,sizeof(notify),&BytesReturned,&version,TempNotification};
//create a thread to monitor the change of data file
HANDLE TrheadWatch=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WatchChanges,&ParameterToThread,0,NULL);
CloseHandle(TrheadWatch);
return a.exec();
}
/*
*function : monitor the change of file
*parameter : Thread's parameter
*return :
*/
DWORD WINAPI WatchChanges(LPVOID lpParameter)
{
ThreadParameter *parameter = (ThreadParameter*)lpParameter;
LPCTSTR WatchDirectory = parameter->in_directory;//
int edit_flag = 0;
//create a directory handle
HANDLE handle_directory = CreateFile(WatchDirectory,FILE_LIST_DIRECTORY,
FILE_SHARE_READ |
FILE_SHARE_WRITE |
FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,NULL);
if(handle_directory == INVALID_HANDLE_VALUE)
{
//DWORD ERROR_DIR=GetLastError();
}
//the state of the ReadDirectoryChangesW
BOOL watch_state;
while (TRUE)
{
watch_state = ReadDirectoryChangesW(handle_directory,
(LPVOID)parameter->in_out_notification,
parameter->in_MemorySize,
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
(LPDWORD)parameter->in_out_BytesReturned,
NULL,
NULL);
if (GetLastError() == ERROR_INVALID_FUNCTION)
{
cout<<"current system is unsupported!\n"<<endl;
}
else if(watch_state == 0)
{
cout<<"monitor is fail!\n"<<endl;
}
else if (GetLastError() == ERROR_NOTIFY_ENUM_DIR)
{
cout<<"out of memory!\n"<<endl;
}
else
{
string file_name;
DWORD length = WideCharToMultiByte(0,0,parameter->in_out_notification->FileName,-1,NULL,0,NULL,NULL);
PSTR ps = new CHAR[length];
if(length >= 0)
{
WideCharToMultiByte(0,0,parameter->in_out_notification->FileName,-1,ps,length,NULL,NULL);
file_name = string(ps);
delete[] ps;
}
if (parameter->in_out_notification->Action == FILE_ACTION_ADDED)
{
cout<<file_name<<"FILE_ACTION_ADDED"<<endl;
}
if (parameter->in_out_notification->Action == FILE_ACTION_REMOVED)
{
cout<<file_name<<"FILE_ACTION_REMOVED"<<endl;
}
if (parameter->in_out_notification->Action == FILE_ACTION_MODIFIED)
{
edit_flag++;
if(edit_flag == 1)
{
cout<<file_name<<"FILE_ACTION_MODIFIED"<<endl;
}
else if(edit_flag == 2)
{
edit_flag = 0;
(*(parameter->in_out_version))--;
}
else
return -1;//break;
}
if (parameter->in_out_notification->Action == FILE_ACTION_RENAMED_OLD_NAME)
{
cout<<file_name<<"FILE_ACTION_RENAMED_OLD_NAME"<<endl;
}
if (parameter->in_out_notification->Action == FILE_ACTION_RENAMED_NEW_NAME)
{
cout<<file_name<<"FILE_ACTION_RENAMED_NEW_NAME"<<endl;
}
//QDir dir_make;
//tr("%1/%2").arg();
//file_name += *parameter->in_directory;
//if(dir_make.exists())
//{
//XmlParser(file_name);
cout<<"path:"<<file_name<<endl<<*(parameter->in_directory)<<endl;
//}
(*(parameter->in_out_version))++;
memset(parameter->in_out_notification,'\0',1024);
}
Sleep(500);
}
return 0;
}
typedef struct ThreadParameter
{
LPTSTR in_directory;//the path of monitored data file
FILE_NOTIFY_INFORMATION *in_out_notification;//storage the pointer infomationof monitor method
DWORD in_MemorySize;//storage the byte of returned information's memory
DWORD *in_out_BytesReturned;//storage the byte of return information of monitor method
DWORD *in_out_version;//version
FILE_NOTIFY_INFORMATION *temp_notification;
}ThreadParameter;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
wchar_t *p= L"D:\\temp";
LPCWSTR path = p;
char notify[1024];
memset(notify,'\0',1024);
FILE_NOTIFY_INFORMATION *Notification = (FILE_NOTIFY_INFORMATION *)notify;
FILE_NOTIFY_INFORMATION *TempNotification = NULL;
LPTSTR Directory = p;
DWORD BytesReturned = 0;
DWORD version = 0;
//the monitor of data file
//the define of threadparameter
ThreadParameter ParameterToThread={Directory,Notification,sizeof(notify),&BytesReturned,&version,TempNotification};
//create a thread to monitor the change of data file
HANDLE TrheadWatch=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WatchChanges,&ParameterToThread,0,NULL);
CloseHandle(TrheadWatch);
return a.exec();
}
/*
*function : monitor the change of file
*parameter : Thread's parameter
*return :
*/
DWORD WINAPI WatchChanges(LPVOID lpParameter)
{
ThreadParameter *parameter = (ThreadParameter*)lpParameter;
LPCTSTR WatchDirectory = parameter->in_directory;//
int edit_flag = 0;
//create a directory handle
HANDLE handle_directory = CreateFile(WatchDirectory,FILE_LIST_DIRECTORY,
FILE_SHARE_READ |
FILE_SHARE_WRITE |
FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,NULL);
if(handle_directory == INVALID_HANDLE_VALUE)
{
//DWORD ERROR_DIR=GetLastError();
}
//the state of the ReadDirectoryChangesW
BOOL watch_state;
while (TRUE)
{
watch_state = ReadDirectoryChangesW(handle_directory,
(LPVOID)parameter->in_out_notification,
parameter->in_MemorySize,
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE,
(LPDWORD)parameter->in_out_BytesReturned,
NULL,
NULL);
if (GetLastError() == ERROR_INVALID_FUNCTION)
{
cout<<"current system is unsupported!\n"<<endl;
}
else if(watch_state == 0)
{
cout<<"monitor is fail!\n"<<endl;
}
else if (GetLastError() == ERROR_NOTIFY_ENUM_DIR)
{
cout<<"out of memory!\n"<<endl;
}
else
{
string file_name;
DWORD length = WideCharToMultiByte(0,0,parameter->in_out_notification->FileName,-1,NULL,0,NULL,NULL);
PSTR ps = new CHAR[length];
if(length >= 0)
{
WideCharToMultiByte(0,0,parameter->in_out_notification->FileName,-1,ps,length,NULL,NULL);
file_name = string(ps);
delete[] ps;
}
if (parameter->in_out_notification->Action == FILE_ACTION_ADDED)
{
cout<<file_name<<"FILE_ACTION_ADDED"<<endl;
}
if (parameter->in_out_notification->Action == FILE_ACTION_REMOVED)
{
cout<<file_name<<"FILE_ACTION_REMOVED"<<endl;
}
if (parameter->in_out_notification->Action == FILE_ACTION_MODIFIED)
{
edit_flag++;
if(edit_flag == 1)
{
cout<<file_name<<"FILE_ACTION_MODIFIED"<<endl;
}
else if(edit_flag == 2)
{
edit_flag = 0;
(*(parameter->in_out_version))--;
}
else
return -1;//break;
}
if (parameter->in_out_notification->Action == FILE_ACTION_RENAMED_OLD_NAME)
{
cout<<file_name<<"FILE_ACTION_RENAMED_OLD_NAME"<<endl;
}
if (parameter->in_out_notification->Action == FILE_ACTION_RENAMED_NEW_NAME)
{
cout<<file_name<<"FILE_ACTION_RENAMED_NEW_NAME"<<endl;
}
//QDir dir_make;
//tr("%1/%2").arg();
//file_name += *parameter->in_directory;
//if(dir_make.exists())
//{
//XmlParser(file_name);
cout<<"path:"<<file_name<<endl<<*(parameter->in_directory)<<endl;
//}
(*(parameter->in_out_version))++;
memset(parameter->in_out_notification,'\0',1024);
}
Sleep(500);
}
return 0;
}