Unbalanced stack error when using GetDiskFreeSpaceExA
The following code (calling proc DiskFreeSpace) throws an "unbalanced stack" error message.
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, _
ByVal lpFreeBytesAvailableToCaller As Long, _
ByVal lpTotalNumberOfBytes As Long, _
ByVal lpTotalNumberOfFreeBytes As Long) As Long
Friend Shared Function DiskFreeSpace(ByVal sdirDrive As String) As Long
Dim Status As Long
Dim TotalBytes As Long
Dim FreeBytes As Long
Dim BytesAvailableToCaller As Long
Status = GetDiskFreeSpaceEx(sdirDrive, BytesAvailableToCaller, TotalBytes, FreeBytes)
Return FreeBytes
End Function
What is wrong here?
The exact error message is:
A call to PInvoke function 'Test!XYZ.Test.FN.MyFileSystem::GetDiskFreeSpaceEx' 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 t开发者_C百科he PInvoke signature match the target unmanaged signature.
Additional note: My function does need to work for UNC paths also (local and/or network).
I think your problem in signature. try to use this (from pinvoke):
<DllImport("Kernel32.dll", EntryPoint:="GetDiskFreeSpaceEx", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetDiskFreeSpaceEx( _
ByVal lpDirectoryName As String, _
ByRef lpFreeBytesAvailable As ULong, _
ByRef lpTotalNumberOfBytes As ULong, _
ByRef lpTotalNumberOfFreeBytes As ULong) As Boolean
End Function
Difference in returning value
I'm not very sure about this code but you could also use the code My.Computer.FileSystem.Drives
and find out the free, used and total space. I feel it is just easier to use the .NET in-built functions rather than trying to use invokes from Windows DLLs because after all a .NET Application will only work on a computer with a .NET Version same or greater than the one on which the application was built!
Cheers
精彩评论