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

Shell_GetCachedImageIndexA

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

シグネチャ

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

INT Shell_GetCachedImageIndexA(
    LPCSTR pszIconPath,
    INT iIconIndex,
    DWORD uIconFlags
);

パラメーター

名前方向説明
pszIconPathLPCSTRinTBD
iIconIndexINTinpwszIconPath で指定されたファイル内のイメージのインデックス。
uIconFlagsDWORDin

使用されません。

- pwszIconPath [in]

型: PCWSTR

イメージファイルへのパスを格納するバッファーへのポインター。

戻り値の型: INT

公式ドキュメント

Shell_GetCachedImageIndex は変更されるか、利用できなくなる可能性があります。(ANSI)

戻り値

型: 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 文字列が必要です。

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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_GetCachedImageIndexA(
    LPCSTR pszIconPath,
    INT iIconIndex,
    DWORD uIconFlags
);
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int Shell_GetCachedImageIndexA(
    [MarshalAs(UnmanagedType.LPStr)] string pszIconPath,   // LPCSTR
    int iIconIndex,   // INT
    uint uIconFlags   // DWORD
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function Shell_GetCachedImageIndexA(
    <MarshalAs(UnmanagedType.LPStr)> pszIconPath As String,   ' LPCSTR
    iIconIndex As Integer,   ' INT
    uIconFlags As UInteger   ' DWORD
) As Integer
End Function
' pszIconPath : LPCSTR
' iIconIndex : INT
' uIconFlags : DWORD
Declare PtrSafe Function Shell_GetCachedImageIndexA Lib "shell32" ( _
    ByVal pszIconPath As String, _
    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_GetCachedImageIndexA = ctypes.windll.shell32.Shell_GetCachedImageIndexA
Shell_GetCachedImageIndexA.restype = ctypes.c_int
Shell_GetCachedImageIndexA.argtypes = [
    wintypes.LPCSTR,  # pszIconPath : LPCSTR
    ctypes.c_int,  # iIconIndex : INT
    wintypes.DWORD,  # uIconFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
Shell_GetCachedImageIndexA = Fiddle::Function.new(
  lib['Shell_GetCachedImageIndexA'],
  [
    Fiddle::TYPE_VOIDP,  # pszIconPath : LPCSTR
    Fiddle::TYPE_INT,  # iIconIndex : INT
    -Fiddle::TYPE_INT,  # uIconFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "shell32")]
extern "system" {
    fn Shell_GetCachedImageIndexA(
        pszIconPath: *const u8,  // LPCSTR
        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_GetCachedImageIndexA([MarshalAs(UnmanagedType.LPStr)] string pszIconPath, int iIconIndex, uint uIconFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_Shell_GetCachedImageIndexA' -Namespace Win32 -PassThru
# $api::Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags)
#uselib "SHELL32.dll"
#func global Shell_GetCachedImageIndexA "Shell_GetCachedImageIndexA" sptr, sptr, sptr
; Shell_GetCachedImageIndexA pszIconPath, iIconIndex, uIconFlags   ; 戻り値は stat
; pszIconPath : LPCSTR -> "sptr"
; iIconIndex : INT -> "sptr"
; uIconFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHELL32.dll"
#cfunc global Shell_GetCachedImageIndexA "Shell_GetCachedImageIndexA" str, int, int
; res = Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags)
; pszIconPath : LPCSTR -> "str"
; iIconIndex : INT -> "int"
; uIconFlags : DWORD -> "int"
; INT Shell_GetCachedImageIndexA(LPCSTR pszIconPath, INT iIconIndex, DWORD uIconFlags)
#uselib "SHELL32.dll"
#cfunc global Shell_GetCachedImageIndexA "Shell_GetCachedImageIndexA" str, int, int
; res = Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags)
; pszIconPath : LPCSTR -> "str"
; iIconIndex : INT -> "int"
; uIconFlags : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procShell_GetCachedImageIndexA = shell32.NewProc("Shell_GetCachedImageIndexA")
)

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

extern "shell32" fn Shell_GetCachedImageIndexA(
    pszIconPath: [*c]const u8, // LPCSTR
    iIconIndex: i32, // INT
    uIconFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc Shell_GetCachedImageIndexA(
    pszIconPath: cstring,  # LPCSTR
    iIconIndex: int32,  # INT
    uIconFlags: uint32  # DWORD
): int32 {.importc: "Shell_GetCachedImageIndexA", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
int Shell_GetCachedImageIndexA(
    const(char)* pszIconPath,   // LPCSTR
    int iIconIndex,   // INT
    uint uIconFlags   // DWORD
);
ccall((:Shell_GetCachedImageIndexA, "SHELL32.dll"), stdcall, Int32,
      (Cstring, Int32, UInt32),
      pszIconPath, iIconIndex, uIconFlags)
# pszIconPath : LPCSTR -> Cstring
# iIconIndex : INT -> Int32
# uIconFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t Shell_GetCachedImageIndexA(
    const char* pszIconPath,
    int32_t iIconIndex,
    uint32_t uIconFlags);
]]
local shell32 = ffi.load("shell32")
-- shell32.Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags)
-- pszIconPath : LPCSTR
-- iIconIndex : INT
-- uIconFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const Shell_GetCachedImageIndexA = lib.func('__stdcall', 'Shell_GetCachedImageIndexA', 'int32_t', ['str', 'int32_t', 'uint32_t']);
// Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags)
// pszIconPath : LPCSTR -> 'str'
// iIconIndex : INT -> 'int32_t'
// uIconFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  Shell_GetCachedImageIndexA: { parameters: ["buffer", "i32", "u32"], result: "i32" },
});
// lib.symbols.Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags)
// pszIconPath : LPCSTR -> "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_GetCachedImageIndexA(
    const char* pszIconPath,
    int32_t iIconIndex,
    uint32_t uIconFlags);
