==============================ModuleF===================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Compiled using VC++/VC++ .Net.....unmanaged... #include #include //Using two dimensional arrays to store the drive strings //Better try other ways such as using TCHAR etc... char drive2[13][5] = {"A:\\", "B:\\", "C:\\", "D:\\", "E:\\", "F:\\", "G:\\", "H:\\","I:\\", "J:\\", "K:\\", "L:\\"}; int main() { for(int i=0; i<12; i++) { UINT test = GetDriveType(drive2[i]); switch(test) { case 0: printf("Drive %s is type %d - Cannot be determined.\n", &drive2[i], test); break; case 1: printf("Drive %s is type %d - Invalid root path/Not available.\n", &drive2[i], test); break; case 2: printf("Drive %s is type %d - Removable.\n", &drive2[i], test); break; case 3: printf("Drive %s is type %d - Fixed.\n", &drive2[i], test); break; case 4: printf("Drive %s is type %d - Network.\n", &drive2[i], test); break; case 5: printf("Drive %s is type %d - CD-ROM.\n", &drive2[i], test); break; case 6: printf("Drive %s is type %d - RAMDISK.\n", &drive2[i], test); break; default : "Unknown value!\n"; } } return 0; } ---------------------------------------------------------------------------------------------------------------------- #include #include #include #include //initial value TCHAR szDrive[] = _T(" A:"); int main() { DWORD uDriveMask = GetLogicalDrives(); printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask); printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask); if(uDriveMask == 0) printf("GetLogicalDrives() failed with failure code: %d\n", GetLastError()); else { printf("This machine has the following logical drives:\n"); while(uDriveMask) {//Use the bitwise AND, 1–available, 0-not available if(uDriveMask & 1) printf(szDrive); //increment... ++szDrive[1]; //shift the bitmask binary right uDriveMask >>= 1; } printf("\n "); } return 0; } ---------------------------------------------------------------------------------------------------------------------- #include #include //Buffer length DWORD mydrives = 100; //Buffer for drive string storage char lpBuffer[100]; int main() { DWORD test = GetLogicalDriveStrings( mydrives, lpBuffer ); printf("GetLogicalDriveStrings() return value: %d\nError: %d \n", test, GetLastError()); printf("The logical drives of this machine are:\n"); for(int i = 0; i<100; i++) printf("%c", lpBuffer[i]); printf("\n"); return 0; } ---------------------------------------------------------------------------------------------------------------------- #include #include //Query these on my machine //L: is thumb drive, J: and K: are CD-ROMs char lpDeviceName[11][3] = {"A:", "C:", "D:", "E:", "F:", "G:", "H:","I:", "J:", "K:", "L:"}; //The buffer for storage char lpTargetPath[1000]; int main() { for(int i=0; i<13; i++) { //Using NULL for the parameter 1 is not working lol... DWORD test = QueryDosDevice(lpDeviceName[i], lpTargetPath, 1000); //Test the return value and error if any printf("\nQueryDosDevice() return value: %d, Error: %d\n", test, GetLastError()); printf("The DOS device for %s is:\n", lpDeviceName[i]); //Display the result for(int i = 0; i<35; i++) printf("%c", lpTargetPath[i]); } printf("\n"); return 0; } ---------------------------------------------------------------------------------------------------------------------- #include #include int main() { char pszDrive[10] = "C:\\"; //64 bits integer __int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes; DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters; BOOL test = GetDiskFreeSpaceEx( pszDrive, (PULARGE_INTEGER)&lpFreeBytesAvailable, (PULARGE_INTEGER)&lpTotalNumberOfBytes, (PULARGE_INTEGER)&lpTotalNumberOfFreeBytes ); printf("Drive to be checked: %s\n", pszDrive); printf("\nUsing GetDiskFreeSpaceEx()...\n"); //Check the return value printf("The return value: %d, error code: %d\n", test, GetLastError()); printf("Total number of free bytes available for user-caller: %ul\n", lpFreeBytesAvailable); printf("Total number of bytes available for user: %ul\n", lpTotalNumberOfBytes); //Just straight to the free bytes result printf("Total number of free bytes on disk: %ul\n", lpTotalNumberOfFreeBytes); BOOL fResult = GetDiskFreeSpace(pszDrive, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters); printf("\nUsing GetDiskFreeSpace()...\n"); printf("The return value: %d, error code: %d\n", fResult, GetLastError()); printf("Sector per cluster = %ul\n", dwSectPerClust); printf("Bytes per sector = %ul\n", dwBytesPerSect); printf("Free cluster = %ul\n", dwFreeClusters); printf("Total cluster = %ul\n", dwTotalClusters); //Using GetDiskFreeSpace() need some calculation for the //free bytes on disk printf("Total free bytes = %ul\n", (dwFreeClusters*dwSectPerClust*dwBytesPerSect)); return 0; } ---------------------------------------------------------------------------------------------------------------------- //For Win Xp #define _WIN32_WINNT 0x0501 #include #include #define BUFSIZE MAX_PATH #define FILESYSNAMEBUFSIZE MAX_PATH int main() { char buf[BUFSIZE]; //buffer for unique volume identifiers DWORD lpMaximumComponentLength; DWORD dwSysFlags; //flags that describe the file system char FileSysNameBuf[FILESYSNAMEBUFSIZE]; //handle for the volume search HANDLE hVol; //Open a scan for volumes. hVol = FindFirstVolume(buf, BUFSIZE); if(hVol == INVALID_HANDLE_VALUE) { printf ("No volumes found!\n"); return (-1); } BOOL test = GetVolumeInformation( buf, NULL, BUFSIZE, NULL, &lpMaximumComponentLength, &dwSysFlags, FileSysNameBuf, FILESYSNAMEBUFSIZE ); printf("The return value: %d\n", test); printf("The first volume found: %s\n", buf); printf("The buffer for volume name: %d\n", BUFSIZE); printf("The max component length: %d\n", lpMaximumComponentLength); printf("The file system flag: %d\n", dwSysFlags); printf("The file system: %s\n", FileSysNameBuf); printf("The buffer for file system name: %d\n", FILESYSNAMEBUFSIZE); CloseHandle(hVol); return 0; } ---------------------------------------------------------------------------------------------------------------------- //For Win Xp #define _WIN32_WINNT 0x0501 #include #include #define BUFSIZE MAX_PATH #define FILESYSNAMEBUFSIZE MAX_PATH BOOL ProcessVolume(HANDLE hVol, char *Buf, int iBufSize) { DWORD lpMaximumComponentLength; DWORD dwSysFlags; //flags that describe the file system char FileSysNameBuf[FILESYSNAMEBUFSIZE]; BOOL bFlag; //generic results flag GetVolumeInformation( Buf, NULL, BUFSIZE, NULL, &lpMaximumComponentLength, &dwSysFlags, FileSysNameBuf, FILESYSNAMEBUFSIZE ); ////////----------caution--------------/////// //For file system, in order the removal drives such as floppy //and CD-ROM to be recognized, you must insert the media... printf("The volume found: %s\n", Buf); printf("The buffer for volume name: %d\n", BUFSIZE); printf("The max component length: %d\n", lpMaximumComponentLength); printf("The file system flag: %d\n", dwSysFlags); printf("The file system: %s\n", FileSysNameBuf); printf("The buffer for file system name: %d\n\n", FILESYSNAMEBUFSIZE); bFlag = FindNextVolume( hVol, //handle to search being conducted Buf, //pointer to output iBufSize //size of output buffer ); return (bFlag); } int main() { char buf[BUFSIZE]; //buffer for unique volume identifiers HANDLE hVol; //handle for the volume scan BOOL bFlag; //Open a search for volumes. hVol = FindFirstVolume(buf, BUFSIZE); if(hVol == INVALID_HANDLE_VALUE) { printf("No volumes found!\n"); return (-1); } bFlag = ProcessVolume(hVol, buf, BUFSIZE); //Do while we have volumes to process. while(bFlag) { bFlag = ProcessVolume(hVol, buf, BUFSIZE); } //Close out the volume search //and close the handle bFlag = FindVolumeClose(hVol); return 0; } ---------------------------------------------------------------------------------------------------------------------- //For Win Xp #define _WIN32_WINNT 0x0501 #include #include #define Bufsize MAX_PATH #define FILESYSNAMEBufsize MAX_PATH //Process each mount point found here. //The result indicates whether there is //another mount point to be searched. //This routine prints out the path to a mount point and its target. BOOL ProcessVolumeMountPoint(HANDLE hPt, char *PtBuffer, DWORD dwPtBufsize, char *Buffer, DWORD dwBufsize) { BOOL bFlag; //Boolean result char Path[Bufsize]; //construct a complete path here char Target[Bufsize]; //target of mount at mount point printf("Volume mount point found is \"%s\"\n", PtBuffer); //Detect the volume mounted there. //Build a unique path to the mount point strcpy(Path, Buffer); strcat(Path, PtBuffer); bFlag = GetVolumeNameForVolumeMountPoint( Path, //input volume mount point or directory Target, //output volume name buffer Bufsize //size of volume name buffer ); if(!bFlag) printf("Attempt to get volume name for %s failed.\n", Path); else printf("Target of the volume mount point is %s.\n\n", Target); //Now, either get the next mount point and return it, or return a //value indicating there are no more mount points. bFlag = FindNextVolumeMountPoint( hPt, //handle to scan PtBuffer, //pointer to output string dwPtBufsize //size of output buffer ); return (bFlag); } //Process each volume. The Boolean result //indicates whether there is another //volume to be searched. BOOL ProcessVolume(HANDLE hVol, char *Buffer, int iBufsize) { BOOL bFlag; //generic results flag for return HANDLE hPt; //handle for mount point scan char PtBuffer[Bufsize]; //string buffer for mount points DWORD dwSysFlags; //flags that describe the file system char FileSysNameBuffer[FILESYSNAMEBufsize]; printf("Volume found is \"%s\".\n", Buffer); //Is this volume NTFS or other? GetVolumeInformation(Buffer, NULL, 0, NULL, NULL, &dwSysFlags, FileSysNameBuffer, FILESYSNAMEBufsize); ////////----------caution--------------/////// //For file system, in order the removal drives such as floppy //and CD-ROM to be recognized, you must insert the media... printf("The file system is %s\n", FileSysNameBuffer); //Detect support for reparse points, and therefore for volume //mount points, which are implemented using reparse points. if(!(dwSysFlags & FILE_SUPPORTS_REPARSE_POINTS)) { printf("This file system does not support volume mount points.\n\n"); } else { //Start processing mount points on this volume. hPt = FindFirstVolumeMountPoint( Buffer, //root path of volume to be scanned PtBuffer, //pointer to output string Bufsize //size of output buffer ); if(hPt == INVALID_HANDLE_VALUE) {printf("No volume mount points found!\n\n");} else { //Process the volume mount point. bFlag = ProcessVolumeMountPoint(hPt, PtBuffer, Bufsize, Buffer, Bufsize); //Do while we have volume mount points to process. while (bFlag) bFlag = ProcessVolumeMountPoint(hPt, PtBuffer, Bufsize, Buffer, Bufsize); FindVolumeMountPointClose(hPt); } } //Stop processing mount points on this volume. bFlag = FindNextVolume( hVol, //handle to scan being conducted Buffer, //pointer to output iBufsize //size of output buffer ); return (bFlag); } int main() { char Buffer[Bufsize]; //Buffer for unique volume identifiers HANDLE hVol; //handle for the volume scan BOOL bFlag; //generic results flag //Open a search for volumes. hVol = FindFirstVolume(Buffer, Bufsize); if(hVol == INVALID_HANDLE_VALUE) { printf("No volumes found!\n"); return (-1); } bFlag = ProcessVolume(hVol, Buffer, Bufsize); //Do while we have volumes to process. while(bFlag) {bFlag = ProcessVolume(hVol, Buffer, Bufsize);} //Close out the volume scan. //handle to be closed bFlag = FindVolumeClose(hVol); return (bFlag); } ---------------------------------------------------------------------------------------------------------------------- //*********mymount.cpp************ #define _WIN32_WINNT 0x0501 #include #include #define BUFSIZE MAX_PATH int main(int argc, char *argv[]) { BOOL bFlag; //temporary buffer for volume name char Buf[BUFSIZE]; //If not enough arguments supplied... //for this example you have to create the mount point first if(argc != 3) { printf("\"%s\" command mounts a volume at a mount point.\n", argv[0]); printf("Usage: %s \n", argv[0]); printf("And make sure the exists!!!\n", argv[0]); printf("Example: \"%s c:\\mymnt\\gdrive\\ g:\\\"\n", argv[0]); //Just exit return(-1); } //Some verification, check the inputs validity bFlag = GetVolumeNameForVolumeMountPoint( argv[2], //input volume mount point or directory Buf, //output volume name buffer BUFSIZE //size of volume name buffer ); if(bFlag != TRUE) { printf("Retrieving volume name for %s failed.\n", argv[2]); //Just exit return (-2); } printf("Volume name of %s is %s\n", argv[2], Buf); bFlag = SetVolumeMountPoint( argv[1], //mount point Buf //volume to be mounted ); //Another verification, error checking if(!bFlag) printf("Attempt to mount %s at %s failed.\n", argv[2], argv[1]); else { printf("%s is successfully mounted at %s\n", argv[2], argv[1]); char lpszFileName[100] = "F:\\mymntpoint\\projectc\\doc\\Borland.doc"; char lpszVolumePathName[100]; DWORD cchBufferLength = 100; BOOL test = GetVolumePathName( lpszFileName, lpszVolumePathName, cchBufferLength ); printf("The Volume path name for %s is:\n %s\n", lpszFileName, lpszVolumePathName); } return (bFlag); } ---------------------------------------------------------------------------------------------------------------------- //*******myunmount.cpp*********** #define _WIN32_WINNT 0x0501 #include #include int main(int argc, char *argv[]) { BOOL bFlag; //If the argument for the mount point is not given... if(argc != 2) { printf("%s command unmounts a volume from the volume mount point.\n", argv[0]); printf("Usage: \"%s \n", argv[0]); printf("Example: \"%s c:\\mymnt\\gdrive\\\"\n", argv[0]); //Just exit return (-1); } //Do some verification, error checking... //The argv[1] is a path of the volume mount point bFlag = DeleteVolumeMountPoint(argv[1]); if(!bFlag) { printf("Unmounting the volume at %s failed!\n", argv[1]); exit(-1); } else printf("Unmounting the volume at %s successfully done!\n", argv[1]); return (bFlag); } ===========================================================================================================================