メインコンテンツへスキップ
見出し画像

PowerShell はじめました #80 Win32APIでDLLからのアイコン取得

    ども。

    Windowsが標準で抱えてるアイコンいっぱい入ってるDLLファイルあるじゃないですか。

    あれからアイコンひっぱってきたいなー。
    っておもったんです。

    チャットAI Copilot なこぱ先生にお願いしたら、めずらしく一発で動いたんでそのまま貼ってしまいます。


    この記事で使用しているPowerShellたちのバージョンは
    ・Windows PowerShell 5.1
    ・.NET Framework 4.8
    です。



    こぱ先生にもらったコード

    やっていること

    つかってるAPIはアイコンを引っこ抜くための ExtractIconEx です。
    あとは取得したあとのお片付け用に DestroyIcon です。

    Add-TypeでのWin32API準備 については参照してもらえるといいなぁとおもいます。

    まとめて引っこ抜いて一覧表示ってのやりたかったので、
    フォームにListViewコントロール置いて、そこにひたすら突っ込んでます。

    アイコン表示なのでListViewのViewプロパティには LargeIconをセット。

    参照DLLは
    $dll = "$env:windir\System32\imageres.dll"
    です。

    なんとなくアイコン集といえばshell32.dllなイメージでしたが、今はちがうんですかね?

    #$dll = "$env:windir\System32\imageres.dll"
    $dll = "$env:windir\System32\shell32.dll"

    shell32.dllからもちゃんと取れているんで、お好みで?

    どちらも、そんなにきれいでもなくモダンな感じはないですが。

    # DLLアイコン
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    
    public static class NativeMethods
    {
        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        public static extern uint ExtractIconEx(
            string szFileName,
            int nIconIndex,
            IntPtr[] phiconLarge,
            IntPtr[] phiconSmall,
            uint nIcons);
    
        [DllImport("user32.dll")] public static extern bool DestroyIcon(IntPtr hIcon);
    }
    "@
    
    $form = New-Object Windows.Forms.Form -Property @{
        Text = 'imageres.dll Icons'
        Width = 1000
        Height = 700
    }
    
    $listView = New-Object Windows.Forms.ListView -Property @{
        Dock = 'Fill'
        View = 'LargeIcon'
    }
    
    $imageList = New-Object Windows.Forms.ImageList
    $imageList.ImageSize = [Drawing.Size]::new(32,32)
    $listView.LargeImageList = $imageList
    
    $form.Controls.Add($listView)
    
    $dll = "$env:windir\System32\imageres.dll"
    
    for($i = 0; $i -lt 1000; $i++)
    {
        $large = [IntPtr[]]::new(1)
    
        $count = [NativeMethods]::ExtractIconEx(
            $dll,
            $i,
            $large,
            $null,
            1
        )
    
        if($count -gt 0 -and $large[0] -ne [IntPtr]::Zero)
        {
            try
            {
                $icon = [Drawing.Icon]::FromHandle($large[0])
    
                $imageList.Images.Add($icon)
    
                $item = New-Object Windows.Forms.ListViewItem
                $item.Text = $i
                $item.ImageIndex = $imageList.Images.Count - 1
    
                $listView.Items.Add($item) | Out-Null
            }
            finally
            {
                [NativeMethods]::DestroyIcon($large[0]) | Out-Null
            }
        }
    }
    
    [void]$form.ShowDialog()

    個別にひっこぬく

    一覧で表示できたら、あとは個別に引っこ抜いて使うわけですよ。
    今回PictureBoxに突っ込みたかったので、 ToBitmap()入れてます。

    アイコンとしての取得はここですね。
    [Drawing.Icon]::FromHandle($large[0])

    $dll = "$env:windir\System32\imageres.dll"
    $large = [IntPtr[]]::new(1)
    $count = [Win32]::ExtractIconEx($dll, 64, $large, $null, 1)
    if($count -gt 0 -and $large[0] -ne [IntPtr]::Zero)
    {
        try
        {
            $gear = ([Drawing.Icon]::FromHandle($large[0])).ToBitmap()
        } finally
        {
            [Win32]::DestroyIcon($large[0]) | Out-Null
        }
    }
    

    では!


    はじめました まとめました


     
     
    こんにちは。 大した知識があるわけではありませんが、物や物事の仕組みを考える時間が好きです。 最近はPowerShellが楽しくなってきています。 (サムネ画像はAIで生成しています)

    あなたへのおすすめ