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

ExtractAssociatedIconExW

関数
ファイルに関連付けられたアイコンを索引付きで取得する。
DLLSHELL32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

HICON ExtractAssociatedIconExW(
    HINSTANCE hInst,   // optional
    LPWSTR pszIconPath,
    WORD* piIconIndex,
    WORD* piIconId
);

パラメーター

名前方向説明
hInstHINSTANCEoptionalアイコンを抽出する元のモジュールのハンドルです。
pszIconPathLPWSTRinout

文字列へのポインターです。入力時には、アイコンを含むファイルのフルパスとファイル名を指定します。この関数は、そのファイル、またはそのファイルに関連付けられた実行可能ファイルからアイコンハンドルを抽出します。

この関数が返るとき、アイコンハンドルが実行可能ファイル(このパラメーターで直接指定された実行可能ファイル、または関連付けられた実行可能ファイル)から取得された場合、この関数はその実行可能ファイルのフルパスとファイル名を、このパラメーターが指すバッファーに格納します。

piIconIndexWORD*inout

WORD 値へのポインターです。入力時には、ハンドルを取得するアイコンのインデックスを指定します。

この関数が返るとき、アイコンハンドルが実行可能ファイル(lpIconPath が指す実行可能ファイル、または関連付けられた実行可能ファイル)から取得された場合、この値はそのファイル内のアイコンのインデックスを指します。

piIconIdWORD*inout

WORD 値へのポインターです。入力時には、ハンドルを取得するアイコンの ID を指定します。

この関数が返るとき、アイコンハンドルが実行可能ファイル(lpIconPath が指す実行可能ファイル、または関連付けられた実行可能ファイル)から取得された場合、この値はそのファイル内のアイコンの ID を指します。

戻り値の型: HICON

公式ドキュメント

ExtractAssociatedIconEx は変更または利用不可となる場合があります。(Unicode)

戻り値

型: HICON

成功した場合はアイコンのハンドルを返します。それ以外の場合は NULL を返します。

解説(Remarks)

この関数が返すアイコンハンドルは、不要になった時点で DestroyIcon を呼び出して解放する必要があります。

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 ExtractAssociatedIconExW(
    HINSTANCE hInst,   // optional
    LPWSTR pszIconPath,
    WORD* piIconIndex,
    WORD* piIconId
);
[DllImport("SHELL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr ExtractAssociatedIconExW(
    IntPtr hInst,   // HINSTANCE optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszIconPath,   // LPWSTR in/out
    ref ushort piIconIndex,   // WORD* in/out
    ref ushort piIconId   // WORD* in/out
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function ExtractAssociatedIconExW(
    hInst As IntPtr,   ' HINSTANCE optional
    <MarshalAs(UnmanagedType.LPWStr)> pszIconPath As System.Text.StringBuilder,   ' LPWSTR in/out
    ByRef piIconIndex As UShort,   ' WORD* in/out
    ByRef piIconId As UShort   ' WORD* in/out
) As IntPtr
End Function
' hInst : HINSTANCE optional
' pszIconPath : LPWSTR in/out
' piIconIndex : WORD* in/out
' piIconId : WORD* in/out
Declare PtrSafe Function ExtractAssociatedIconExW Lib "shell32" ( _
    ByVal hInst As LongPtr, _
    ByVal pszIconPath As LongPtr, _
    ByRef piIconIndex As Integer, _
    ByRef piIconId As Integer) 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

ExtractAssociatedIconExW = ctypes.windll.shell32.ExtractAssociatedIconExW
ExtractAssociatedIconExW.restype = ctypes.c_void_p
ExtractAssociatedIconExW.argtypes = [
    wintypes.HANDLE,  # hInst : HINSTANCE optional
    wintypes.LPWSTR,  # pszIconPath : LPWSTR in/out
    ctypes.POINTER(ctypes.c_ushort),  # piIconIndex : WORD* in/out
    ctypes.POINTER(ctypes.c_ushort),  # piIconId : WORD* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
ExtractAssociatedIconExW = Fiddle::Function.new(
  lib['ExtractAssociatedIconExW'],
  [
    Fiddle::TYPE_VOIDP,  # hInst : HINSTANCE optional
    Fiddle::TYPE_VOIDP,  # pszIconPath : LPWSTR in/out
    Fiddle::TYPE_VOIDP,  # piIconIndex : WORD* in/out
    Fiddle::TYPE_VOIDP,  # piIconId : WORD* in/out
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "shell32")]
extern "system" {
    fn ExtractAssociatedIconExW(
        hInst: *mut core::ffi::c_void,  // HINSTANCE optional
        pszIconPath: *mut u16,  // LPWSTR in/out
        piIconIndex: *mut u16,  // WORD* in/out
        piIconId: *mut u16  // WORD* in/out
    ) -> *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 ExtractAssociatedIconExW(IntPtr hInst, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszIconPath, ref ushort piIconIndex, ref ushort piIconId);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_ExtractAssociatedIconExW' -Namespace Win32 -PassThru
# $api::ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId)
#uselib "SHELL32.dll"
#func global ExtractAssociatedIconExW "ExtractAssociatedIconExW" wptr, wptr, wptr, wptr
; ExtractAssociatedIconExW hInst, varptr(pszIconPath), varptr(piIconIndex), varptr(piIconId)   ; 戻り値は stat
; hInst : HINSTANCE optional -> "wptr"
; pszIconPath : LPWSTR in/out -> "wptr"
; piIconIndex : WORD* in/out -> "wptr"
; piIconId : WORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHELL32.dll"
#cfunc global ExtractAssociatedIconExW "ExtractAssociatedIconExW" sptr, var, var, var
; res = ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId)
; hInst : HINSTANCE optional -> "sptr"
; pszIconPath : LPWSTR in/out -> "var"
; piIconIndex : WORD* in/out -> "var"
; piIconId : WORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HICON ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR pszIconPath, WORD* piIconIndex, WORD* piIconId)
#uselib "SHELL32.dll"
#cfunc global ExtractAssociatedIconExW "ExtractAssociatedIconExW" intptr, var, var, var
; res = ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId)
; hInst : HINSTANCE optional -> "intptr"
; pszIconPath : LPWSTR in/out -> "var"
; piIconIndex : WORD* in/out -> "var"
; piIconId : WORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procExtractAssociatedIconExW = shell32.NewProc("ExtractAssociatedIconExW")
)

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

