user1835004
user1835004

Reputation: 1

stuck with api getdrivetype

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; }

Upvotes: 0

Views: 4253

Answers (2)

user2100815
user2100815

Reputation:

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

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612964

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.

Upvotes: 4

Related Questions