0

If I have a path on a drive, how do I retrieve that drive's GUID number?

I need it to read this key:

HK_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume

enter image description here

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • Whatever it is you want to do, your approach is fundamentally flawed. You're attempting to create a program that relies on internal details of Windows that may change for any reason in the future. Don't do that. Take a step back, think about what you're *really* hoping to accomplish, and then figure out how to do that (possibly by asking if you get stuck). There will almost always be a more sensible method. –  May 18 '14 at 20:52
  • @hvd: OK, since you're so _sensible_, please tell me how to get the maximum size of a recycle bin on a drive? – c00000fd May 18 '14 at 20:56
  • 1
    What I meant is: why do you need to figure out that size? Unless getting the maximum size of the recycle bin is a goal all in itself, and that would surprise me, you'll have a specific problem for which you've determined that getting the maximum size of the recycle bin would be a step in solving that problem. I cannot think of any reason you might want this, other than in an improved Windows Explorer, in which case a potential better approach would be supporting shell extensions, and letting the system shell extension for the recycle bin handle it. –  May 18 '14 at 21:12
  • [Your previous question](http://stackoverflow.com/questions/23720519/how-to-safely-delete-folder-into-recycle-bin) gives some relevant details. That is a problem that *should* be solvable without resorting to anything like what you're trying now, though I must admit that off of the top of my head, I cannot tell you how. –  May 18 '14 at 21:20
  • Having thought about it, one way you might be able to prevent files from being permanently deleted is by locking them. The type of lock affects which operations get blocked, and ordinarily, a lock that prevents deletion doesn't prevent a file from being renamed or moved. It requires some work and testing, but you might be able to temporarily lock a file in such a way that moving it to the recycle bin is possible, but permanently deleting it would fail. (I'm not entirely sure it will actually work, but logically, I think it should, and there is a fair chance it will.) –  May 18 '14 at 21:47
  • @hvd: I'm sorry, can you be more specific about the "locks" you're talking about? – c00000fd May 18 '14 at 21:53
  • @hvd: I tried to open a folder with CreateFile before passing it to SHFileOperation, but in that case it simply returns 32 in either case, which is ERROR_SHARING_VIOLATION. – c00000fd May 18 '14 at 22:03
  • I was thinking that when a program is running, it's possible to move/rename the executable, but it's not possible to delete it. Meaning it should be possible to lock a different file the same way. Unfortunately, when actually trying to make use of that, I've now seen that even though moving *is* possible in general, moving *to the Recycle Bin* still doesn't work... –  May 18 '14 at 22:05
  • Thinking some more, actually, what you're asking for seems dangerous, even though it does initially seem like a good idea. It fails the "what if two programs do this?" test. Suppose the limit is a measly 50 MB. First, program 1 deletes a 40 MB file, but only if it can be moved to the recycle bin. Then, program 2 deletes another 40 MB file, but only if it can be moved to the recycle bin. It can: by *permanently* deleting the file deleted by program 1, even though it does *not* want the file deleted. The permanent deletion is an operation that can and will be performed automatically. –  May 18 '14 at 22:31
  • It can seem even worse if those "program 1" and "program 2" are actually the same program, and you inadvertently end up deleting *your own* files that you did not want deleted yet. –  May 18 '14 at 22:31
  • @hvd: I agree that using Recycle Bin is risky, but unfortunately my hands are tied. I'm going by client's specification. I tried to reason with him, but to no avail... – c00000fd May 18 '14 at 22:37
  • @hvd: Although I don't understand why the API itself that does the move to Recycle Bin can't fail if it has to permanently delete files. It clearly knows it internally because it shows the UI. Anyway... I see that it's a moot point to argue it here. This is Microsoft. – c00000fd May 18 '14 at 22:39

1 Answers1

2

You can use the winapi GetVolumeNameForVolumeMountPoint function to retrieve the GUID for the volume that has that drive letter.

#include "stdafx.h"
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR volumeName[MAX_PATH + 1] = { 0 };

    GetVolumeNameForVolumeMountPoint("C:\\", volumeName, MAX_PATH);
    _tprintf(_T("VolumeName: %s\n"), volumeName);
}

Here is the output:

VolumeName: \\?\Volume{f3b89248-8457-1ae3-9f8c-806e6f6b6923}\

From here you can use a substring or regex to cut out the part that contains the GUID.

Ove
  • 6,227
  • 2
  • 39
  • 68
  • Thank you. I thought of it too, but do we know for sure that the path will start with `\\?\Volume` and not `\\?\PinkElephant`? – c00000fd May 18 '14 at 21:32
  • The MSDN documentation for GetVolumeNameForVolumeMountPoint says that `This path is of the form "\\?\Volume{GUID}\" where GUID is a GUID that identifies the volume`. So I am pretty sure that you won't get a pink elephant. – Ove May 19 '14 at 07:04