SHFormatDrive error
-
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
-
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
-
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
-
-
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
-
Shouldn't the
Me.Handle.ToInt32
beMe.Handle
(a long?) --edit-- PInvoke.net[^] claims that the signature should containuint
, not along
.I are Troll :suss:
modified on Sunday, March 7, 2010 6:48 AM
-
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
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 -
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; auint
isn't along
.I are Troll :suss:
-
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
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... -
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
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 FunctionPublic 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 SubIt 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
-
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 FunctionPublic 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 SubIt 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
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 -
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 -
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
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 -
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.comI 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")
NextHere G: is my pen drive's drive name. Suggest me how to format my pen drive. Thanks. Gagan