Posted on 2006-06-07
Last Modified: 2012-06-27
I am writing a Windows front-end to a new storage array product. One of the software requirements is to provide a "Setup" Wizard that will initialize, partition, and format the array.

Unfortunately, I can't find a way to implement this. I've dug around a bit and the DDK routines IOCTL_DISK_CREATE_DISK looks promising but I can't seem to get it to work. The return code indicates success but examining the drive within the Disk Management snap-in after the call shows no visible change. Does anyone have an example of IOCTL_DISK_CREATE_DISK? Are there other alternatives? What am I missing?

Help me Obi-Wan-Experts Exchange, you're my only hope... =)
Question by:Ezchdahal
    LVL 15

    Expert Comment

    The IOCTL_DISK_CREATE_DISK control code initializes the specified disk and disk partition table using the information in the CREATE_DISK structure.
    To perform this operation, call the DeviceIoControl function with the following parameters.

    Look at the sample calling the API DeviceIOControl

    Author Comment

    Yep. I found those two links before posting. The sample code works but does not involve IOCTL_DISK_CREATE_DISK. I've written some code based on the sample but, while returning a positive return code, nothing else appears to happen. Here's my code.

    HANDLE hDevice;              
    BOOL bResult;                
    DWORD junk;                  

    hDevice = CreateFile("\\\\.\\PhysicalDrive2",  
          FILE_SHARE_READ |

    if (hDevice != INVALID_HANDLE_VALUE)
      CREATE_DISK cd;  
      cd.PartitionStyle = PARTITION_STYLE_MBR;
      cd.Mbr.Signature = 0x28;

      bResult = DeviceIoControl(hDevice,  
          &cd, sizeof(cd),    
          NULL, 0,                  
          (LPOVERLAPPED) NULL);  


    Perhaps the signature field has something to do with the problem. It wasn't
    clear what I should set it to. I tried 0 and 0x28.
    LVL 15

    Expert Comment

    You said you get a positive return code. Did you mean DeviceIoControl returned non zero value?

    I am not sure what the signature value should be. You can try to do this way.

    CREATE_DISK cd;  

    cdMbr.Signature = 0x28;

    cd.PartitionStyle = PARTITION_STYLE_MBR;
    cd.Mbr = cdMBR;

    Author Comment

    Yes. DeviceIoControl returns 1.

    I went ahead and tried your suggestion for completeness, the union semantics should be equivalent. The same response was generated.

    My assumptions must be wrong somewhere along the line. I wonder what the Disk Management Snap-In actually does when you select "Initialize Disk"?
    LVL 49

    Accepted Solution

    I don't know the answer to this, but there are some clues to what the Disk Manager does...

    First it displays "Connecting to the Logical Disk Manager Service"
    So I presume it communicates with a Service

    Next, you should be able to Check Dependancies of the snap-in executable and see what APIs it accesses.

    Finally there are utilities at for instance that let you see what system calls are excercised.  That would be a pain to do, but it might bw worth it if you can't otherwise find out.

    This SysInternals utility might help to provide a clue...

    You can always run diskpart.exe using CreateProcess.
    This is probably not what you want but it is good alternative if you want to get your job done very quickly.

    and this source code shoudl be right on point since FormatX

    You will surely want to take a look at...

        SHFormatDrive Function
    It is interactive (displaying a dialgo box) and perhaps that is the best way to go, considering how dangerous formatting operations are.  But if you must have  purely programatic execution, you might be able to automate the dialog by sending message to it.

    -- Dan
    LVL 49

    Expert Comment


    Author Comment

    Thanks Dan,

    To keep the post simple I didn't go into all the detail regarding solutions I had already explored but you have mentioned many of them. Specifically, diskpart, FormatX, and SHFormatDrive. Diskpart scripting is too scary as a production solution. That tool is dangerous. SHFormatDrive brings up UI, which I'd like to avoid. (Not because I want to write a virus, because I want the wizard to be as simple, tailored and foolproof as possible.) FormatX was my current favorite if I couldn't find a DDK solution for formatting. BTW. There's a couple useful Experts Exchange questions on FormatX for anyone researching the subject

    The initializing and partitioning comes first though. I noticed just a couple minutes ago that after disconnecting and reconnecting my test array, it showed up as initialized in Disk Management. This is promising. Perhaps, there is another call I need to make to flush the drive information and reload it. That will be an investigation for Monday...

    Oh and thanks for the APIMonitor suggestion. I was thinking StraceNT but APIMonitor might be a better way to go.

    LVL 49

    Expert Comment

    The Formatx source code is in easy-to-understand C code.
    After formatting, it calls
    which might be what makes up the Explorer to the new drive (for obvious reasons, I am not testing any of these suggestions :-).

    THis looks promising.  Maybe you need to mount the frive:

       Mounting a Volume at a Mount Point

    Author Comment

    Sure, I'll give Dan the points. The puzzle still remains unfinished (mainly because I've been working on other topics) but hopefully we've gathered most of the pieces to solve it.

    Thanks for the help Lakshmanan and Dan,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now