0

I've inherited some c++ code for formatting USB flash drives, but it fails if the flash disk is in a bad state and I can't figure out why (or what makes the flash disk state bad).

If the flash drive (usb fob) has a partition but no file system, and it's in the funny state, my WMI format code fails with error 11 "no media in drive", but using the Windows Disk Management widget the drive formats just fine. And after formatting with the Disk Management widget, my WMI code will also format it fine from that point forward. My code will even format it fine after removing the partition using diskpart and re-creating the partition, assuming it was formatted with the Disk Management widget prior to removing the partition.

I can reproduce this with multiple brands and sizes of USB flash drive. To put it into the funny state, I first copy data to the drive, then remove and recreate the partition using diskpart. Afterwords sometimes the drive will be in the funny state where I cannot format it, and sometimes I can format it fine. Always the Disk Management widget can format it fine. Running my code "As administrator" makes no difference.

Can anyone help me figure out what the Disk Management widget is doing differently, or what I'm missing? I'm at a loss as to why it doesn't work when it doesn't work, so I really don't know what to try next for a solution.

Here's what I'm doing in code (C++):

/* Parameters are set as follows */
pInParInst->FileSystem = exFat
pInParInst->QuickFormat = true
pInParInst->Label = "MyDisk"


IWbemServices::ExecMethod ("\\PC-NAME\ROOT\CIMV2:Win32_Volume.DeviceID="\\\\?\\Volume{f110cf76-ff7b-11e2-8697-005056c00008}\\"", 
                            "Format", 
                            0, 
                            NULL, 
                            pInParInst, 
                            &pOutPar, 
                            NULL); 




/* When checking the result:  */
pOutPar->Get(L"ReturnValue", 0, &vtRetVal, NULL, 0);

vtRetVal.vt = 3 /* type is int */
vtRetVal.intVal = 11 /* No media in drive */

Here is what the flash disk looks like in Windows Disk Management widget before formatting it:

enter image description here

akmozo
  • 9,829
  • 3
  • 28
  • 44
Matt
  • 391
  • 5
  • 15
  • The Disk Management "widget" probably uses another api like e.g. [Disk Management Control Codes](https://msdn.microsoft.com/en-us/library/aa363979%28v=vs.85%29.aspx) , [Virtual Disk Service](https://msdn.microsoft.com/en-us/library/aa383429%28v=vs.85%29.aspx) and/or [Windows Storage Management Provider](https://msdn.microsoft.com/en-us/library/hh830613%28v=vs.85%29.aspx) – VolkerK Apr 08 '16 at 20:56
  • You could be right, but I wonder if there's a way to make it work with this API since it is already implemented. :) I just discovered that using either diskpart to format, or "format.com" in a cmd window to format the partition also fix the bad state. It looks like I'm the only program in town that doesn't know the trick! :( – Matt Apr 08 '16 at 21:47
  • Inheritance is supposed to make you feel better, more money to spend from deceased uncle Harry. This isn't it, turns out he was betting on the ponies and you are paying off his bookie. Much easier to fix in programming than in real life, it is a non-feature that you can trivially delete. – Hans Passant Apr 08 '16 at 22:16
  • I'm getting some comments from the Microsoft forum that other people have seen similar behavior to what I'm seeing, and had to use a different Microsoft API to overcome the issue. – Matt Apr 11 '16 at 15:32

0 Answers0