Win32 API 日本語リファレンス
ホームUI.Shell › ExtractIconW

ExtractIconW

関数
実行ファイルから指定インデックスのアイコンを取り出す。
DLLSHELL32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHELL32.dll  (Unicode / -W)
#include <windows.h>

HICON ExtractIconW(
    HINSTANCE hInst,   // optional
    LPCWSTR pszExeFileName,
    DWORD nIconIndex
);

パラメーター

名前方向説明
hInstHINSTANCEoptional関数を呼び出すアプリケーションのインスタンスへのハンドルです。
pszExeFileNameLPCWSTRin実行可能ファイル、DLL、またはアイコンファイルの名前を指定する、null で終わる文字列へのポインターです。
nIconIndexDWORDin

取得するアイコンの 0 から始まるインデックスを指定します。たとえば、この値が 0 の場合、関数は指定したファイル内の最初のアイコンへのハンドルを返します。

この値が -1 の場合、関数は指定したファイル内のアイコンの総数を返します。ファイルが実行可能ファイルまたは DLL の場合、戻り値は RT_GROUP_ICON リソースの数です。ファイルが .ICO ファイルの場合、戻り値は 1 です。

この値が -1 以外の負の数の場合、関数は指定したファイル内で、リソース識別子が nIconIndex の絶対値に等しいアイコンへのハンドルを返します。たとえば、リソース識別子が 3 のアイコンを抽出するには -3 を使用します。リソース識別子が 1 のアイコンを抽出するには、ExtractIconEx 関数を使用してください。

戻り値の型: HICON

公式ドキュメント

指定した実行可能ファイル、DLL、またはアイコンファイルからアイコンのハンドルを取得します。大きいアイコンまたは小さいアイコンのハンドルの配列を取得するには、ExtractIconEx 関数を使用してください。(Unicode)

戻り値

型: HICON

戻り値はアイコンへのハンドルです。指定したファイルが実行可能ファイル、DLL、またはアイコンファイルでなかった場合、戻り値は 1 です。ファイル内にアイコンが見つからなかった場合、戻り値は NULL です。

解説(Remarks)

ExtractIcon が返したアイコンハンドルが不要になったら、DestroyIcon 関数を呼び出して破棄する必要があります。

メモ

shellapi.h ヘッダーは ExtractIcon を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// SHELL32.dll  (Unicode / -W)
#include <windows.h>

