Results 1 to 1 of 1
  1. #1
    abhar
    abhar is offline
    Guest
    Join Date
    2013 Jun
    Posts
    1
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    Rep Power
    0

    Post Folder monitor - check Games product files for changes in the installed directory

    Code:
    #include "stdafx.h"
    #include <windows.h>
    
    int main()
    {
    	LPCTSTR szMonDirPath = TEXT("C:\\temp"); 
    
    	HANDLE hDir = CreateFile( szMonDirPath,		// pointer to the file path 
    		FILE_LIST_DIRECTORY,					// access (read/write) mode
    		FILE_SHARE_READ|FILE_SHARE_WRITE,		// share mode
    		NULL,									// security descriptor
    		OPEN_EXISTING,							// how to create
    		FILE_FLAG_BACKUP_SEMANTICS,				// FILE_FLAG_OVERLAPPED // file attributes
    		NULL									// file with attributes to copy
    		);
    
    	FILE_NOTIFY_INFORMATION Buffer[1024] = {0}; //The larger the value, the larger lenght filename can be logged
    
    	DWORD BytesReturned;
    
    	printf("This is FolderMon application :- \nCurrently Monitoring %S => \n\n", szMonDirPath);
    
    	char szFilename[MAX_PATH] = {0};			//var used to get the filename to print
    	char szTempFilename[MAX_PATH] = {0};		//var used to get the filename to print
    
    	int iCnt=1, iLen=0, iTempLen=0, iTrack=0, iFilenameEndLen=0;
    	char *pdest = NULL;
    
    	while (true) {
    
    		int ret = ReadDirectoryChangesW(
    			hDir,				// handle to directory
    			&Buffer,			// read results buffer
    			sizeof(Buffer),     // length of buffer
    			TRUE,				// monitoring option
    			// filter conditions
    			FILE_NOTIFY_CHANGE_SECURITY|
    			FILE_NOTIFY_CHANGE_SIZE|		// in file or subdir
    			FILE_NOTIFY_CHANGE_ATTRIBUTES|
    			FILE_NOTIFY_CHANGE_DIR_NAME|	// creating, deleting a directory or sub
    			FILE_NOTIFY_CHANGE_FILE_NAME,	// renaming,creating,deleting a file
    			&BytesReturned,					// bytes returned
    			NULL,							// overlapped buffer
    			NULL							// completion routine
    			);
    
    		if(!ret)
    		{
    			printf("\nFailed function ReadDirectoryChangesW()");
    			continue;
    		}
    
    		if(MAX_PATH == wcstombs(szFilename, Buffer->FileName, MAX_PATH))
    		{
    			printf("\nFailed function wcstombs()");
    
    			if(!memset(Buffer, 0x00, sizeof(Buffer)))
    			{
    				printf("\nFailed function memset()");
    			}			
    
    			continue;
    		}
    
    		iLen=0, iTempLen=0, iTrack=0, iFilenameEndLen=0;
    		pdest = NULL;
    
    		if(iCnt>1)
    		{	
    			iLen = strlen(szFilename);
    			iTempLen = strlen(szTempFilename);
    			if(iLen > iTempLen)
    			{
    				pdest = strstr(szFilename, szTempFilename);
    				iTrack = 1;
    			}
    			else
    			{
    				pdest = strstr( szTempFilename, szFilename);
    				iTrack = 2;
    			}
    
    			if(pdest)
    			{
    				switch(iTrack)
    				{
    				case 1:
    					szFilename[iTempLen] = '\0';
    					break;
    
    				case 2:
    					szFilename[iLen] = '\0';
    					break;
    				}
    			}
    		}
    
    		if(!memcmp(szTempFilename, szFilename, strlen(szFilename)))
    		{
    			if(!memset(Buffer, 0x00, sizeof(Buffer)))
    			{
    				printf("\nFailed function memset()");
    			}
    
    			continue;
    		}
    		
    		if(memcpy(szTempFilename, szFilename, MAX_PATH) == NULL)
    		{
    			printf("\nFailed memcpy()");
    		}
    
    		DWORD dwAction = Buffer[0].Action;
    
    		if(!memset(Buffer, 0x00, sizeof(Buffer)))
    		{
    			printf("\nFailed function memset()");
    			continue;
    		}
    		
    		switch(dwAction)
    		{
    		case FILE_ACTION_ADDED: 
    			//printf("\nfile added : %s", szFilename);
    			printf("\t%s", szFilename);
    			break; 
    
    		case FILE_ACTION_REMOVED: 
    			printf("file removed : %s\n", szFilename);
    			break; 
    
    		case FILE_ACTION_MODIFIED:
    			//printf("\nfile added : %s", szFilename);
    			printf("\t%s", szFilename);
    			break; 
    
    		case FILE_ACTION_RENAMED_OLD_NAME: 
    			printf("file rename - oldname : %s", szFilename);
    			break;
    
    		default:
    			printf("No file operation event matched");
    			break;
    		}
    
    		iCnt++;
    
    		memset(szFilename, 0x00, MAX_PATH);
    
    	}//while
    
    	CloseHandle( hDir );
    
    	return 0;
    }

Similar Threads

  1. Replies: 1
    Last Post: 2014-09-12, 01:33 AM
  2. Replies: 0
    Last Post: 2013-06-01, 10:50 PM
  3. Replies: 0
    Last Post: 2013-05-30, 08:18 PM
  4. [C#] Packet Sniffer/Monitor Source
    By Dwar in forum VB, .NET Framework
    Replies: 0
    Last Post: 2012-08-24, 01:39 AM
  5. Game run through the folder
    By moskwa2013 in forum Martial Empires
    Replies: 0
    Last Post: 2012-03-07, 02:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •