0

Because I need to extract an icon from a file, but not the first icon, I cannot use the vb.net icon extraction function. The WIN32API function that should do this expects a pointer to an integer array.

How can I provide this type as a parameter?

Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" _
(ByVal lpszFile As String, _
ByVal nIconIndex As Integer, _
ByRef phiconLarge As Integer, _
ByRef phiconSmall As Integer, _
ByVal nIcons As Long) As Integer


    Dim icons As integer()
    ExtractIconEx("%systemroot%/shell32.dll", 15, icons, 0, 5)

I've taken a gander at the System.Reflection.Pointer class?/namespace?, but the documentation is sparse and less than sensible.

IntPtr doesn't provide support for arrays afaikt

Ok tx to Hans I've managed to correct the signature to:

<Runtime.InteropServices.DllImport("shell32.dll", _
CharSet:=Runtime.InteropServicesCharSet.Auto)> _
Shared Function ExtractIconEx(ByVal szFileName As String, _
        ByVal nIconIndex As Integer, _
        ByRef phiconLarge() As IntPtr, _
        ByRef phiconSmall() As IntPtr, _
        ByVal nIcons As UInteger) As UInteger
End Function 

...

 Dim icons(8) As IntPtr, smicons(8) As IntPtr
    MsgBox(ExtractIconEx("%systemroot%/shell32.dll", 15, icons, smicons, 1))
    Try
        MsgBox(icons.Count)
    Catch ex As Exception
        MsgBox(ex.Message & " by " & ex.Source)

    End Try
...

The subsequent calls always cause an exception (Value cannot be null). I get a return value of 4294967295, which is the maximum 32 bit integer value.

Any ideas how to tame this function and make it work?

1

1 Answer 1

2
    ByRef phiconLarge() As IntPtr, _
    ByRef phiconSmall() As IntPtr, _

The VB.NET declaration on that webpage has a bug, these arrays need to be passed ByVal, not ByRef. Note how they got it correct in the example code on the bottom of the page.

I edited the page to correct the bug.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.