HICON ExtractIconW(
    HINSTANCE hInst,   // optional
    LPCWSTR pszExeFileName,
    DWORD nIconIndex
);
[DllImport("SHELL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr ExtractIconW(
    IntPtr hInst,   // HINSTANCE optional
    [MarshalAs(UnmanagedType.LPWStr)] string pszExeFileName,   // LPCWSTR
    uint nIconIndex   // DWORD
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function ExtractIconW(
    hInst As IntPtr,   ' HINSTANCE optional
    <MarshalAs(UnmanagedType.LPWStr)> pszExeFileName As String,   ' LPCWSTR
    nIconIndex As UInteger   ' DWORD
) As IntPtr
End Function
' hInst : HINSTANCE optional
' pszExeFileName : LPCWSTR
' nIconIndex : DWORD
Declare PtrSafe Function ExtractIconW Lib "shell32" ( _
    ByVal hInst As LongPtr, _
    ByVal pszExeFileName As LongPtr, _
    ByVal nIconIndex As Long) As LongPtr
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ExtractIconW = ctypes.windll.shell32.ExtractIconW
ExtractIconW.restype = ctypes.c_void_p
ExtractIconW.argtypes = [
    wintypes.HANDLE,  # hInst : HINSTANCE optional
    wintypes.LPCWSTR,  # pszExeFileName : LPCWSTR
    wintypes.DWORD,  # nIconIndex : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
ExtractIconW = Fiddle::Function.new(
  lib['ExtractIconW'],
  [
    Fiddle::TYPE_VOIDP,  # hInst : HINSTANCE optional
    Fiddle::TYPE_VOIDP,  # pszExeFileName : LPCWSTR
    -Fiddle::TYPE_INT,  # nIconIndex : DWORD
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "shell32")]
extern "system" {
    fn ExtractIconW(
        hInst: *mut core::ffi::c_void,  // HINSTANCE optional
        pszExeFileName: *const u16,  // LPCWSTR
        nIconIndex: u32  // DWORD
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHELL32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ExtractIconW(IntPtr hInst, [MarshalAs(UnmanagedType.LPWStr)] string pszExeFileName, uint nIconIndex);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_ExtractIconW' -Namespace Win32 -PassThru
# $api::ExtractIconW(hInst, pszExeFileName, nIconIndex)
#uselib "SHELL32.dll"
#func global ExtractIconW "ExtractIconW" wptr, wptr, wptr
; ExtractIconW hInst, pszExeFileName, nIconIndex   ; 戻り値は stat
; hInst : HINSTANCE optional -> "wptr"
; pszExeFileName : LPCWSTR -> "wptr"
; nIconIndex : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHELL32.dll"
#cfunc global ExtractIconW "ExtractIconW" sptr, wstr, int
; res = ExtractIconW(hInst, pszExeFileName, nIconIndex)
; hInst : HINSTANCE optional -> "sptr"
; pszExeFileName : LPCWSTR -> "wstr"
; nIconIndex : DWORD -> "int"
; HICON ExtractIconW(HINSTANCE hInst, LPCWSTR pszExeFileName, DWORD nIconIndex)
#uselib "SHELL32.dll"
#cfunc global ExtractIconW "ExtractIconW" intptr, wstr, int
; res = ExtractIconW(hInst, pszExeFileName, nIconIndex)
; hInst : HINSTANCE optional -> "intptr"
; pszExeFileName : LPCWSTR -> "wstr"
; nIconIndex : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procExtractIconW = shell32.NewProc("ExtractIconW")
)

// hInst (HINSTANCE optional), pszExeFileName (LPCWSTR), nIconIndex (DWORD)
r1, _, err := procExtractIconW.Call(
	uintptr(hInst),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszExeFileName))),
	uintptr(nIconIndex),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HICON
function ExtractIconW(
  hInst: THandle;   // HINSTANCE optional
  pszExeFileName: PWideChar;   // LPCWSTR
  nIconIndex: DWORD   // DWORD
): THandle; stdcall;
  external 'SHELL32.dll' name 'ExtractIconW';
result := DllCall("SHELL32\ExtractIconW"
    , "Ptr", hInst   ; HINSTANCE optional
    , "WStr", pszExeFileName   ; LPCWSTR
    , "UInt", nIconIndex   ; DWORD
    , "Ptr")   ; return: HICON
●ExtractIconW(hInst, pszExeFileName, nIconIndex) = DLL("SHELL32.dll", "void* ExtractIconW(void*, char*, dword)")
# 呼び出し: ExtractIconW(hInst, pszExeFileName, nIconIndex)
# hInst : HINSTANCE optional -> "void*"
# pszExeFileName : LPCWSTR -> "char*"
# nIconIndex : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "shell32" fn ExtractIconW(
    hInst: ?*anyopaque, // HINSTANCE optional
    pszExeFileName: [*c]const u16, // LPCWSTR
    nIconIndex: u32 // DWORD
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc ExtractIconW(
    hInst: pointer,  # HINSTANCE optional
    pszExeFileName: WideCString,  # LPCWSTR
    nIconIndex: uint32  # DWORD
): pointer {.importc: "ExtractIconW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shell32");
extern(Windows)
void* ExtractIconW(
    void* hInst,   // HINSTANCE optional
    const(wchar)* pszExeFileName,   // LPCWSTR
    uint nIconIndex   // DWORD
);
ccall((:ExtractIconW, "SHELL32.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Cwstring, UInt32),
      hInst, pszExeFileName, nIconIndex)
# hInst : HINSTANCE optional -> Ptr{Cvoid}
# pszExeFileName : LPCWSTR -> Cwstring
# nIconIndex : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
void* ExtractIconW(
    void* hInst,
    const uint16_t* pszExeFileName,
    uint32_t nIconIndex);
]]
local shell32 = ffi.load("shell32")
-- shell32.ExtractIconW(hInst, pszExeFileName, nIconIndex)
-- hInst : HINSTANCE optional
-- pszExeFileName : LPCWSTR
-- nIconIndex : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const ExtractIconW = lib.func('__stdcall', 'ExtractIconW', 'void *', ['void *', 'str16', 'uint32_t']);
// ExtractIconW(hInst, pszExeFileName, nIconIndex)
// hInst : HINSTANCE optional -> 'void *'
// pszExeFileName : LPCWSTR -> 'str16'
// nIconIndex : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  ExtractIconW: { parameters: ["pointer", "buffer", "u32"], result: "pointer" },
});
// lib.symbols.ExtractIconW(hInst, pszExeFileName, nIconIndex)
// hInst : HINSTANCE optional -> "pointer"
// pszExeFileName : LPCWSTR -> "buffer"
// nIconIndex : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* ExtractIconW(
    void* hInst,
    const uint16_t* pszExeFileName,
    uint32_t nIconIndex);