extern "shell32" fn ExtractAssociatedIconExW(
    hInst: ?*anyopaque, // HINSTANCE optional
    pszIconPath: [*c]u16, // LPWSTR in/out
    piIconIndex: [*c]u16, // WORD* in/out
    piIconId: [*c]u16 // WORD* in/out
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc ExtractAssociatedIconExW(
    hInst: pointer,  # HINSTANCE optional
    pszIconPath: ptr uint16,  # LPWSTR in/out
    piIconIndex: ptr uint16,  # WORD* in/out
    piIconId: ptr uint16  # WORD* in/out
): pointer {.importc: "ExtractAssociatedIconExW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shell32");
extern(Windows)
void* ExtractAssociatedIconExW(
    void* hInst,   // HINSTANCE optional
    wchar* pszIconPath,   // LPWSTR in/out
    ushort* piIconIndex,   // WORD* in/out
    ushort* piIconId   // WORD* in/out
);
ccall((:ExtractAssociatedIconExW, "SHELL32.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Ptr{UInt16}, Ptr{UInt16}, Ptr{UInt16}),
      hInst, pszIconPath, piIconIndex, piIconId)
# hInst : HINSTANCE optional -> Ptr{Cvoid}
# pszIconPath : LPWSTR in/out -> Ptr{UInt16}
# piIconIndex : WORD* in/out -> Ptr{UInt16}
# piIconId : WORD* in/out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
void* ExtractAssociatedIconExW(
    void* hInst,
    uint16_t* pszIconPath,
    uint16_t* piIconIndex,
    uint16_t* piIconId);
]]
local shell32 = ffi.load("shell32")
-- shell32.ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId)
-- hInst : HINSTANCE optional
-- pszIconPath : LPWSTR in/out
-- piIconIndex : WORD* in/out
-- piIconId : WORD* in/out
-- 構造体/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 ExtractAssociatedIconExW = lib.func('__stdcall', 'ExtractAssociatedIconExW', 'void *', ['void *', 'uint16_t *', 'uint16_t *', 'uint16_t *']);
// ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId)
// hInst : HINSTANCE optional -> 'void *'
// pszIconPath : LPWSTR in/out -> 'uint16_t *'
// piIconIndex : WORD* in/out -> 'uint16_t *'
// piIconId : WORD* in/out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  ExtractAssociatedIconExW: { parameters: ["pointer", "buffer", "pointer", "pointer"], result: "pointer" },
});
// lib.symbols.ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId)
// hInst : HINSTANCE optional -> "pointer"
// pszIconPath : LPWSTR in/out -> "buffer"
// piIconIndex : WORD* in/out -> "pointer"
// piIconId : WORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* ExtractAssociatedIconExW(
    void* hInst,
    uint16_t* pszIconPath,
    uint16_t* piIconIndex,
    uint16_t* piIconId);
C, "SHELL32.dll");
// $ffi->ExtractAssociatedIconExW(hInst, pszIconPath, piIconIndex, piIconId);
// hInst : HINSTANCE optional
// pszIconPath : LPWSTR in/out
// piIconIndex : WORD* in/out
// piIconId : WORD* in/out
// 構造体/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 ExtractAssociatedIconExW(
        Pointer hInst,   // HINSTANCE optional
        char[] pszIconPath,   // LPWSTR in/out
        ShortByReference piIconIndex,   // WORD* in/out
        ShortByReference piIconId   // WORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shell32")]
lib LibSHELL32
  fun ExtractAssociatedIconExW = ExtractAssociatedIconExW(
    hInst : Void*,   # HINSTANCE optional
    pszIconPath : UInt16*,   # LPWSTR in/out
    piIconIndex : UInt16*,   # WORD* in/out
    piIconId : UInt16*   # WORD* in/out
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef ExtractAssociatedIconExWNative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Uint16>, Pointer<Uint16>);
typedef ExtractAssociatedIconExWDart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Uint16>, Pointer<Uint16>);
final ExtractAssociatedIconExW = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<ExtractAssociatedIconExWNative, ExtractAssociatedIconExWDart>('ExtractAssociatedIconExW');
// hInst : HINSTANCE optional -> Pointer<Void>
// pszIconPath : LPWSTR in/out -> Pointer<Utf16>
// piIconIndex : WORD* in/out -> Pointer<Uint16>
// piIconId : WORD* in/out -> Pointer<Uint16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function ExtractAssociatedIconExW(
  hInst: THandle;   // HINSTANCE optional
  pszIconPath: PWideChar;   // LPWSTR in/out
  piIconIndex: Pointer;   // WORD* in/out
  piIconId: Pointer   // WORD* in/out
): THandle; stdcall;
  external 'SHELL32.dll' name 'ExtractAssociatedIconExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "ExtractAssociatedIconExW"
  c_ExtractAssociatedIconExW :: Ptr () -> CWString -> Ptr Word16 -> Ptr Word16 -> IO (Ptr ())
-- hInst : HINSTANCE optional -> Ptr ()
-- pszIconPath : LPWSTR in/out -> CWString
-- piIconIndex : WORD* in/out -> Ptr Word16
-- piIconId : WORD* in/out -> Ptr Word16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let extractassociatediconexw =
  foreign "ExtractAssociatedIconExW"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning (ptr void))
(* hInst : HINSTANCE optional -> (ptr void) *)
(* pszIconPath : LPWSTR in/out -> (ptr uint16_t) *)
(* piIconIndex : WORD* in/out -> (ptr uint16_t) *)
(* piIconId : WORD* in/out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("ExtractAssociatedIconExW" extract-associated-icon-ex-w :convention :stdcall) :pointer
  (h-inst :pointer)   ; HINSTANCE optional
  (psz-icon-path :pointer)   ; LPWSTR in/out
  (pi-icon-index :pointer)   ; WORD* in/out
  (pi-icon-id :pointer))   ; WORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ExtractAssociatedIconExW = Win32::API::More->new('SHELL32',
    'HANDLE ExtractAssociatedIconExW(HANDLE hInst, LPWSTR pszIconPath, LPVOID piIconIndex, LPVOID piIconId)');
# my $ret = $ExtractAssociatedIconExW->Call($hInst, $pszIconPath, $piIconIndex, $piIconId);
# hInst : HINSTANCE optional -> HANDLE
# pszIconPath : LPWSTR in/out -> LPWSTR
# piIconIndex : WORD* in/out -> LPVOID
# piIconId : WORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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