Getting the main hardrive?

This is a discussion on Getting the main hardrive? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm using the volume serial of the harddrive for authorization of my program, I'm using the c:\ drive as ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    158

    Getting the main hardrive?

    Hi, I'm using the volume serial of the harddrive for authorization of my program, I'm using the c:\ drive as default but some user do not have a c:\ drive so I need some way to check what the main hardrive is called. I'm using code like so:
    Code:
    DWORD dwVolSerial;
    BOOL bIsRetrieved;
    bIsRetrieved = GetVolumeInformation("C:\\",NULL,NULL,&dwVolSerial,NULL,NULL,NULL,NULL);
    if (bIsRetrieved) 
    {
    	TSTR buf;
    	buf.printf(_T("%u"),dwVolSerial);
    	return new String(buf);
    } 
    return &undefined;
    Any idea on how to expand on this code to also include such a check?

    cheers,
    CML

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Main drive is boot drive or windows drive?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by Elysia View Post
    Main drive is boot drive or windows drive?
    Hi Elysia, I'm not quite sure what the difference is really. They are all windows systems so I guess I need the main drive where windows is installed.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    The computer has to boot from one hard drive, which is the boot drive.
    However, Windows does not need to be installed on the boot drive.
    The boot drive contains the OS boot loader, however.

    To get the hard drive which Windows is installed on, try using the GetWindowsDirectory API. It will return the full path to the Windows directory, including drive and directory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Thanks Elysia, I got that to work. For me that returns:
    C:\WINDOWS

    but the next problem is how do I split up that string to something I can pass to the GetVolumeInformation function? ("C:\\") I do not have much experience working with strings in Windows.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Well, I'll demonstrate a C method because I don't really use std::string.
    Code:
    char Drive[4];
    std::string WinDir = "C:\\WINDOWS";
    memcpy(Drive, WinDir.c_str(), 3);
    Drive[3] = 0;
    I'm probably going to get flamed for this
    Though I'm sure someone else will show a better solution.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    In a line....
    Code:
    strcpy(Drive, WinDir.substr(0,3).c_str() ) ;
    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    In that case, why not
    Code:
    std::string strDrive = WinDir.substr(0,3);
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I'm not familiar with the GetVolumeInformation() function, and whether it takes a c-string or a string. I assumed it took a c-string since that's what you had.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Of course it does, but you only need to do .c_str() to get a C-style string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Todd
    and whether it takes a c-string or a string.
    Quote Originally Posted by Elysia
    Of course it does,
    That's as clear as mud.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I looked it up. GetVolumeInformation takes a c-string.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Alright I got it working now, thanks a lot for the help, that was fast!
    Here is what I ended up with. Note that TCHAR and TSTR are substitutes for char and C string. If you can see any issue let me know.
    Code:
    TCHAR infoBuf[32767];
    if (GetWindowsDirectory(infoBuf, 32767))
    {
    	TSTR drive;
    	drive.printf(_T("%s"),infoBuf);
    	TSTR maindir = drive.Substr(0,3);
    	DWORD dwVolSerial;
    	BOOL bIsRetrieved = GetVolumeInformation(maindir,NULL,NULL,&dwVolSerial,NULL,NULL,NULL,NULL);
    	if (bIsRetrieved) 
    	{
    		TSTR buf;
    		buf.printf(_T("%u"),dwVolSerial);
    		return new String(buf);
    	} 
    	return &undefined;
    }
    return &undefined;

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Quote Originally Posted by Todd Burch View Post
    That's as clear as mud.
    Sorry, I meant "of course it takes a C-string."

    Quote Originally Posted by Todd Burch View Post
    I looked it up. GetVolumeInformation takes a c-string.
    All Windows API takes a C-string. No exceptions.

    Quote Originally Posted by DrSnuggles View Post
    Here is what I ended up with. Note that TCHAR and TSTR are substitutes for char and C string.
    No, it's not. They are multi-byte/unicode "safe" macros that you can use to make the project compile with either unicode or multi-byte. But since you are using _T, all is well.

    Code:
    TCHAR infoBuf[32767];
    if (GetWindowsDirectory(infoBuf, 32767))
    {
    	TSTR drive;
    	drive.printf(_T("%s"),infoBuf);
    	TSTR maindir = drive.Substr(0,3);
    	DWORD dwVolSerial;
    	BOOL bIsRetrieved = GetVolumeInformation(maindir,NULL,NULL,&dwVolSerial,NULL,NULL,NULL,NULL);
    	if (bIsRetrieved) 
    	{
    		TSTR buf;
    		buf.printf(_T("%u"),dwVolSerial);
    		return new String(buf);
    	} 
    	return &undefined;
    }
    return &undefined;
    [/QUOTE]

    Side note: you can first call GetWindowsDirectory with no buffer and 0 for size. It will return the needed size to hold the path. You allocate that and then call it to make sure it works, whatever the length.
    I'm pretty sure you can assign a string buffer to a std::string without the need of std::string.printf.
    Also return NULL, not undefined if it fails.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Thanks for the tips Elysia. Yes those are indeed Unicode safe macros. The values I'm returning are actually values that are returned to the scripting language of the program I'm making the plugin for.(3ds max)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 01:36 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 05:29 PM
  4. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 10:46 PM
  5. getting a function to return to main??
    By Neildadon in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2002, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21