C, "SHELL32.dll");
// $ffi->ExtractIconW(hInst, pszExeFileName, nIconIndex);
// hInst : HINSTANCE optional
// pszExeFileName : LPCWSTR
// nIconIndex : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Shell32 extends StdCallLibrary {
    Shell32 INSTANCE = Native.load("shell32", Shell32.class, W32APIOptions.UNICODE_OPTIONS);
    Pointer ExtractIconW(
        Pointer hInst,   // HINSTANCE optional
        WString pszExeFileName,   // LPCWSTR
        int nIconIndex   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shell32")]
lib LibSHELL32
  fun ExtractIconW = ExtractIconW(
    hInst : Void*,   # HINSTANCE optional
    pszExeFileName : UInt16*,   # LPCWSTR
    nIconIndex : UInt32   # DWORD
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef ExtractIconWNative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Uint32);
typedef ExtractIconWDart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, int);
final ExtractIconW = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<ExtractIconWNative, ExtractIconWDart>('ExtractIconW');
// hInst : HINSTANCE optional -> Pointer<Void>
// pszExeFileName : LPCWSTR -> Pointer<Utf16>
// nIconIndex : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function ExtractIconW(
  hInst: THandle;   // HINSTANCE optional
  pszExeFileName: PWideChar;   // LPCWSTR
  nIconIndex: DWORD   // DWORD
): THandle; stdcall;
  external 'SHELL32.dll' name 'ExtractIconW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "ExtractIconW"
  c_ExtractIconW :: Ptr () -> CWString -> Word32 -> IO (Ptr ())
-- hInst : HINSTANCE optional -> Ptr ()
-- pszExeFileName : LPCWSTR -> CWString
-- nIconIndex : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let extracticonw =
  foreign "ExtractIconW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> returning (ptr void))
(* hInst : HINSTANCE optional -> (ptr void) *)
(* pszExeFileName : LPCWSTR -> (ptr uint16_t) *)
(* nIconIndex : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("ExtractIconW" extract-icon-w :convention :stdcall) :pointer
  (h-inst :pointer)   ; HINSTANCE optional
  (psz-exe-file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (n-icon-index :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ExtractIconW = Win32::API::More->new('SHELL32',
    'HANDLE ExtractIconW(HANDLE hInst, LPCWSTR pszExeFileName, DWORD nIconIndex)');
# my $ret = $ExtractIconW->Call($hInst, $pszExeFileName, $nIconIndex);
# hInst : HINSTANCE optional -> HANDLE
# pszExeFileName : LPCWSTR -> LPCWSTR
# nIconIndex : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
公式の関連項目