Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
ExtractIcon followup...
Messages   Related Types
This message was discovered on microsoft.public.dotnet.general.


Steve Randall
Tom,

Using your supplied code fragment I get the error message
"Error: PInvoke item (field,method) must be Static.". I
notice that you define hInst and the return values as type
IntPtr instead of long (if this makes any difference?) I
admit that when it comes to playing with system/API calls
I'm not that familiar. The code fragment I'm using is
below:

<DllImport("shell32", CharSet:=CharSet.Auto,
SetLastError:=True)> _
Public Function ExtractIcon( _
ByVal hInst As IntPtr, _
ByVal lpszExeFileName, _
ByVal nIconIndex As Integer) As IntPtr
End Function

Private Declare Function DrawIcon Lib "user32" (ByVal hdc
As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As
Long) As Long

Dim hIcon As IntPtr = ExtractIcon(Process.GetCurrentProcess
().Handle, "C:\WINNT\Installer\{90280409-6000-11D3-8CFE-
0050048383C9}\wordicon.exe", 1)

z = DrawIcon(Picture1.Handle.ToInt32, 0, 0, hIcon.ToInt32)

Many thanks.
Steve
Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Tom Shelton
"Steve Randall" <Click here to reveal e-mail address> wrote in message
news:191e01c27383$070da540$Click here to reveal e-mail address...
[Original message clipped]

Handles, such as hWnds, hIcons, etc should always be declared as IntPtr. It
makes the code more portable to say 64-bit when the time comes, since IntPtr
does not have a fixed size - it changes with the OS.

Any way, I see two problems with your declare... The first is that you
(maybe I) forgot the As String on you lpszExeFileName parameter. The
second, is that if you made this a member of a class then you need to add
Shared to the declaration.

Your DrawIcon declare should be:

Private Declare Function DrawIcon Lib "user32" (ByVal hdc As IntPtr, ByVal x
As Integer, ByVal y As Integer, ByVal hIcon As Integer) As Integer

Remember, that Long in VB.NET is a 64-bit integer, not 32 as in VB.CLASSIC.
Further more, you don't really need to use DrawIcon to draw the Icon into
the PictureBox. Here is some code that will do this:

' Extract Icon - make it a shared member of Form1
<DllImport("shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function ExtractIcon( _
ByVal hInst As IntPtr, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Integer) As IntPtr
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim hIcon As IntPtr =
Form1.ExtractIcon(Process.GetCurrentProcess.Handle, "calc.exe", 0)
Dim ic As Icon = Icon.FromHandle(hIcon)
Dim g As Graphics = PictureBox1.CreateGraphics()

g.DrawIcon(ic, 0, 0)
g.Dispose()
Icon.Dispose()
End Sub

HTH,
Tom Shelton

Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
Tom Shelton
[Original message clipped]

Should Be: ByVal hIcon As IntPtr

Tom Shelton

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Diagnostics.Process
System.Drawing.Icon
System.EventArgs
System.IntPtr
System.Object
System.Runtime.InteropServices.CharSet
System.Windows.Forms.PictureBox




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734