How to get the drive letter of pendrive - mfc

I want to get the drive letter of pendrive or external harddisk connected through USB.
Is it possible to get the Drive letter. I am using VC++

GetLogicalDriveStrings lists all existing drive letters and GetDriveType can determine whether the drive is removable. GetDriveType help suggests using SetupDiGetDeviceRegistryProperty to make sure the drive is USB one.

Related

How do I use the drive I just mapped on net use? (C++)

I'm writing code on C++ that will map to the nearest available drive using "net use * \server...etc" and afterwards I want to open a file on it.
Is there a command that will let me interact with this new drive without needing to know on which drive it was mapped beforehand? (Usually it will be drive Z:, but not always!)
Clearer example:
system("net use * \\server\folder\ p455word /user:server\user /p:no")
(output: "Server succesfully mapped to drive Z:")
Then I would normally try to open the file:
system("Z:\\folder\mydoc.docx")
My question is on how to do this:
system("*\\\folder\mydoc.docx")
With * being whatever the drive the computer chose to map to, or if there's a workaround to this. Thanks!
If you're looking for some net use-specific magic here, you're going to be disappointed. Your program simply passes a text string to the Windows shell, and has no knowledge or information about the command you've performed. It certainly can't pull out status information about the result of that call, beyond reading the command's text output.
Fortunately, the text output is all you need — your quote shows that the assigned drive letter is right there. You just need to pull it out, then construct the subsequent system calls dynamically by building up a string from the now-known drive letter, and the fixed part of your path.
system is not really designed for any of this, though. Here's how to execute a command and retrieve its output. Extracting the drive letter from the string "Server succesfully mapped to drive Z:" would also be a separate question. In general, when you have a problem, break it down into component parts; then you'll be able to "look it up" with success.

How do I convert drive Letter to a physical drive name in c++?

My question is this:
Given an offline disk connected to my windows machine, how can I get it's volume letter?
The scenario:
I need to create a handler to a shared storage (with CreateFile function) in order to use it in the DeviceIoControl function (need to perform SCSI reservations).
The "CreateFile" function gets the physical drive name (i.e \.\PhysicalDisk1) or the volume letter of the disk (i.e \.\E:).
My problem is, that I don't want to use the physical drive name, just the disk letter.
This works fine when the disk is online, but I have to support also when the disk is offline.
Is there a way to determine the letter when the device/disk is offline?
I assume there is, because when I get the disk online, windows automatically gives it the right letter. So if the operation system knows what will be the letter, there must be an API or a tool that I can use too.
Thanks,
Daniel.
Unfortunately, when a drive is offline the drive letter is available for use by another drive that may be inserted. So although Windows remembers what drive letter a particular drive used to have, and it attempts to re-use the same letter when it is re-inserted, if another drive was inserted that used the remembered letter then the old drive will get a different letter if it is then inserted.
But you cannot open a Physical Drive, either by name or letter, if that drive is not currently connected. You'll simply get the equivalent of "File not found".

Find USB Mass Storage Instance Id based on Drive Letter in Window

I have a requirement in which I need to fetch USB Mass Storage Metadata [VID, PID and Serial No].
I found a nice post for enumerating USB devices and list out all the Mass storage Devices.
Here is the link
Now What I want is based on the drive letter, need to retrieve USB Mass Storage Metadata.
Thanks in advance for your kind support.
If you had read and understood the "USB drive to drive letter" questions, this one would have been simple too. Enumerate both, compare, pick the match. Works in both directions.

How do I check if a Visual C++ argument is a valid drive letter?

Like the title says, I have a C++ program and the user passes in an argument. I want to validate whether the argument passed is a valid DOS/Windows drive letter (i.e. C:. D:. E:) and that the drive exists on the system. How do I do this?
You can get the type of a drive with GetDriveType() - which also tells you if a drive doesn't exist or isn't availble
Or the 'correct' way is to enumerate through all the drives

How to differentiate Whether a Drive or NetWork Drive, on RightClick

When I Rightclick on a Shell Drive I want to differentiate whether the Drive is a Normal Drive or a Network Drive.
I hope we can do this using Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY) method but unsure which parameter to use.
Initialize is now documented to take a PCIDLIST_ABSOLUTE (not LPCITEMIDLIST), so you know it's rooted in My Desktop. My Computer is the second ItemID on that list, and the drive is the third ItemID. As Luke indicated, once you have the drive, GetDriveType will tell you whether the drive is remote.
You can use GetDriveType.