Ladies and Gentlemen Coders,

I'm working with the code below:

    int GetDrives(void)
    {
        wchar_t LogicalDrives[MAX_PATH] = {0}; 
        DWORD r = GetLogicalDriveStringsW(MAX_PATH, LogicalDrives); 
        if ( r == 0 )
        {
             printf("Failed to get drive names. %ld", GetLastError()); 
             return 1; 
             }
             int NumDriveCount = 0;         
         if ( r > 0 && r <= MAX_PATH) 
         {
              wchar_t *SingleDrive = LogicalDrives; 
              while(*SingleDrive){
                                  printf("Local Drives: %s Drive Size: %ls", SingleDrive, ); 
                                  SingleDrive += wcslen(SingleDrive) +1; 
                                  NumDriveCount++;
                                  }    
                                  }
              printf("Total Drives: %i\n", NumDriveCount); 
    }

    #pragma comment(lib, "user32.lib")
    char DRIVE_TYPE[1]; 

    int GetDriveFree(char *DRIVE[])
    {
    //int GetDriveFree(void) {


        unsigned __int64 freeCall,
                         total,
                         free;

        int r = GetDiskFreeSpaceExW(L"C:\\", (PULARGE_INTEGER) &freeCall,
            (PULARGE_INTEGER) &total, (PULARGE_INTEGER) &free);

        if (r == 0) {

            wprintf(L"Failed to get free disk space %ld", GetLastError());
            return 1;
        }   

        wprintf(L"Available space to caller: %I64u MB\n", freeCall / (1024*1024));
        wprintf(L"Total space: %I64u MB\n", total / (1024*1024));
        wprintf(L"Free space on drive: %I64u MB\n", free / (1024*1024));

        return 0;
    }

And as you can see on line:

printf("Local Drives: %s Drive Size: %ls", SingleDrive, ); 
                              SingleDrive += wcslen(SingleDrive) +1; 
                              NumDriveCount++;



                          I have tried running 



  //ALSO LINK THIS TO FREE SPACE
                              //GetDriveFree(LogicalDrives); 
                              printf("This is the drive: %s\n", LogicalDrives); 
                              //GetDriveFree(LogicalDrives);
                              printf("This is a drive test %s\n", LogicalDrives); 
                              LogicalDrives = DRIVE_TYPE;
                              SingleDrive += wcslen(SingleDrive) +1; 



                          And, for some reason I can't for the life of me get this to work. I am beginning in C and have burned through 3 books in the last few days. Can someone maybe explain as to why this does not work? The rest of the code does work, but it's beginning to walk without holding onto something that is getting me. I'd greatly appreciate it. 

I am assuming you are working in Windows, as GetLogicalDriveStringsW and GetDiskFreeSpaceExW appear to be Windows functions.

What specifically is going wrong? Are you getting compiler warnings and/or errors? If so, which compiler? If the program runs, what behaviour are you getting that you are not expecting or what behaviour are you not getting that you were expecting?

Nutster,

The issue I'm having is that I'm trying to point the drives that I found to the drive function to get the size, but if I do something like this: 

char DRIVE_TYPE[1]; 

GetDiskSpace("%ls", LogicalDrives); 


I can't do it, I get this: 89 passing arg 1 of `GetDriveFree' from incompatible pointer type. Any suggestions? 

Thank you

I copied your code into a new VS2015 console application. After adding #include <Windows.h>, I found the following issues:

In C, try to write your functions so that all functions that all the called functions are defined (or at least declared) prior to the functions that call them.

Line 15: printf("Local Drives: %s Drive Size: %ls", SingleDrive, );
You use 2 format specifiers but only give one parameter; you have a comma at the end of the argument list. I think you want to have a variable or function call after that last comma. Why are you looking for a string to describe Drive Size? Maybe change that to %lu for a long unsigned interger.

Line 23: Why are you including a comment pragma? It makes no difference to the program execution and is really only useful when doing deep debugging. That is not the way to include a library in your program.

Line 24: The one character array DRIVE_TYPE is never referenced. I would recommend removing unused variables, like DRIVE_TYPE.

Line 26: You are actually defining two layers of pointers in this definition. In C, an array is passed as a pointer to the beginning of the array, so defining a pointer to an array, is actually a pointer to a pointer to the beginning of the array. This is not the same a passing a pointer to an array, which you have further down. I recommend removing the pointer symbol on the argument definition.

Line 35: The type casts are not necessary here because that is the type those pointers already are. I would change the string literal for the drive to the passed drive name.

Nutster,

Thank you for pointing that out. I'll check it out when I have a chance. I appreciate all your help. I will let you know what happens when I test it.

Thank you again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.