GetDiskFreeSpaceExW() unicode version



  • Hallo,

    ich brauche Hilfe bei der Methode GetDiskFreeSpaceExW(). Ich benutze unicode und die Methode liefert mir eine 0. Ich weiss nicht was ich falsch mache. Kann mir jemand helfen.

    bool USBStick::checkMemory()
    {
    	DISK_GEOMETRY pdg;            // disk drive geometry structure
    	HANDLE hDevice = INVALID_HANDLE_VALUE;               // handle to the drive to be examined 
    	BOOL bResult;                 // results flag
    	DWORD junk;                   // discard results
    	ULONGLONG DiskSize;           // size of the drive, in bytes	
    	char driveL[10] = "\\\\.\\";
    	TCHAR path[10] ;
    	__int64 freeBytesAvailable, totalNumberOfBytes,totalNumberOfFreeBytes;//ULARGE_INTEGER
    	P_GDFSE pGetDiskFreeSpaceEx = NULL;
    
    	//Get a path to a USB stick	
    	strncat(driveL,driveLetter,4);	
    	MultiByteToWideChar(CP_ACP,0,driveL,-1,path,sizeof(path));
    
    	//Open  a USB stick
    	hDevice = CreateFile(path,  // path to the USB stick 
    		GENERIC_READ,                // no access to the drive
    		FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode		
    		NULL,             // default security attributes
    		OPEN_EXISTING,    // disposition
    		0,                // file attributes
    		0);            // do not copy file attributes
    
    	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    	{
    		printf("CreateFile() faild, error %d\n",GetLastError());
    		return (FALSE);
    	}
    
    	bResult = DeviceIoControl(hDevice,  // device to be queried
    		IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
    		NULL, 0, // no input buffer
    		&pdg, 
    		sizeof(pdg),     // output buffer
    		&junk,                 // # bytes returned
    		(LPOVERLAPPED) NULL);  // synchronous I/O
    
    	CloseHandle(hDevice);
    
    	if (bResult) 
    	{
    
    		DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
    			(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    		printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
    			DiskSize / (1024 * 1024 * 1024));
    	} 
    	else 
    	{
    		printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    	}
    
    	/*
    	pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(L"kernel32.dll"),"GetDiskFreeSpaceExW");
    	if(pGetDiskFreeSpaceEx)
    	{*/
    		bResult = GetDiskFreeSpaceExW(path,(PULARGE_INTEGER)&freeBytesAvailable,(PULARGE_INTEGER)&totalNumberOfBytes,(PULARGE_INTEGER) &totalNumberOfFreeBytes);
    
    		if(bResult){
    
    		printf("%I64u B\n",freeBytesAvailable);
    			printf("%I64u B\n",totalNumberOfBytes);
    			printf("%I64u B\n",totalNumberOfFreeBytes);
    		}
    	//}
    	if(((DiskSize/1024) - record) > 0){
    		return true;
    	}else{
    		return false;
    	}
    }
    


  • Warum rufst du nicht GetLastError auf, wenn du eine 0 zurückbekommst, das würde dir schon mal weiterhelfen. Weiter oben machst du das ja auch, bzw. das wohl von dir übernommene Beispiel.



  • Da ist ja einiges durcheinander...
    Ursprünglich hast du den Pfad als char-Array, das dann per MultiByteToWideChar in ein Array aus TCHARs (!) konvertiert wird, nur im die Wide-Variante von GetFreeDiskSpaceEx aufzurufen. Das ist etwas sinnfrei.
    Außerdem, wozu der ganze Vorbau, GetDiskFreeSpaceExA ist doch alles, was du brauchst (ich gehe einmal davon aus, dass du nur prüfen willst, ob noch genug freier Speicherplatz zur Verfügung steht, if(((DiskSize/1024) - record) > 0) ist auch hier ein wenig widersprüchlich).
    Und die Variablen sollten auch vom Typ ULARGE_INTEGER sein, dann brauchst du keine Casts.



  • ich habe das Problem schon gelöst. Das ist die Version die Funktioniert:

    bool USBStick::checkMemory()
    {
    	DISK_GEOMETRY pdg;            // disk drive geometry structure
    	HANDLE hDevice = INVALID_HANDLE_VALUE;               // handle to the drive to be examined 
    	BOOL bResult;                 // results flag
    	DWORD junk;                   // discard results
    	ULONGLONG DiskSize;           // size of the drive, in bytes	
    	char driveL[10] = "\\\\.\\";
    	TCHAR path[10] ;
    	TCHAR pathT[4] ;
    	ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes,totalNumberOfFreeBytes;//ULARGE_INTEGER
    	P_GDFSE pGetDiskFreeSpaceEx = NULL;
    
    	//Get a path to a USB stick	
    	strncat(driveL,driveLetter,4);	
    	MultiByteToWideChar(CP_ACP,0,driveL,-1,path,sizeof(path));
    
    	//Open  a USB stick
    	hDevice = CreateFile(path,  // path to the USB stick 
    		GENERIC_READ,                // no access to the drive
    		FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode		
    		NULL,             // default security attributes
    		OPEN_EXISTING,    // disposition
    		0,                // file attributes
    		0);            // do not copy file attributes
    
    	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    	{
    		printf("CreateFile() faild, error %d\n",GetLastError());
    		return (FALSE);
    	}
    
    	bResult = DeviceIoControl(hDevice,  // device to be queried
    		IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
    		NULL, 0, // no input buffer
    		&pdg, 
    		sizeof(pdg),     // output buffer
    		&junk,                 // # bytes returned
    		(LPOVERLAPPED) NULL);  // synchronous I/O
    
    	CloseHandle(hDevice);
    
    	if (bResult) 
    	{
    
    		DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
    			(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    		printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
    			DiskSize / (1024 * 1024 * 1024));
    	} 
    	else 
    	{
    		printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    	}
    
    	MultiByteToWideChar(CP_ACP,0,driveLetter,-1,pathT,sizeof(pathT));
    
    	bResult = GetDiskFreeSpaceEx(pathT,(PULARGE_INTEGER)&freeBytesAvailable,(PULARGE_INTEGER)&totalNumberOfBytes,(PULARGE_INTEGER) &totalNumberOfFreeBytes);
    
    	if(bResult){
    
    		printf("%I64d B\n",freeBytesAvailable);
    		printf("%I64d B\n",totalNumberOfBytes);
    		printf("%I64d B\n",totalNumberOfFreeBytes);
    		printf("%I64d MB\n",((totalNumberOfFreeBytes.QuadPart/(1024*1024)) - record));
    
    		if(((totalNumberOfFreeBytes.QuadPart/(1024*1024)) - record) > 0){
    			return true;
    		}else{
    			return false;
    		}
    	}
    
    }
    

Anmelden zum Antworten