Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting volumes

Status
Not open for further replies.

MagicFrisbee

Programmer
Feb 22, 2006
74
US
In my organization, when a user inserts a memory stick into a computer running Windows, Windows will often give it a drive letter that's already in use by a Novell network drive mapping. For instance, the stick will get the drive letter F:, though F: may already point somewhere. Consequently, they can't use Windows Explorer to work with their stick because F: still shows the network drive contents.

I have become familiar with the DiskPart utility and can run such a script from Delphi with ShellAPI's ShellExecute method. The problem is that DiskPart doesn't have an API--at least I haven't found one. Although I can use DiskPart at a prompt and see the connected volumes by drive letter, I can't seem to get a handle on this information at run-time. What function call can I make to replicate the "list volume" command, the "select volume" command, and the "assign letter" command?

GIS Programmer
City of Orem, UT
 
Check out GetLogicalDriveStrings, GetLogicalDrives, GetDriveType, GetVolumeInformation, and DefineDosDevice.

However, this sounds like a problem with Novell's software. If they give you grief over the phone just drive down to Provo and give them heck. ;-)

You might also want to check out the WM_DEVICECHANGE message (though I've never been able to figure out how to receive it...)

Good luck.
 
Yes, I've looked at those functions and tested them in Delphi. I've copied and pasted code that uses all those functions and they only seem to know about the network drives and their letters. That's why I looked at DiskPart--because it gives the true identities of drives.

I realize that once you give a USB drive a new letter, it remembers that when you put it in again. However, when you use a second stick, it gives it a network drive and not the same letter as the first USB drive. Anyway, I just want to be able to read the volumes as DiskPart does, then assign the same drive letter (S:) it, in case it tries a different drive than I'm expecting.

GIS Programmer
City of Orem, UT
 
I've had the exact same problem with a networked drive also mapped to F:

The problem originates with assigning a mapped network drive so close to the top of the alphabet. You are probably stuck with it as I am (here).

Our solution is: when a new USB drive is inserted, immediately right-click MyComputer and select "Manage".
Then select "Disk Manager" under "Storage". (May require admin privileges?)

Right-click the offending "Disk # Removable (F:)" and select "Change drive letter and paths...".
Press the "Change..." button and select a drive letter from the drop-down next to "Assign the following drive letter" and repeat clicking "OK". Ignore warnings.

From then on it will behave, as long as you put it back into the same USB port. If anyone can do this with Delphi, I'd like to see it.

The USBDLM Site Licence [sic] sounds like an expensive solution.

There can be security issues with allowing every flash drive that comes in the door to connect to a network.

Roo
Delphi Rules!
 
I appreciate the tip and link, but I'd rather not have yet another dependency for my software to work correctly. Clearly if a German can program such functionality, there must be API's or DLL's available which do what he's doing, even at a low level. The next best thing I can think of is to call ShellExecute with a call to DiskPart and have it send its output to a text file with >>, which I could read and parse 3 seconds later.

GIS Programmer
City of Orem, UT
 
roo0047,

You and I posted within 2 minutes of each other, so I was talking to TonHu's reply, not yours.

I am aware that the Computer Management window does graphically what DiskPart does on the command line. My only problem is that I don't trust my users to execute that series of mouse clicks very well every time. I'd rather do it for them programmatically. Anyway, my code always presumes F and changes it to S if DirectoryExists( 'S:' ) returns false.

In Delphi, I call the ShellExecute command with "DiskPart" as one parameter and "/A AssignUSBDrive.txt" as another parameter. AssignUSBDrive.txt looks like this:

select volume F
assign letter S

So, that is how with Delphi I change the physical volume on drive F (the USB drive) to drive S. But if Windows assigns something other than F, I get a support call asking me what's wrong.

GIS Programmer
City of Orem, UT
 

We have had the exact same problem, network drive F: and USB sticks always getting F:

The long term solution was easy.

Map the network drive as something else, like W:, on every PC.
Sure every single shortcut every single person has will be broken, (we still have that..) but it fixes it once and for all, and people will get over it after the first few days.


Steve (Delphi 2007 & XP)
 
You could always try loading up DiskPart in Dependency Walker and then see if you find any calls listed within the file that seem related to the tasks you want.

----------
Measurement is not management.
 
@MagicFrisbee - Yes I'd figured that out, but thanks for the clarification. DiskPart command is new to me, and sounds like a clever solution. I'll research it to see what else it can do. I use SUBST a lot but it doesn't fit all bills, so thanks. Glenn sounds like he may be on to something. SanDisk Cruzer flash-drives with U3 technology add an extra twist with its second partition which appears to Windows as a CD disk-drive. If you haven't run across one yet, heads-up.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top