0

I am kinda stuck with this api GetDriveType(). Every time this api returns me DRIVE_NO_ROOT_DIR, I am passing drive letters like this "c:\" etc. Even for my primary drive "c:\" the api returns the same thing.

I have called GetLogicalDrives() before this to retrieve.

OS: WinXP sp3, NTFS file system

code: compiler VS2005

int main() { 
    bool folder; 
    String * filename; 
    char individualdrive[4],alldrives[100]; 
    memset(alldrives,0,100); 

    GetLogicalDriveStringsA(100,alldrives); 

    for(int i=0;(i<100)&&(alldrives[i]);i+=4) { 
        memset(individualdrive,0,4); 
        strncpy(individualdrive,alldrives+i,4); //extracting individual drive strings.
        if(DRIVE_FIXED == GetDriveTypeW((LPCWSTR)individualdrive)) 
            getfile((System::String*)individualdrive, filename,folder); }
   return 0; }
2
  • How do we help without your code? You need to post it, so we can see what you're trying exactly; then we can help figure out what's wrong with it. Also, there's no OS "Win sp3"; can we guess you mean "WinXP SP3"?
    – Ken White
    Apr 29 '11 at 11:23
  • You need to format your code. In the SO editor, select all the code with the mouse and then press the {} button above the editor.
    – user2100815
    May 2 '11 at 9:59
3

Without code we have to guess. My guesses are:

  • You failed to escape the \ as "C:\\".
  • You are passing an ANSI string to the Unicode version of the API, or vice versa.

It's very hard to see how the API could fail unless it was a coding error of this nature.


Having added code to your question it is now clear that the second of these bullet points is the explanation. You are passing ANSI string data to a Unicode API and this, of course, fails.

To fix this you are probably best switching all the code to use Unicode strings.

In addition to the Unicode/ANSI issues, I think the buffer handling code is suspect. I don't know why you are assuming 4 characters for a drive. I think it is possible for GetLogicalDriveStrings to return un-mapped drives.

4
  • +1 for the same reasons I mentioned in my comment to @unaperson, David. Good psychic debugging.
    – Ken White
    Apr 29 '11 at 20:58
  • @glc049 I know. I have updated my question with a diagnosis of the reason for it failing. May 2 '11 at 11:39
  • Their is an Ansi version of GetDriveType which is GetDriveTypeA. After including this my code works properly. And you were right about buffer management as well David. Thanks for the info. May 3 '11 at 9:42
  • @glc I'm glad you have solved your problem. please remember to accept an answer May 3 '11 at 10:46
1

You are supposed to pass the root directory of the drive, so that would be:

GetDriveType( "C:\\" );

Note the double backslash- the backlslash is a special "escape" character in C and C++ string literals, and must be escaped itself.

The following code returns 3 for me, which means a fixed drive:

#include <iostream>
#include <windows.h>
using namespace std;

int main() {
    cout << GetDriveType( "C:\\" ) << endl;
}

The following code lists all the drives on my system, together with the numeric value for thier type:

#include <windows.h>
#include <iostream>
using namespace std;

int main() {
    const int BUFSIZE = 100;
    char buffer[ BUFSIZE ];
    DWORD n = GetLogicalDriveStrings( BUFSIZE, buffer );
    DWORD i =  0;
    while( i < n )   {
        int t = GetDriveType( &buffer[i] );
        cout << &buffer[i] << " type is " << t << endl;
        i += strlen( &buffer[i] ) + 1 ;
    }
}

producing:

C:\ type is 3
D:\ type is 5
E:\ type is 5
F:\ type is 2
Q:\ type is 3
4
  • Good catch. I missed the single `` in the text; hopefully I would have spotted it if the OP had posted code. +1.
    – Ken White
    Apr 29 '11 at 20:57
  • I used the code unapersson posted.But same result. Here is the code May 2 '11 at 5:43
  • @glc049 You mean you compiled the EXACT code I posted, with no changes whatsoever, and it did not print 3?
    – user2100815
    May 2 '11 at 8:39
  • The code which unapersson has posted works fine. I am looking into my mistakes . But I've also looked into what David said, and i've found it to be correct. I don't know what is technically correct for now. But thanks to both of you.... May 2 '11 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.