--- mstools/samples/filer/enumdrv.c 2018/08/09 18:20:59 1.1 +++ mstools/samples/filer/enumdrv.c 2018/08/09 18:24:28 1.1.1.3 @@ -1,3 +1,14 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + /**************************************************************************** * * PROGRAM: Enumdrv.C @@ -5,16 +16,9 @@ * PURPOSE: Determines all drives in the system, both local and remote, * and their file system type * -* FUNCTIONS: -* -* EnumDrives() - main algorithm -* CheckRM() - verifies that a removeable media drive contains a disk -* -* COMMENTS: -* ****************************************************************************/ +#define STRICT #include -#include #include "globals.h" #include "filer.h" #include "enumdrv.h" @@ -26,7 +30,7 @@ extern LPDINFO glpDrives; /**************************************************************************** * -* FUNCTION: CheckRM(LPSTR) +* FUNCTION: CheckRM(LPTSTR) * * PURPOSE: Verifies that a removeable media drive contains a disk * @@ -43,29 +47,39 @@ extern LPDINFO glpDrives; * unwanted dialog boxes that prompt for disks to be placed in the * drive. * -* INPUT: lpszDriveName - removeable media drive name (ex - "a:") +* INPUT: szDriveName - removeable media drive name (ex - "a:") * * OUTPUT: Returns TRUE if media present * FALSE if media is not present * ****************************************************************************/ -BOOL CheckRM(LPSTR lpszDriveName) +BOOL CheckRM( LPTSTR lpszDriveName ) { - int iRC; - LPSTR lpszFileName=" "; - OFSTRUCT ofstruct; - WORD wStyle=OF_EXIST; - - SetErrorMode(1); - strcpy(lpszFileName,lpszDriveName); - strcat(lpszFileName,"."); - iRC=OpenFile(lpszFileName,&ofstruct,wStyle); - SetErrorMode(0); - if (iRC==-1) - return (FALSE); - _lclose(iRC); - return (TRUE); + TCHAR szFileName[DIRECTORY_STRING_SIZE]; + DWORD dwHold; + + SetErrorMode( SEM_FAILCRITICALERRORS ); + + lstrcpy( szFileName, lpszDriveName ); + lstrcat( szFileName, TEXT(".") ); + + dwHold = GetFileAttributes( szFileName ); + + SetErrorMode( 0 ); + + // If no error, media must be in drive. + if( dwHold != 0xFFFFFFFF ){ + return(TRUE); + } + else{ + dwHold = GetLastError(); + if( dwHold != ERROR_NOT_READY ) + ErrorMsg(TEXT("CheckRM: Get Removable Media Info Failure.")); + + return(FALSE); + } + } @@ -88,32 +102,26 @@ BOOL CheckRM(LPSTR lpszDriveName) * was created with the GetLogicalDrives call, then this drive is * bypassed. * -* GetVolumeInformation is used to determine the file system -* for the logical drive. This information is returned in the -* lpszFileSystemNameBuffer variable. If the drive type is -* remote, a check must be made to see if the drive contains a -* disk. This is accomplished by opening '.' in the root -* directory of the remote drive. The error level is temporarily -* changed from 0 to 1, to allow any OpenFileErrors to immediately -* return to the calling function, thus eliminating any unwanted -* dialog boxes. If the remote drive contains a disk, then it is -* safe to proceed with the GetVolumeInformation call. +* GetVolumeInformation is used to determine the file syste for the +* logical drive. If the drive type is removable, a check must be made +* to see if the drive contains a disk. If the remote drive contains a +* disk, then it is safe to proceed with the GetVolumeInformation call. * * INPUT: LPDINFO dINfo: A pointer to a DRVINFO Structure. * * OUTPUT: Returns the number of DINFO structures in the linked * list pointed to by dInfo. Value is negative if error. * -****************************************************************************/ +\****************************************************************************/ void EnumDrives(LPDINFO *lplpRoot) { DWORD dwDriveMask; DWORD dwCount; - LPSTR lpszRootPathName="?:\\"; + LPTSTR lpszRootPathName=TEXT("?:\\"); - static LPSTR lpDriveStrings = NULL; - LPSTR lpParse; + static LPTSTR lpDriveStrings = NULL; + LPTSTR lpParse; LPDINFO lpDInfo, // new node ptr lpHold, // list walking ptrs @@ -129,30 +137,31 @@ void EnumDrives(LPDINFO *lplpRoot) // if(lpDriveStrings != NULL){ EnterCriticalSection(&gHeapCS); - HeapFree( ghHeap, lpDriveStrings); + HeapFree( ghHeap, 0, lpDriveStrings); LeaveCriticalSection(&gHeapCS); } dwCount=GetLogicalDriveStrings( 0, lpDriveStrings); if( !dwCount ){ LeaveCriticalSection(&gDrvCS); - ErrorMsg("EnumDrives: Get Drive Strings error"); + ErrorMsg(TEXT("EnumDrives: Get Drive Strings error")); ExitThread((DWORD)-1); } EnterCriticalSection(&gHeapCS); - lpDriveStrings = HeapAlloc( ghHeap, dwCount); + // allocate memory, +1 for trailing NULL + lpDriveStrings = (LPTSTR)HeapAlloc( ghHeap, 0, (dwCount + 1) * sizeof(TCHAR) ); LeaveCriticalSection(&gHeapCS); if( lpDriveStrings == NULL){ LeaveCriticalSection(&gDrvCS); - ErrorMsg("EnumDrives: Allocation error"); + ErrorMsg(TEXT("EnumDrives: Allocation error")); ExitThread((DWORD)-2); } - if(dwCount != GetLogicalDriveStrings( dwCount, lpDriveStrings) ){ + if(dwCount < GetLogicalDriveStrings( dwCount, lpDriveStrings) ){ LeaveCriticalSection(&gDrvCS); - ErrorMsg("EnumDrives: Drive String size Changed!"); + ErrorMsg(TEXT("EnumDrives: Drive String size Changed!")); ExitThread((DWORD)-3); } lpParse = lpDriveStrings; @@ -165,7 +174,7 @@ void EnumDrives(LPDINFO *lplpRoot) dwCount = 0; lpHold = lpBack = lpRoot; - for (*lpszRootPathName='a';*lpszRootPathName<='z';(*lpszRootPathName)++){ + for (*lpszRootPathName=TEXT('a');*lpszRootPathName<=TEXT('z');(*lpszRootPathName)++){ if (dwDriveMask & 1){ // drive exists. Insert or update. dwCount++; @@ -179,11 +188,11 @@ void EnumDrives(LPDINFO *lplpRoot) // Allocating memory for DRVINFO node // EnterCriticalSection(&gHeapCS); - lpDInfo = (LPDINFO) HeapAlloc( ghHeap, (DWORD)sizeof(DRVINFO) ); + lpDInfo = (LPDINFO) HeapAlloc( ghHeap, 0, (DWORD)sizeof(DRVINFO) ); LeaveCriticalSection(&gHeapCS); if( lpDInfo == NULL){ LeaveCriticalSection(&gDrvCS); - ErrorMsg("EnumDrives: DRVINFO Allocation error"); + ErrorMsg(TEXT("EnumDrives: DRVINFO Allocation error")); ExitThread((DWORD)-4); } @@ -211,18 +220,25 @@ void EnumDrives(LPDINFO *lplpRoot) } lpBack->DriveType = GetDriveType(lpszRootPathName); - if( lpBack->DriveType != DRIVE_REMOVABLE || - ( lpBack->DriveType == DRIVE_REMOVABLE && CheckRM(lpszRootPathName) ) ) - if( !GetVolumeInformation(lpszRootPathName,NULL,NULL,NULL,NULL,NULL, - (LPSTR)lpBack->FileSystem, - (DWORD)FILE_SYSTEM_STRING_SIZE) ) - strcpy(lpBack->FileSystem, "UNKNOWN"); + if( lpBack->DriveType == DRIVE_REMOVABLE || + lpBack->DriveType == DRIVE_CDROM ) + if( CheckRM(lpszRootPathName ) ) + if( !GetVolumeInformation(lpszRootPathName,NULL,0,NULL,NULL,NULL, + (LPTSTR)lpBack->FileSystem, + (DWORD)FILE_SYSTEM_STRING_SIZE) ) + lstrcpy(lpBack->FileSystem, TEXT("UNKNOWN")); + else; + else // no removable media + lstrcpy(lpBack->FileSystem, TEXT("UNKNOWN")); + else // not removable + if( !GetVolumeInformation(lpszRootPathName,NULL,0,NULL,NULL,NULL, + (LPTSTR)lpBack->FileSystem, + (DWORD)FILE_SYSTEM_STRING_SIZE) ) + lstrcpy(lpBack->FileSystem, TEXT("UNKNOWN")); else; - else - strcpy(lpBack->FileSystem, "UNKNOWN"); lpBack->DriveName = lpParse; - lpParse += strlen(lpParse)+1; + lpParse += lstrlen(lpParse)+1; } else{ // drive does not exist. if( lpHold ) // if node exists, delete it. @@ -233,7 +249,7 @@ void EnumDrives(LPDINFO *lplpRoot) lpBack->next = lpHold->next; EnterCriticalSection(&gHeapCS); - HeapFree(ghHeap, (LPSTR)lpHold); + HeapFree(ghHeap, 0, (LPVOID)lpHold); LeaveCriticalSection(&gHeapCS); lpHold = lpBack->next;