views:

33

answers:

2

Hello,

I would like to use the GetVolumeInformation call to retrieve the name of a removable device. I can retrieve the name just fine and store into a TCHAR array variable szVolNameBuff. Here is my code for that:

 // Get Volume Information to check for NTFS or FAT

  TCHAR szFileSys[256];
  TCHAR szVolNameBuff[256];
  DWORD dwSerial = 0;
  DWORD dwMFL = 0;
  DWORD dwSysFlags = 0;
  bool bSuccess;
  char fileType[255];
  int bSuccessdebug = 0;
  //LPCTSTR temp = _T("E:\\"); For debugging only

  bSuccess = GetVolumeInformation(drivePath, 
                                szVolNameBuff,
                                sizeof(szVolNameBuff),
                                &dwSerial, 
                                &dwMFL, 
                                &dwSysFlags,
                                szFileSys,
                                sizeof(szFileSys));

When i try to print the contents of the variable with the line:

printf("szVolNameBuff holds: %s \n", &szVolNameBuff);

I get an output of "T" instead of the name "Transcend" which is the name of the drive. I debugged it with Visual Studio 2008 and found out that the TCHAR array stores the name as: [0] 'T' [1] 0 [2] 'R' [3] 0 [4] 'A' [5] 0 [6] 'N' [7] 0

and so on and so forth. Why is that? I want the array to store the word as just:

[0] 'T' [1] 'R' [2] 'A' [3] 'N' [4] 'S'

to later use it for string concatenation. Is there a way to fix this?

+1  A: 

It looks like you are using the unicode Win32 APIs. You should use _tprintf so that the appropriate function (printf or wprintf) is used according to the character type.

If you don't know unicode - here's a quick overview. The reason this is happening is that the unicode for the regular ascii characters is a null byte followed by the ascii character. That's why you are seeing the string padded with nulls.

Note that when using TCHAR, you should also wrap all strings in the _T() macro, so that they are also declared of the correct type. If you follow this consistently, converting from unicode to ansi is just a matter of changing a preprocessor directive.

mdma
Check project settings for General/Character Set
Greg Domjan
Thanks for the printf tip! However, I still want to try and fix the array situation so that I can concatenate strings later. When I try to concatenate strings using lstrcat right now, I still get the error. Is there a way to concatenate strings so that I can get an output of (for example) "Example-Transcend" rather than "Example-T" ?
ffrstar777
You might have more luck using _tstrcat and all the related _tXXX functions. That way you know you are using the right set of functions and not mixing Unicode and ANSI.
mdma
A: 

The why is that you're using the Unicode versions of the Win32 APIs.

And there are 2 fixes. The first is to use the standard versions of the API which if you are using Visual Studio can be done by changing your project's Character Set to 'Not set' in Projects-> Properties-> Configuration Properties-> General-> Character Set, or by making sure UNICODE is not #defined before including windows.h if you aren't using VS.

The second fix, as mdma said, is to use the Unicode text manipulation function wprintf or use %S in the standard library. This is the preferred fix as your program would then become internationalisation friendly and work whatever character set the file names were using. However it would mean that all down stream functions would need to use Unicode too, which might mean a lot of work, depending on the size of the project.

DominicMcDonnell