C, "SHELL32.dll");
// $ffi->Shell_GetCachedImageIndexA(pszIconPath, iIconIndex, uIconFlags);
// pszIconPath : LPCSTR
// 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_GetCachedImageIndexA(
        String pszIconPath,   // LPCSTR
        int iIconIndex,   // INT
        int uIconFlags   // DWORD
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun Shell_GetCachedImageIndexA = Shell_GetCachedImageIndexA(
    pszIconPath : UInt8*,   # LPCSTR
    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_GetCachedImageIndexANative = Int32 Function(Pointer<Utf8>, Int32, Uint32);
typedef Shell_GetCachedImageIndexADart = int Function(Pointer<Utf8>, int, int);
final Shell_GetCachedImageIndexA = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<Shell_GetCachedImageIndexANative, Shell_GetCachedImageIndexADart>('Shell_GetCachedImageIndexA');
// pszIconPath : LPCSTR -> Pointer<Utf8>
// iIconIndex : INT -> Int32
// uIconFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function Shell_GetCachedImageIndexA(
  pszIconPath: PAnsiChar;   // LPCSTR
  iIconIndex: Integer;   // INT
  uIconFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'SHELL32.dll' name 'Shell_GetCachedImageIndexA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let shell_getcachedimageindexa =
  foreign "Shell_GetCachedImageIndexA"
    (string @-> int32_t @-> uint32_t @-> returning int32_t)
(* pszIconPath : LPCSTR -> string *)
(* 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_GetCachedImageIndexA" shell-get-cached-image-index-a :convention :stdcall) :int32
  (psz-icon-path :string)   ; LPCSTR
  (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_GetCachedImageIndexA = Win32::API::More->new('SHELL32',
    'int Shell_GetCachedImageIndexA(LPCSTR pszIconPath, int iIconIndex, DWORD uIconFlags)');
# my $ret = $Shell_GetCachedImageIndexA->Call($pszIconPath, $iIconIndex, $uIconFlags);
# pszIconPath : LPCSTR -> LPCSTR
# iIconIndex : INT -> int
# uIconFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い