Multimobile Development: Building Applications for any Smartphone
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...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
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...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
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.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734