473,382 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Convert windows SHFILEINFO to DotNet

Need this:

typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO

This is what I tried:
Public Structure SHFILEINFO

Public hIcon As IntPtr

Public iIcon As Integer

Public dwAttributes As Integer

<MarshalAs(UnmanagedType.LPStr, SizeConst:=Kernel.MAX_PATH)> Public
szDisplayName As String

<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As String

End Structure

Kernel.MAX_PATH equals 260

But it doesn't appear to work. Does it look OK to you or do you know how to
fix it.

Thanks


Nov 21 '05 #1
11 3434
" **Developer**" <RE*************@a-znet.com> wrote in message
news:u%****************@TK2MSFTNGP14.phx.gbl...
Need this:

typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO

This is what I tried:
Public Structure SHFILEINFO

Public hIcon As IntPtr

Public iIcon As Integer

Public dwAttributes As Integer

<MarshalAs(UnmanagedType.LPStr, SizeConst:=Kernel.MAX_PATH)> Public
szDisplayName As String

<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As
String

End Structure


Google is your friend!
http://www.google.com/search?biw=951...=Google+Search

From
http://www.codeguru.com/vb/gen/vb_mi...cle.php/c5597/

The difference is in the UnmangedType for szDisplayName

Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Nov 21 '05 #2
Hi,

Here is a quick example. Add a listview control to the form and
name it lv. Add an imagelist to form name it imlIcon.

Private Function getLocalIcons(ByVal szFolderPath As String)

Dim dirInfo As New System.IO.DirectoryInfo(szFolderPath)

Dim di As System.IO.DirectoryInfo

Dim fi As System.IO.FileInfo

Dim lvitem As ListViewItem

Dim hIcon As Icon

Dim cIcon As New clsGetIcon

Dim htIcons As New Hashtable

Dim intIndex As Integer

imlIcon.Images.Clear()

lv.Items.Clear()

lv.SmallImageList = imlIcon

For Each di In dirInfo.GetDirectories()

lvitem = lv.Items.Add(di.Name)

hIcon = cIcon.getIcon(di.FullName)

If htIcons.ContainsKey(hIcon) Then

intIndex = htIcons(hIcon)

Else

imlIcon.Images.Add(hIcon.ToBitmap)

intIndex = imlIcon.Images.Count - 1

End If

lvitem.ImageIndex = intIndex

Next

For Each fi In dirInfo.GetFiles()

lvitem = lv.Items.Add(fi.Name)

hIcon = cIcon.getIcon(fi.FullName)

If htIcons.ContainsKey(hIcon) Then

intIndex = htIcons(hIcon)

Else

imlIcon.Images.Add(hIcon.ToBitmap)

intIndex = imlIcon.Images.Count - 1

End If

lvitem.ImageIndex = intIndex

Next

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lv.SuspendLayout()

