Win32 handle that was passed to icon is not valid or is the wrong type.

I was testing articlehttp://support.microsoft.com/kb/319340/ to see if I could use this to populate a listview2 with the icons fromDirectory.GetFiles(apath) and getDirectories. If you go into c:\windows\system32 the program will hang for about 4 mins then if you go back to windows then back to system32 you will get the error.

win32 handle that was passed to icon is not valid or is the wrong type.

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: BufferedGraphicsContext cannot be disposed of because a buffer operation is currently in progress.
at System.Drawing.BufferedGraphicsContext.Dispose(Boolean disposing)
at System.Drawing.BufferedGraphicsContext.Dispose()
at System.Drawing.BufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
at System.Drawing.BufferedGraphicsContext.AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
at System.Drawing.BufferedGraphicsContext.Allocate(IntPtr targetDC, Rectangle targetRectangle)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Imports System.Runtime.InteropServices

Imports System.IO

PublicClass Form1

PrivateStructure SHFILEINFO

Public hIconAs IntPtr' : icon

Public iIconAsInteger' : icondex

Public dwAttributesAsInteger' : SFGAO_ flags

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _

Public szDisplayNameAsString

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _

Public szTypeNameAsString

EndStructure

PrivateDeclareAnsiFunction SHGetFileInfoLib"shell32.dll" (ByVal pszPathAsString, _

ByVal dwFileAttributesAsInteger,ByRef psfiAs SHFILEINFO,ByVal cbFileInfoAsInteger, _

ByVal uFlagsAsInteger)As IntPtr

PrivateConst SHGFI_ICON = &H100

PrivateConst SHGFI_SMALLICON = &H1

PrivateConst SHGFI_LARGEICON = &H0' Large icon

Private nIndex = 0

Public files()As IO.FileSystemInfo

PrivateSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

listview2.LargeImageList = ImageList1

listview2.SmallImageList = ImageList1

listview2.Columns.Add("File Name", 250, HorizontalAlignment.Center)

listview2.Columns.Add("Last Access", 75, HorizontalAlignment.Left)

listview2.Columns.Add("Last Write", 75, HorizontalAlignment.Right)

LogicalDrive()

EndSub

PublicSub PopulateListView(ByVal apathAsString)

Try

Dim hImgSmallAs IntPtr'The handle to the system image list.

Dim hImgLargeAs IntPtr'The handle to the system image list.

Dim fnameAsNew ArrayList(Directory.GetDirectories(apath))'The file name to get the icon from.

Dim myNameAsString() = Directory.GetFiles(apath)

Dim countAs Int32 = 0

While count < myName.Length

fname.Add(myName(count))

count += 1

EndWhile

Dim shinfoAs SHFILEINFO

shinfo =New SHFILEINFO()

listview2.Items.Clear()

listview2.BeginUpdate()

listview2.View = View.Details

shinfo.szDisplayName =NewString(Chr(0), 260)

shinfo.szTypeName =NewString(Chr(0), 80)

'Use this to get the small icon.

count = 0

While count < fname.Count

hImgSmall = SHGetFileInfo(fname(count), 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICONOr SHGFI_SMALLICON)

'Use this to get the large icon.

'hImgLarge = SHGetFileInfo(fname(count), 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)

'The icon is returned in the hIcon member of the shinfo struct.

Dim myIconAs System.Drawing.Icon

myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

ImageList1.Images.Add(myIcon)'Add icon to imageList.

Dim myItemAsNew ListViewItem(fname(count).ToString)

If File.Exists(fname(count))Then

myItem.SubItems.Add(File.GetCreationTime(fname(count)))

myItem.SubItems.Add(File.GetLastAccessTime(fname(count)))

myItem.ImageIndex = nIndex

Else

myItem.SubItems.Add(Directory.GetCreationTime(fname(count)))

myItem.SubItems.Add(Directory.GetLastAccessTime(fname(count)))

myItem.ImageIndex = nIndex

EndIf

listview2.Items.Add(myItem)

'listview.Items.Add(fname(count), nIndex) 'Add file name and icon to listview.

nIndex = nIndex + 1

count += 1

EndWhile

fname.Clear()

listview2.EndUpdate()

Catch exAs Exception

MessageBox.Show(ex.Message.ToString)

EndTry

EndSub

PublicSub LogicalDrive()

Dim driveList()AsString = Directory.GetLogicalDrives

Dim driveAsString =""

cmb.Items.Add(My.Computer.FileSystem.SpecialDirectories.MyDocuments)

ForEach driveIn driveList

cmb.Items.Add(drive)

Next

Dim senderAs System.Object =Nothing

Dim eAs System.EventArgs =Nothing

cmb.Text ="d:\"'My.Computer.FileSystem.SpecialDirectories.MyDocuments

'cmb_SelectedIndexChanged(sender, e)

EndSub

PrivateSub cmb_KeyDown(ByVal senderAsObject,ByVal eAs System.Windows.Forms.KeyEventArgs)Handles cmb.KeyDown

Try

If e.KeyCode = Keys.EnterThen

PopulateListView(cmb.Text)

EndIf

Catch exAs Exception

EndTry

EndSub

PrivateSub cmb_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles cmb.SelectedIndexChanged

PopulateListView(cmb.Text)

EndSub

PrivateSub listview2_DoubleClick(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles listview2.DoubleClick

Dim itemsAsString

Dim CountAs Int32 = 0

'ListView2.Items.Clear()

Dim listFolderItemsAs ListView.SelectedListViewItemCollection =Me.listview2.SelectedItems

Dim itemAs ListViewItem

ForEach itemIn listFolderItems

cmb.Text = item.SubItems(0).Text

cmb_SelectedIndexChanged(sender, e)

Next

EndSub

EndClass

[18426 byte] By [aajrb] at [2007-12-28]