windows下调用WINAPI函数ReadDirectoryChangesW监控目录

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;


}
Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc DefFrameProc DefMDIChildProc DefWindowProc DispatchMessage GetMessage GetMessageExtraInfo GetMessagePos GetMessageTime GetQueueStatus InSendMessage PeekMessage PostMessage PostQuitMessage PostThreadMessage RegisterWindowMessage ReplyMessage SendMessage SendMessageCallback SendMessageTimeout SendNotifyMessage SetMessageExtraInfo SetWindowsHookEx TranslateMessage UnhookWindowsHookEx WaitMessage Window Information AnyPopup ChildWindowFromPoint ChildWindowFromPointEx EnableWindow EnumChildWindows EnumPropsEx EnumThreadWindows EnumWindows FindWindow FindWindowEx GetClassInfoEx GetClassLong GetClassName GetClientRect GetDesktopWindow GetFocus GetForegroundWindow GetNextWindow GetParent GetProp GetTopWindow GetWindow GetWindowLong GetWindowRect GetWindowText GetWindowTextLength IsChild IsIconic IsWindow IsWindowEnabled IsWindowUnicode IsWindowVisible IsZoomed RemoveProp SetActiveWindow SetClassLong SetFocus SetForegroundWindow SetParent SetProp SetWindowLong SetWindowText WindowFromPoint Processes and Threads CreateEvent CreateMutex CreateProcess CreateSemaphore CreateThread DeleteCriticalSection DuplicateHandle EnterCriticalSection ExitProcess ExitThread GetCurrentProcess GetCurrentProcessId GetCurrentThread GetCurrentThreadId GetExitCodeProcess GetExitCodeThread GetPriorityClass GetThreadPriority GetWindowThreadProcessId InitializeCriticalSection InterlockedDecrement InterlockedExchange InterlockedIncrement LeaveCriticalSection OpenEvent OpenMutex OpenProcess OpenSemaphore PulseEvent ReleaseMutex ReleaseSemaphore ResetEvent ResumeThread SetEvent SetPr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenjian0415

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值