getLocalIcons("C:\")

lv.ResumeLayout()

End Sub

The helper class.

Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon
representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON As Integer = &H100

Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon

Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

aSHFileInfo.szTypeName = Space(255)

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

Trace.WriteLine(aSHFileInfo.szTypeName)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

---------------------------

" **Developer**" <RE*************@a-znet.com> wrote in message
news:u%****************@TK2MSFTNGP14.phx.gbl...
Need this:

typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO

This is what I tried:
Public Structure SHFILEINFO

Public hIcon As IntPtr

Public iIcon As Integer

Public dwAttributes As Integer

<MarshalAs(UnmanagedType.LPStr, SizeConst:=Kernel.MAX_PATH)> Public
szDisplayName As String

<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As String

End Structure

Kernel.MAX_PATH equals 260

But it doesn't appear to work. Does it look OK to you or do you know how to
fix it.

Thanks

Nov 21 '05 #3
" **Developer**" <RE*************@a-znet.com> schrieb:
typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO
[...]
<MarshalAs(UnmanagedType.LPStr, SizeConst:=Kernel.MAX_PATH)> Public
szDisplayName As String

<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As
String

End Structure


The strings must be stored as 'ByValTStr's, which means that they are stored
inside the structure.

<URL:http://groups.google.to/groups?selm=u%23IVf5baFHA.1040%40TK2MSFTNGP10.phx. gbl>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
" **Developer**" <RE*************@a-znet.com> wrote in message
news:u%****************@TK2MSFTNGP14.phx.gbl... snip
The difference is in the UnmangedType for szDisplayName

Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure

Jim

I did see this in the help but was (and am still) confused about the ByVal
in ByValStr. This struc is used to obtain strings so I'd expect ByRef

As you have it can the API call return strings?

Do I need to somehow define some place to put the string or does this
actually set aside memory for the returned string?

I did try this and it didn't fix my problem but that's most likely because I
have an additional problem in the code.

Thanks
Nov 21 '05 #5
Is something missing for the following.
myIcon = Icon.FromHandle(aSHFileInfo.hIcon)


I tried your structure in my code and that did not fix my problem so I'd
like to get your example running exactly as you have it.

Thanks
Nov 21 '05 #6
For that statement I get

Reference to a non-shared member requires an object reference.

Thanks

" **Developer**" <RE*************@a-znet.com> wrote in message
news:eu**************@TK2MSFTNGP15.phx.gbl...
Is something missing for the following.
myIcon = Icon.FromHandle(aSHFileInfo.hIcon)


I tried your structure in my code and that did not fix my problem so I'd
like to get your example running exactly as you have it.

Thanks

Nov 21 '05 #7

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
" **Developer**" <RE*************@a-znet.com> schrieb:
typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO
[...]
<MarshalAs(UnmanagedType.LPStr, SizeConst:=Kernel.MAX_PATH)> Public
szDisplayName As String

<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As
String

End Structure
The strings must be stored as 'ByValTStr's, which means that they are
stored inside the structure.

With ByValStr is memory allocated for the API call and the result put into
the string without me doing anything else?
Why is it called ByVal since I expect a returned string. Why doesn't
ByRefTStr make more sense?

Thanks for the input


<URL:http://groups.google.to/groups?selm=u%23IVf5baFHA.1040%40TK2MSFTNGP10.phx. gbl>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
FYI

I simply changed Icon to MyIcon

" **Developer**" <RE*************@a-znet.com> wrote in message
news:eD*************@TK2MSFTNGP14.phx.gbl...
For that statement I get

Reference to a non-shared member requires an object reference.

Thanks

" **Developer**" <RE*************@a-znet.com> wrote in message
news:eu**************@TK2MSFTNGP15.phx.gbl...
Is something missing for the following.
myIcon = Icon.FromHandle(aSHFileInfo.hIcon)


I tried your structure in my code and that did not fix my problem so I'd
like to get your example running exactly as you have it.

Thanks


Nov 21 '05 #9
" **Developer**" <RE*************@a-znet.com> schrieb:
Why is it called ByVal since I expect a returned string. Why doesn't
ByRefTStr make more sense?


In this particular case the buffer for the string (value) is stored inline
in the structure, and not just a pointer to a string (reference) is stored
inside the structure.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10


I simply changed Icon to MyIcon


but aSHFileInfo.szTypeName always returns spaces

so I tried the struct suggested by Amin and Herfried but I still get only
spaces
Is something missing for the following.

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Nov 21 '05 #11
Thanks again

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
" **Developer**" <RE*************@a-znet.com> schrieb:
Why is it called ByVal since I expect a returned string. Why doesn't
ByRefTStr make more sense?


In this particular case the buffer for the string (value) is stored inline
in the structure, and not just a pointer to a string (reference) is stored
inside the structure.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Altramagnus | last post by:
I have a complicated Region object, which I need to draw the outline, but not fill How can I convert the Region object to GraphicsPath object? or How can I draw the outline of the Region object?
6
by: Marty | last post by:
Hi, If I don't know that my (or any) windows 2k regional setting for the decimal symbol is the dot (".") and I want to do this: 1- Dim dblOutput As Double 2- Dim strInput As...
14
by: David C. Taylor | last post by:
I saw an article that described how to convert the query string into an Url without a query string and return the page. For example, this request: ...
1
by: DotNET74 | last post by:
Hi, I want to get the same information that Windows Explorer give when we clic on Properties for a file. Example: When i clic on JPEG file and i choose "Properties", i have "Picture JPEG" ...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
7
by: Tom wilson | last post by:
How does one convert "LightGray" (ie.) to an HTML color? Thanks
0
by: **Developer** | last post by:
Need this: typedef struct _SHFILEINFO { HICON hIcon; int iIcon; DWORD dwAttributes; TCHAR szDisplayName; TCHAR szTypeName; } SHFILEINFO
5
by: moondaddy | last post by:
I have a website that currently has all static htm pages and nothing will be dynamic for quite some time. This site is made up of a bunch of htm pages. is there any advantage or disadvantage of...
10
by: Andrew Backer | last post by:
I have a few file:///c:/windows/somewhere style uris that I need to convert to the actual physical path. I am hoping there is a built in way to handle this. I know I can do some text replacing...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.