Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:01:18+00:00 2026-05-13T09:01:18+00:00

I need a working example of the ExtractAssociatedIcon function in Shell32.dll. I cannot get

  • 0

I need a working example of the ExtractAssociatedIcon function in Shell32.dll. I cannot get it working and I am out of ideas. I need another set of eyes on the following code. When the form loads, its icon should be set to the Visual Studio icon, but all I get is the default system icon.

Imports System.Runtime.InteropServices

Public Class Form1

    Public Function ExtractIcon(ByVal path As String, ByVal handle As IntPtr) As Icon
        Dim oResult As Icon
        Dim hIcon As IntPtr
        Dim iIndex As Integer
        Dim oPath As New System.Text.StringBuilder(260, 260)

        oPath.Append(path)

        hIcon = ExtractAssociatedIcon(handle, oPath, iIndex)

        'hIcon = ExtractAssociatedIcon(handle, path, iIndex)

        Dim oIcon As Icon = Icon.FromHandle(hIcon)

        oResult = DirectCast(oIcon.Clone, Icon)

        DestroyIcon(hIcon)

        Return oResult
    End Function

    Public Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
        ByVal hInst As IntPtr, _
        <MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As System.Text.StringBuilder, _
        ByRef lpiIcon As Integer) As IntPtr

    'Public Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
    '    ByVal hInst As IntPtr, _
    '    <MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As String, _
    '    ByRef lpiIcon As Integer) As IntPtr

    Friend Declare Auto Function DestroyIcon Lib "user32" (<[In]()> ByVal hIcon As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean

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

        'ExtractAssociatedIcon uses ExtractAssociatedIcon that is in Shell32.dll.
        'This works, so why doesn't mine? What am I missing?
        'Me.Icon = System.Drawing.Icon.ExtractAssociatedIcon("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe")

        Me.Icon = ExtractIcon("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe", Me.Handle)

    End Sub

End Class

Corrected

<MarshalAs(UnmanagedType.LPStr)> was the problem. It should have been LPTStr, not LPStr.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:01:19+00:00Added an answer on May 13, 2026 at 9:01 am

    Passing in the handle is definitely wrong, the 1st argument to EAI is a module handle, not an icon handle. This worked well:

    Imports System.Runtime.InteropServices
    Imports System.ComponentModel
    ...
      Public Shared Function ExtractIcon(ByVal path As String, Optional ByVal index As Integer = 0) As Icon
        Dim handle As IntPtr = ExtractAssociatedIcon(IntPtr.Zero, path, index)
        If handle = IntPtr.Zero Then Throw New Win32Exception(Marshal.GetLastWin32Error())
        Dim retval As Icon = Nothing
        using temp As Icon = Icon.FromHandle(handle)
          retval = CType(temp.Clone(), Icon)
          DestroyIcon(handle)
        end using
        Return retval      
      End Function
    
      Private Declare Auto Function ExtractAssociatedIcon Lib "shell32" ( _
          ByVal hInst As IntPtr, ByVal path As String, ByRef index As Integer) As IntPtr
      Private Declare Auto Function DestroyIcon Lib "user32" (ByVal hIcon As IntPtr) As Boolean
    

    No need for StringBuilder if the module handle is null. Don’t use this code if you don’t the “index” argument. Icon.ExtractAssocationIcon will work just as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 307k
  • Answers 307k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer IntelliJ IDEA offers you special tool for generifying legacy collections. May 13, 2026 at 9:25 pm
  • Editorial Team
    Editorial Team added an answer For a simple request, you could use stringWithContentsOfURL NSURL *url… May 13, 2026 at 9:25 pm
  • Editorial Team
    Editorial Team added an answer I ended up fixing this by placing a below the… May 13, 2026 at 9:25 pm

Related Questions

Can somebody provide a working example of JavaScriptResult in asp.net mvc. I understand that
I'm having a ridiculous time trying to get an SMS API working ( ZeepMobile
Hey all need a lil help sorting a loop for this table out, cant
I am in the design phase of writing a new Windows service application that
I have been all over the place, seems the UITableView with a static background

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.