Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. SHFormatDrive error

SHFormatDrive error

Scheduled Pinned Locked Moved Visual Basic
databasedata-structuresregexjsonhelp
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DaveAuld

    Can you not just use My.Computer.FileSystem.Drives(x).DriveFormat() and keep it managed? Ignore, i'm speaking pish!

    Dave Don't forget to rate messages!
    Find Me On: Web|Facebook|Twitter|LinkedIn
    Waving? dave.m.auld[at]googlewave.com

    modified on Sunday, March 7, 2010 7:45 AM

    G Offline
    G Offline
    Gagan 20
    wrote on last edited by
    #4

    daveauld wrote:

    My.Computer.FileSystem.Drives(x).DriveFormat()

    Don't you know what it does? It only tells the drive format as FAT or NTFS and does not format the drive. I want to format my pen drive not to get its drive format name. Gagan

    D 1 Reply Last reply
    0
    • G Gagan 20

      daveauld wrote:

      My.Computer.FileSystem.Drives(x).DriveFormat()

      Don't you know what it does? It only tells the drive format as FAT or NTFS and does not format the drive. I want to format my pen drive not to get its drive format name. Gagan

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #5

      Apologies misread the doc's, when i saw the FAT/NTFS, i thought that was the format options. :doh: Note To Self: Don't skim read! :sigh:

      Dave Don't forget to rate messages!
      Find Me On: Web|Facebook|Twitter|LinkedIn
      Waving? dave.m.auld[at]googlewave.com

      1 Reply Last reply
      0
      • L Lost User

        Shouldn't the Me.Handle.ToInt32 be Me.Handle (a long?) --edit-- PInvoke.net[^] claims that the signature should contain uint, not a long.

        I are Troll :suss:

        modified on Sunday, March 7, 2010 6:48 AM

        G Offline
        G Offline
        Gagan 20
        wrote on last edited by
        #6

        I tried Me.Handle.ToInt64 as it returns a long value but I still have the same error.

        L 1 Reply Last reply
        0
        • G Gagan 20

          Hi all... I am trying to format my pen drive by calling API SHFormatDrive function but it gives an error message. I am using the following lines of code :

          Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal iDrive As Long, ByVal iCapacity As Long, ByVal iFormatType As Long) As Long
          Const SHFD_CAPACITY_DEFAULT = 0 'default drive capacity
          Const SHFD_FORMAT_QUICK = 0 'Quick format.
          Const SHFD_FORMAT_FULL = 1 'Full format.

          Private Sub btnStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
          
                   Try
             
                      Dim drvToFormat As Long = 6 '6 is the index of pendrive for G: drive.
                      Dim ret As Long
          
                      If chkQuickFormat.Checked Then 'Check for quick format.
                          ret = SHFormatDrive(Me.Handle.ToInt32, \_
                          drvToFormat, \_
                          SHFD\_CAPACITY\_DEFAULT, \_
                          SHFD\_FORMAT\_QUICK)
                      Else
                          ret = SHFormatDrive(Me.Handle.ToInt32, \_
                          drvToFormat, \_
                          SHFD\_CAPACITY\_DEFAULT, \_
                          SHFD\_FORMAT\_FULL)
                      End If
          
                      Select Case ret
                          Case -1
                              MsgBox("Error during format operation")
                          Case -2
                              MsgBox("Operation canceled by user")
                          Case -3
                              MsgBox("This drive cannot be formatted")
                          Case Else
                              MsgBox("Done")
                      End Select
                  Catch ex As Exception
               
                      MsgBox(ex.Message)
                  End Try
              End If
          End Sub
          

          The above code is giving an error that "A call to PInvoke function 'SHFormatDrive' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature." I don't know exactly what is going wrong and when I am trying to format my pen drive, the above code is first checking for A: drive and then it is giving error. Suggest me what should I do. Thanks. Gagan

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #7

          See here for code; http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/ca51f9f8-5422-4e1d-8ee6-497e8b66b2b5[^] I don't know where you got your initial reference from, but it is different to the MSDN Docs for SHFormatDrive? If the code in the link works then sorted! [Update - Just tried the code, works fine.]

          Dave Don't forget to rate messages!
          Find Me On: Web|Facebook|Twitter|LinkedIn
          Waving? dave.m.auld[at]googlewave.com

          1 Reply Last reply
          0
          • G Gagan 20

            I tried Me.Handle.ToInt64 as it returns a long value but I still have the same error.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            That would only pose a problem if the handle doesn't fit in a Int32. Still, seems like the error you're getting is supported by the PInvoke.net claim; a uint isn't a long.

            I are Troll :suss:

            1 Reply Last reply
            0
            • G Gagan 20

              Hi all... I am trying to format my pen drive by calling API SHFormatDrive function but it gives an error message. I am using the following lines of code :

              Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal iDrive As Long, ByVal iCapacity As Long, ByVal iFormatType As Long) As Long
              Const SHFD_CAPACITY_DEFAULT = 0 'default drive capacity
              Const SHFD_FORMAT_QUICK = 0 'Quick format.
              Const SHFD_FORMAT_FULL = 1 'Full format.

              Private Sub btnStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
              
                       Try
                 
                          Dim drvToFormat As Long = 6 '6 is the index of pendrive for G: drive.
                          Dim ret As Long
              
                          If chkQuickFormat.Checked Then 'Check for quick format.
                              ret = SHFormatDrive(Me.Handle.ToInt32, \_
                              drvToFormat, \_
                              SHFD\_CAPACITY\_DEFAULT, \_
                              SHFD\_FORMAT\_QUICK)
                          Else
                              ret = SHFormatDrive(Me.Handle.ToInt32, \_
                              drvToFormat, \_
                              SHFD\_CAPACITY\_DEFAULT, \_
                              SHFD\_FORMAT\_FULL)
                          End If
              
                          Select Case ret
                              Case -1
                                  MsgBox("Error during format operation")
                              Case -2
                                  MsgBox("Operation canceled by user")
                              Case -3
                                  MsgBox("This drive cannot be formatted")
                              Case Else
                                  MsgBox("Done")
                          End Select
                      Catch ex As Exception
                   
                          MsgBox(ex.Message)
                      End Try
                  End If
              End Sub
              

              The above code is giving an error that "A call to PInvoke function 'SHFormatDrive' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature." I don't know exactly what is going wrong and when I am trying to format my pen drive, the above code is first checking for A: drive and then it is giving error. Suggest me what should I do. Thanks. Gagan

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #9

              Gagan.20 wrote:

              Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal iDrive As Long, ByVal iCapacity As Long, ByVal iFormatType As Long) As Long

              You found and used a Declare for SHFormat that works with VB6, not VB.NET. Hint: Long in VB6 was a 32-bit signed Integer. Long in VB.NET is a 64-bit signed Integer.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              1 Reply Last reply
              0
              • G Gagan 20

                Hi all... I am trying to format my pen drive by calling API SHFormatDrive function but it gives an error message. I am using the following lines of code :

                Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal iDrive As Long, ByVal iCapacity As Long, ByVal iFormatType As Long) As Long
                Const SHFD_CAPACITY_DEFAULT = 0 'default drive capacity
                Const SHFD_FORMAT_QUICK = 0 'Quick format.
                Const SHFD_FORMAT_FULL = 1 'Full format.

                Private Sub btnStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
                
                         Try
                   
                            Dim drvToFormat As Long = 6 '6 is the index of pendrive for G: drive.
                            Dim ret As Long
                
                            If chkQuickFormat.Checked Then 'Check for quick format.
                                ret = SHFormatDrive(Me.Handle.ToInt32, \_
                                drvToFormat, \_
                                SHFD\_CAPACITY\_DEFAULT, \_
                                SHFD\_FORMAT\_QUICK)
                            Else
                                ret = SHFormatDrive(Me.Handle.ToInt32, \_
                                drvToFormat, \_
                                SHFD\_CAPACITY\_DEFAULT, \_
                                SHFD\_FORMAT\_FULL)
                            End If
                
                            Select Case ret
                                Case -1
                                    MsgBox("Error during format operation")
                                Case -2
                                    MsgBox("Operation canceled by user")
                                Case -3
                                    MsgBox("This drive cannot be formatted")
                                Case Else
                                    MsgBox("Done")
                            End Select
                        Catch ex As Exception
                     
                            MsgBox(ex.Message)
                        End Try
                    End If
                End Sub
                

                The above code is giving an error that "A call to PInvoke function 'SHFormatDrive' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature." I don't know exactly what is going wrong and when I am trying to format my pen drive, the above code is first checking for A: drive and then it is giving error. Suggest me what should I do. Thanks. Gagan

                G Offline
                G Offline
                Gagan 20
                wrote on last edited by
                #10

                When I am using the below code, it appears a Windows format window to format the drive.

                _
                Private Shared Function SHFormatDrive(ByVal hwnd As IntPtr, ByVal drive As UInteger, ByVal fmtID As UInteger, ByVal options As UInteger) As UInteger
                End Function

                Public Enum SHFormatFlags As Integer
                    SHFMT\_ID\_DEFAULT = &HFFFF
                    SHFMT\_OPT\_FULL = &H1
                    SHFMT\_OPT\_SYSONLY = &H2
                    SHFMT\_ERROR = &HFFFFFFFF
                    SHFMT\_CANCEL = &HFFFFFFFE
                    SHFMT\_NOFORMAT = &HFFFFFFD
                End Enum
                

                Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

                Dim ret = SHFormatDrive(Me.Handle.ToInt64, _
                drvToFormat, _
                CUInt(SHFormatFlags.SHFMT_ID_DEFAULT), _
                0)
                msgbox(ret.tostring)
                End Sub

                It appears Windows format option to format the drive but I want that when I click on start button, it start formatting rather than displaying another window to format drive. Tell me what should I do. Thanks. Gagan

                D 1 Reply Last reply
                0
                • G Gagan 20

                  When I am using the below code, it appears a Windows format window to format the drive.

                  _
                  Private Shared Function SHFormatDrive(ByVal hwnd As IntPtr, ByVal drive As UInteger, ByVal fmtID As UInteger, ByVal options As UInteger) As UInteger
                  End Function

                  Public Enum SHFormatFlags As Integer
                      SHFMT\_ID\_DEFAULT = &HFFFF
                      SHFMT\_OPT\_FULL = &H1
                      SHFMT\_OPT\_SYSONLY = &H2
                      SHFMT\_ERROR = &HFFFFFFFF
                      SHFMT\_CANCEL = &HFFFFFFFE
                      SHFMT\_NOFORMAT = &HFFFFFFD
                  End Enum
                  

                  Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

                  Dim ret = SHFormatDrive(Me.Handle.ToInt64, _
                  drvToFormat, _
                  CUInt(SHFormatFlags.SHFMT_ID_DEFAULT), _
                  0)
                  msgbox(ret.tostring)
                  End Sub

                  It appears Windows format option to format the drive but I want that when I click on start button, it start formatting rather than displaying another window to format drive. Tell me what should I do. Thanks. Gagan

                  D Offline
                  D Offline
                  DaveAuld
                  wrote on last edited by
                  #11

                  You could use WMI to format the drive; see http://msdn.microsoft.com/en-us/library/aa390432(VS.85).aspx[^] I don't have any sample code to share, you will have to do some digging yourself. [Edit: this page says that it is supported on Server only, not Clients. I would imagine there is an equivalent somewhere that is supported.]

                  Dave Don't forget to rate messages!
                  Find Me On: Web|Facebook|Twitter|LinkedIn
                  Waving? dave.m.auld[at]googlewave.com

                  G 1 Reply Last reply
                  0
                  • D DaveAuld

                    You could use WMI to format the drive; see http://msdn.microsoft.com/en-us/library/aa390432(VS.85).aspx[^] I don't have any sample code to share, you will have to do some digging yourself. [Edit: this page says that it is supported on Server only, not Clients. I would imagine there is an equivalent somewhere that is supported.]

                    Dave Don't forget to rate messages!
                    Find Me On: Web|Facebook|Twitter|LinkedIn
                    Waving? dave.m.auld[at]googlewave.com

                    G Offline
                    G Offline
                    Gagan 20
                    wrote on last edited by
                    #12

                    The WMI method you suggest to format drive is good but I've still some doubt. In this WMI method I do not understand that what drive it will format or what should I do to format a perticular drive. Suggest me what should I do. Thanks. Gagan

                    D 1 Reply Last reply
                    0
                    • G Gagan 20

                      The WMI method you suggest to format drive is good but I've still some doubt. In this WMI method I do not understand that what drive it will format or what should I do to format a perticular drive. Suggest me what should I do. Thanks. Gagan

                      D Offline
                      D Offline
                      DaveAuld
                      wrote on last edited by
                      #13

                      This Format method of WMI is a member of a Volume object, therefore, it would format that particular volume. Why don't you start with the link i gave you and work your way back up the WMI documentation, and you will see all sorts of classes and methods. There are classes that enumerate the logical disk, volumes, drive types etc. etc. There are examples scattered all over the place to do different things, so i guess a little bit of googling will eventually get you want you want.

                      Dave Don't forget to rate messages!
                      Find Me On: Web|Facebook|Twitter|LinkedIn
                      Waving? dave.m.auld[at]googlewave.com

                      G 1 Reply Last reply
                      0
                      • D DaveAuld

                        This Format method of WMI is a member of a Volume object, therefore, it would format that particular volume. Why don't you start with the link i gave you and work your way back up the WMI documentation, and you will see all sorts of classes and methods. There are classes that enumerate the logical disk, volumes, drive types etc. etc. There are examples scattered all over the place to do different things, so i guess a little bit of googling will eventually get you want you want.

                        Dave Don't forget to rate messages!
                        Find Me On: Web|Facebook|Twitter|LinkedIn
                        Waving? dave.m.auld[at]googlewave.com

                        G Offline
                        G Offline
                        Gagan 20
                        wrote on last edited by
                        #14

                        I have a VBScript that uses WMI to format a drive. I got the code from some site but it is not formatting my pen drvie and giving error while executing the script. The code is below:

                        strComputer = "."
                        Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

                        Set colVolumes = objWMIService.ExecQuery _
                        ("Select * from Win32_Volume Where Name = 'G:\\'")

                        For Each objVolume in colVolumes
                        errResult = objVolume.Format("NTFS")
                        Next

                        Here G: is my pen drive's drive name. Suggest me how to format my pen drive. Thanks. Gagan

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups