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

ExtractAssociatedIconW

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

シグネチャ

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

HICON ExtractAssociatedIconW(
    HINSTANCE hInst,   // optional
    LPWSTR pszIconPath,
    WORD* piIcon
);

パラメーター

名前方向説明
hInstHINSTANCEoptional呼び出し元アプリケーションのインスタンスへのハンドルです。
pszIconPathLPWSTRinout

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

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

piIconWORD*inout

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

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

戻り値の型: HICON

公式ドキュメント

ファイル内にリソースとして格納されたアイコン、またはファイルに関連付けられた実行可能ファイルに格納されたアイコンのハンドルを取得します。(Unicode)

戻り値

型: HICON

関数が成功した場合、戻り値はアイコンハンドルです。アイコンが関連付けられた実行可能ファイルから抽出された場合、関数はその実行可能ファイルのフルパスとファイル名を lpIconPath が指す文字列に格納し、アイコンの識別子を lpiIcon が指す WORD に格納します。

関数が失敗した場合、戻り値は NULL です。

解説(Remarks)

ExtractAssociatedIcon が返すアイコンハンドルが不要になったら、呼び出し元は DestroyIcon 関数を呼び出してそのハンドルを解放する責任があります。

ExtractAssociatedIcon 関数は、まず lpIconPath で指定されたファイル内で、インデックスで指定されたアイコンを探します。そのファイルからアイコンハンドルを取得できず、かつそのファイルに関連付けられた実行可能ファイルがある場合、その実行可能ファイル内でアイコンを探します。実行可能ファイルとの関連付けはファイル名拡張子に基づいており、レジストリのユーザーごとの部分に格納されています。

メモ

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

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

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

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

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procExtractAssociatedIconW = shell32.NewProc("ExtractAssociatedIconW")
)

// hInst (HINSTANCE optional), pszIconPath (LPWSTR in/out), piIcon (WORD* in/out)
r1, _, err := procExtractAssociatedIconW.Call(
	uintptr(hInst),
	uintptr(pszIconPath),
	uintptr(piIcon),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HICON
function ExtractAssociatedIconW(
  hInst: THandle;   // HINSTANCE optional
  pszIconPath: PWideChar;   // LPWSTR in/out
  piIcon: Pointer   // WORD* in/out
): THandle; stdcall;
  external 'SHELL32.dll' name 'ExtractAssociatedIconW';
result := DllCall("SHELL32\ExtractAssociatedIconW"
    , "Ptr", hInst   ; HINSTANCE optional
    , "Ptr", pszIconPath   ; LPWSTR in/out
    , "Ptr", piIcon   ; WORD* in/out
    , "Ptr")   ; return: HICON
●ExtractAssociatedIconW(hInst, pszIconPath, piIcon) = DLL("SHELL32.dll", "void* ExtractAssociatedIconW(void*, char*, void*)")
# 呼び出し: ExtractAssociatedIconW(hInst, pszIconPath, piIcon)
# hInst : HINSTANCE optional -> "void*"
# pszIconPath : LPWSTR in/out -> "char*"
# piIcon : 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 ExtractAssociatedIconW(
    hInst: ?*anyopaque, // HINSTANCE optional
    pszIconPath: [*c]u16, // LPWSTR in/out
    piIcon: [*c]u16 // WORD* in/out
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc ExtractAssociatedIconW(
    hInst: pointer,  # HINSTANCE optional
    pszIconPath: ptr uint16,  # LPWSTR in/out
    piIcon: ptr uint16  # WORD* in/out
): pointer {.importc: "ExtractAssociatedIconW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shell32");
extern(Windows)
void* ExtractAssociatedIconW(
    void* hInst,   // HINSTANCE optional
    wchar* pszIconPath,   // LPWSTR in/out
    ushort* piIcon   // WORD* in/out
);
ccall((:ExtractAssociatedIconW, "SHELL32.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Ptr{UInt16}, Ptr{UInt16}),
      hInst, pszIconPath, piIcon)
# hInst : HINSTANCE optional -> Ptr{Cvoid}
# pszIconPath : LPWSTR in/out -> Ptr{UInt16}
# piIcon : WORD* in/out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
void* ExtractAssociatedIconW(
    void* hInst,
    uint16_t* pszIconPath,
    uint16_t* piIcon);
]]
local shell32 = ffi.load("shell32")
-- shell32.ExtractAssociatedIconW(hInst, pszIconPath, piIcon)
-- hInst : HINSTANCE optional
-- pszIconPath : LPWSTR in/out
-- piIcon : 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 ExtractAssociatedIconW = lib.func('__stdcall', 'ExtractAssociatedIconW', 'void *', ['void *', 'uint16_t *', 'uint16_t *']);
// ExtractAssociatedIconW(hInst, pszIconPath, piIcon)
// hInst : HINSTANCE optional -> 'void *'
// pszIconPath : LPWSTR in/out -> 'uint16_t *'
// piIcon : WORD* in/out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  ExtractAssociatedIconW: { parameters: ["pointer", "buffer", "pointer"], result: "pointer" },
});
// lib.symbols.ExtractAssociatedIconW(hInst, pszIconPath, piIcon)
// hInst : HINSTANCE optional -> "pointer"
// pszIconPath : LPWSTR in/out -> "buffer"
// piIcon : WORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* ExtractAssociatedIconW(
    void* hInst,
    uint16_t* pszIconPath,
    uint16_t* piIcon);
C, "SHELL32.dll");
// $ffi->ExtractAssociatedIconW(hInst, pszIconPath, piIcon);
// hInst : HINSTANCE optional
// pszIconPath : LPWSTR in/out
// piIcon : 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 ExtractAssociatedIconW(
        Pointer hInst,   // HINSTANCE optional
        char[] pszIconPath,   // LPWSTR in/out
        ShortByReference piIcon   // WORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shell32")]
lib LibSHELL32
  fun ExtractAssociatedIconW = ExtractAssociatedIconW(
    hInst : Void*,   # HINSTANCE optional
    pszIconPath : UInt16*,   # LPWSTR in/out
    piIcon : 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 ExtractAssociatedIconWNative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Uint16>);
typedef ExtractAssociatedIconWDart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Pointer<Uint16>);
final ExtractAssociatedIconW = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<ExtractAssociatedIconWNative, ExtractAssociatedIconWDart>('ExtractAssociatedIconW');
// hInst : HINSTANCE optional -> Pointer<Void>
// pszIconPath : LPWSTR in/out -> Pointer<Utf16>
// piIcon : WORD* in/out -> Pointer<Uint16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function ExtractAssociatedIconW(
  hInst: THandle;   // HINSTANCE optional
  pszIconPath: PWideChar;   // LPWSTR in/out
  piIcon: Pointer   // WORD* in/out
): THandle; stdcall;
  external 'SHELL32.dll' name 'ExtractAssociatedIconW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let extractassociatediconw =
  foreign "ExtractAssociatedIconW"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning (ptr void))
(* hInst : HINSTANCE optional -> (ptr void) *)
(* pszIconPath : LPWSTR in/out -> (ptr uint16_t) *)
(* piIcon : 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 ("ExtractAssociatedIconW" extract-associated-icon-w :convention :stdcall) :pointer
  (h-inst :pointer)   ; HINSTANCE optional
  (psz-icon-path :pointer)   ; LPWSTR in/out
  (pi-icon :pointer))   ; WORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ExtractAssociatedIconW = Win32::API::More->new('SHELL32',
    'HANDLE ExtractAssociatedIconW(HANDLE hInst, LPWSTR pszIconPath, LPVOID piIcon)');
# my $ret = $ExtractAssociatedIconW->Call($hInst, $pszIconPath, $piIcon);
# hInst : HINSTANCE optional -> HANDLE
# pszIconPath : LPWSTR in/out -> LPWSTR
# piIcon : WORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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