Advertisement
Guest User

Untitled

a guest
Oct 25th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. DWORD ListDirectory(LPTSTR path, dirEntry* dirBuffer, DWORD* bufferSize, DWORD* bufferPosition){
  2.     DWORD lastError;
  3.     HANDLE findHandle = NULL;
  4.     dirEntry* temp = NULL;
  5.     WIN32_FIND_DATA dataFound;
  6.     char errMsg[MAX_BUFFER_SIZE];
  7.  
  8.     _RPTFW1(_CRT_WARN, _T("Listing directory: %s\n"), path);
  9.     if((path == NULL) || (dirBuffer = NULL) || (*bufferSize <= 0) || (*bufferPosition < 0)){
  10.         lastError = ERROR_INVALID_PARAMETER;
  11.         goto error;
  12.     }
  13.  
  14.     findHandle = FindFirstFile(path, &dataFound);
  15.     if((findHandle == NULL) || (findHandle == INVALID_HANDLE_VALUE)){
  16.         lastError = GetLastError();
  17.         goto error;
  18.     }
  19.  
  20.     _tprintf(_T("%s"), dataFound.cFileName);
  21.     dirBuffer[*bufferPosition].fileSizeLow = dataFound.nFileSizeLow;
  22.     _tcscpy(dirBuffer[*bufferPosition].fileName, dataFound.cFileName);
  23.     (*bufferPosition)++;
  24.  
  25.     while(FindNextFile(findHandle, &dataFound) != 0){
  26.         if(*bufferPosition == *bufferSize){
  27.             temp = (dirEntry*) realloc(dirBuffer, (*bufferSize + MEM_SIZE) * sizeof(dirEntry));
  28.             if(temp == NULL){
  29.                 lastError = ERROR_NOT_ENOUGH_MEMORY;
  30.                 goto error;
  31.             } else {
  32.                 dirBuffer = temp;
  33.                 *bufferSize += MEM_SIZE;
  34.             }
  35.         }
  36.         dirBuffer[*bufferPosition].fileSizeLow = dataFound.nFileSizeLow;
  37.         _tcscpy(dirBuffer[*bufferPosition].fileName, dataFound.cFileName);
  38.         (*bufferPosition)++;
  39.     }
  40.  
  41.     goto finalize;
  42.  
  43. error:
  44.     ExtractMessage(lastError, errMsg);
  45.     _RPTFW1(_CRT_ERROR, _T("Listing Failed! Cause: %S"), errMsg);
  46.     if((findHandle != NULL) && (findHandle != INVALID_HANDLE_VALUE)){
  47.         CloseHandle(findHandle);
  48.     }
  49.     return lastError;
  50. finalize:
  51.     _RPTFW0(_CRT_WARN, _T("Listing successfull.\n"));
  52.     CloseHandle(findHandle);
  53.     return ERROR_SUCCESS;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement