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

Shell_GetCachedImageIndex

関数
キャッシュ済みアイコンのシステムイメージリスト内インデックスを取得する。
DLLSHELL32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHELL32.dll  (ANSI / -A)
#include <windows.h>

INT Shell_GetCachedImageIndex(
    LPCWSTR pwszIconPath,
    INT iIconIndex,
    DWORD uIconFlags
);

パラメーター

名前方向説明
pwszIconPathLPCWSTRinイメージファイルへのパスを格納したバッファーへのポインター。
iIconIndexINTinpwszIconPath で指定したファイル内のイメージのインデックス。
uIconFlagsDWORDin使用されません。

戻り値の型: INT

公式ドキュメント

Shell_GetCachedImageIndex は変更されたり利用できなくなる場合があります。

戻り値

型: int

イメージのインデックスを返します。失敗した場合は –1 を返します。

解説(Remarks)

この関数の Shell_GetCachedImageIndexA 版および Shell_GetCachedImageIndexW 版は Windows Vista で追加されました。Unicode 文字列の場合は、Shell_GetCachedImageIndexW または Shell_GetCachedImageIndex のいずれかを呼び出します。ANSI 文字列の場合は、Shell_GetCachedImageIndexA を明示的に呼び出す必要があります。

Windows Server 2003 および Windows XP: Shell_GetCachedImageIndex のみがサポートされます。Shell_GetCachedImageIndex には Unicode 文字列が必要です。

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

各言語での呼び出し定義

// SHELL32.dll  (ANSI / -A)
#include <windows.h>

INT Shell_GetCachedImageIndex(
    LPCWSTR pwszIconPath,
    INT iIconIndex,
    DWORD uIconFlags
);
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int Shell_GetCachedImageIndex(
    [MarshalAs(UnmanagedType.LPWStr)] string pwszIconPath,   // LPCWSTR
    int iIconIndex,   // INT
    uint uIconFlags   // DWORD
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function Shell_GetCachedImageIndex(
    <MarshalAs(UnmanagedType.LPWStr)> pwszIconPath As String,   ' LPCWSTR
    iIconIndex As Integer,   ' INT
    uIconFlags As UInteger   ' DWORD
) As Integer
End Function
' pwszIconPath : LPCWSTR
' iIconIndex : INT
' uIconFlags : DWORD
Declare PtrSafe Function Shell_GetCachedImageIndex Lib "shell32" ( _
    ByVal pwszIconPath As LongPtr, _
    ByVal iIconIndex As Long, _
    ByVal uIconFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

Shell_GetCachedImageIndex = ctypes.windll.shell32.Shell_GetCachedImageIndex
Shell_GetCachedImageIndex.restype = ctypes.c_int
Shell_GetCachedImageIndex.argtypes = [
    wintypes.LPCWSTR,  # pwszIconPath : LPCWSTR
    ctypes.c_int,  # iIconIndex : INT
    wintypes.DWORD,  # uIconFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
Shell_GetCachedImageIndex = Fiddle::Function.new(
  lib['Shell_GetCachedImageIndex'],
  [
    Fiddle::TYPE_VOIDP,  # pwszIconPath : LPCWSTR
    Fiddle::TYPE_INT,  # iIconIndex : INT
    -Fiddle::TYPE_INT,  # uIconFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "shell32")]
extern "system" {
    fn Shell_GetCachedImageIndex(
        pwszIconPath: *const u16,  // LPCWSTR
        iIconIndex: i32,  // INT
        uIconFlags: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi)]
public static extern int Shell_GetCachedImageIndex([MarshalAs(UnmanagedType.LPWStr)] string pwszIconPath, int iIconIndex, uint uIconFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_Shell_GetCachedImageIndex' -Namespace Win32 -PassThru
# $api::Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
#uselib "SHELL32.dll"
#func global Shell_GetCachedImageIndex "Shell_GetCachedImageIndex" sptr, sptr, sptr
; Shell_GetCachedImageIndex pwszIconPath, iIconIndex, uIconFlags   ; 戻り値は stat
; pwszIconPath : LPCWSTR -> "sptr"
; iIconIndex : INT -> "sptr"
; uIconFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHELL32.dll"
#cfunc global Shell_GetCachedImageIndex "Shell_GetCachedImageIndex" wstr, int, int
; res = Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
; pwszIconPath : LPCWSTR -> "wstr"
; iIconIndex : INT -> "int"
; uIconFlags : DWORD -> "int"
; INT Shell_GetCachedImageIndex(LPCWSTR pwszIconPath, INT iIconIndex, DWORD uIconFlags)
#uselib "SHELL32.dll"
#cfunc global Shell_GetCachedImageIndex "Shell_GetCachedImageIndex" wstr, int, int
; res = Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
; pwszIconPath : LPCWSTR -> "wstr"
; iIconIndex : INT -> "int"
; uIconFlags : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procShell_GetCachedImageIndex = shell32.NewProc("Shell_GetCachedImageIndex")
)

// pwszIconPath (LPCWSTR), iIconIndex (INT), uIconFlags (DWORD)
r1, _, err := procShell_GetCachedImageIndex.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszIconPath))),
	uintptr(iIconIndex),
	uintptr(uIconFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function Shell_GetCachedImageIndex(
  pwszIconPath: PWideChar;   // LPCWSTR
  iIconIndex: Integer;   // INT
  uIconFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'SHELL32.dll' name 'Shell_GetCachedImageIndex';
result := DllCall("SHELL32\Shell_GetCachedImageIndex"
    , "WStr", pwszIconPath   ; LPCWSTR
    , "Int", iIconIndex   ; INT
    , "UInt", uIconFlags   ; DWORD
    , "Int")   ; return: INT
●Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags) = DLL("SHELL32.dll", "int Shell_GetCachedImageIndex(char*, int, dword)")
# 呼び出し: Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
# pwszIconPath : LPCWSTR -> "char*"
# iIconIndex : INT -> "int"
# uIconFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shell32" fn Shell_GetCachedImageIndex(
    pwszIconPath: [*c]const u16, // LPCWSTR
    iIconIndex: i32, // INT
    uIconFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc Shell_GetCachedImageIndex(
    pwszIconPath: WideCString,  # LPCWSTR
    iIconIndex: int32,  # INT
    uIconFlags: uint32  # DWORD
): int32 {.importc: "Shell_GetCachedImageIndex", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
int Shell_GetCachedImageIndex(
    const(wchar)* pwszIconPath,   // LPCWSTR
    int iIconIndex,   // INT
    uint uIconFlags   // DWORD
);
ccall((:Shell_GetCachedImageIndex, "SHELL32.dll"), stdcall, Int32,
      (Cwstring, Int32, UInt32),
      pwszIconPath, iIconIndex, uIconFlags)
# pwszIconPath : LPCWSTR -> Cwstring
# iIconIndex : INT -> Int32
# uIconFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t Shell_GetCachedImageIndex(
    const uint16_t* pwszIconPath,
    int32_t iIconIndex,
    uint32_t uIconFlags);
]]
local shell32 = ffi.load("shell32")
-- shell32.Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
-- pwszIconPath : LPCWSTR
-- iIconIndex : INT
-- uIconFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const Shell_GetCachedImageIndex = lib.func('__stdcall', 'Shell_GetCachedImageIndex', 'int32_t', ['str16', 'int32_t', 'uint32_t']);
// Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
// pwszIconPath : LPCWSTR -> 'str16'
// iIconIndex : INT -> 'int32_t'
// uIconFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  Shell_GetCachedImageIndex: { parameters: ["buffer", "i32", "u32"], result: "i32" },
});
// lib.symbols.Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags)
// pwszIconPath : LPCWSTR -> "buffer"
// iIconIndex : INT -> "i32"
// uIconFlags : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t Shell_GetCachedImageIndex(
    const uint16_t* pwszIconPath,
    int32_t iIconIndex,
    uint32_t uIconFlags);
C, "SHELL32.dll");
// $ffi->Shell_GetCachedImageIndex(pwszIconPath, iIconIndex, uIconFlags);
// pwszIconPath : LPCWSTR
// iIconIndex : INT
// uIconFlags : 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.ASCII_OPTIONS);
    int Shell_GetCachedImageIndex(
        WString pwszIconPath,   // LPCWSTR
        int iIconIndex,   // INT
        int uIconFlags   // DWORD
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun Shell_GetCachedImageIndex = Shell_GetCachedImageIndex(
    pwszIconPath : UInt16*,   # LPCWSTR
    iIconIndex : Int32,   # INT
    uIconFlags : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef Shell_GetCachedImageIndexNative = Int32 Function(Pointer<Utf16>, Int32, Uint32);
typedef Shell_GetCachedImageIndexDart = int Function(Pointer<Utf16>, int, int);
final Shell_GetCachedImageIndex = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<Shell_GetCachedImageIndexNative, Shell_GetCachedImageIndexDart>('Shell_GetCachedImageIndex');
// pwszIconPath : LPCWSTR -> Pointer<Utf16>
// iIconIndex : INT -> Int32
// uIconFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function Shell_GetCachedImageIndex(
  pwszIconPath: PWideChar;   // LPCWSTR
  iIconIndex: Integer;   // INT
  uIconFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'SHELL32.dll' name 'Shell_GetCachedImageIndex';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "Shell_GetCachedImageIndex"
  c_Shell_GetCachedImageIndex :: CWString -> Int32 -> Word32 -> IO Int32
-- pwszIconPath : LPCWSTR -> CWString
-- iIconIndex : INT -> Int32
-- uIconFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shell_getcachedimageindex =
  foreign "Shell_GetCachedImageIndex"
    ((ptr uint16_t) @-> int32_t @-> uint32_t @-> returning int32_t)
(* pwszIconPath : LPCWSTR -> (ptr uint16_t) *)
(* iIconIndex : INT -> int32_t *)
(* uIconFlags : 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 ("Shell_GetCachedImageIndex" shell-get-cached-image-index :convention :stdcall) :int32
  (pwsz-icon-path (:string :encoding :utf-16le))   ; LPCWSTR
  (i-icon-index :int32)   ; INT
  (u-icon-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $Shell_GetCachedImageIndex = Win32::API::More->new('SHELL32',
    'int Shell_GetCachedImageIndex(LPCWSTR pwszIconPath, int iIconIndex, DWORD uIconFlags)');
# my $ret = $Shell_GetCachedImageIndex->Call($pwszIconPath, $iIconIndex, $uIconFlags);
# pwszIconPath : LPCWSTR -> LPCWSTR
# iIconIndex : INT -> int
# uIconFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い