Click to See Complete Forum and Search --> : GetVolumeInformation Compatibility


ahoodin
December 13th, 2006, 09:03 AM
I wrote a program that looks for the volume name using GetVolumeInformation Win32 API call. I need to run it on different versions of Windows. On Windows XP the the disk volume name or disk label is returned. On windows 98, the disk volume (VolName) name returns an empty string.

Does anybody have any experience with this? Is this a bug in MS or my code? :ehh: MSDN turns up nothing(as of yet).

Is there a better API call?

Here is a short version that shows the problem.

// volumeinfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "volumeinfo.h"
#include <stdio.h>
#include <direct.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int MoveBackup(char *fname);
void lerror(char *cfunc, int err);
/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CString svRoot("D://");
DWORD dwVNS=32, dwFSNS;
char VolName[288]="\0",FSName[288];
dwVNS = (sizeof(VolName)/sizeof(VolName[0]));
dwFSNS = sizeof(FSName);
//SetVolumeLabel("c:\\","Development");
for( int drive = 1; drive <= 26; drive++ )
if( !_chdrive( drive ) )
{
printf( "%c: ", drive + 'a' - 1 );

svRoot.Format("%c://",drive+'a'-1);

GetVolumeInformation(
svRoot, // address of root directory of the file system
&VolName[0],// address of name of the volume
dwVNS, // length of lpVolumeNameBuffer
NULL, // address of volume serial number
NULL, // address of system's maximum filename length
NULL,
NULL, // address of name of file system
NULL); // length of lpFileSystemNameBuffer

printf("%s\n",VolName);
if (!(strncmp(VolName,"EDMBACKUP",9)))
{
break;
}
}
}

return nRetCode;
}

Boris K K
December 13th, 2006, 10:18 AM
Why two forward slashes ("//") in cvRoot?

Only the backslash needs to be doubled in string literals because it has a special meaning combined with other symbols.
The native path separator for Windows is backslash.


Also check function result and GetLastError().