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

SHSetLocalizedName

関数
パスにローカライズされた表示名を設定する。
DLLSHELL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHELL32.dll
#include <windows.h>

HRESULT SHSetLocalizedName(
    LPCWSTR pszPath,
    LPCWSTR pszResModule,
    INT idsRes
);

パラメーター

名前方向説明
pszPathLPCWSTRin対象ファイルの完全修飾パスを指定する文字列へのポインターです。
pszResModuleLPCWSTRinファイル名のローカライズされたバージョンを指定する文字列リソースへのポインターです。
idsResINTin文字列リソース内のローカライズされたファイル名を指定する整数 ID です。

戻り値の型: HRESULT

公式ドキュメント

シェルフォルダー内のファイルのローカライズされた名前を設定します。

戻り値

型: HRESULT

この関数が成功した場合は S_OK を返します。それ以外の場合は HRESULT エラーコードを返します。

解説(Remarks)

この文字列が設定されると、エクスプローラーはファイル名の代わりにこの文字列を表示します。ファイルへのパスは変更されません。

アプリケーションは、SIGDN_NORMALDISPLAY フラグを指定した IShellFolder::GetDisplayNameOf で表示用 (ローカライズされた) 名前を取得でき、SIGDN_DESKTOPABSOLUTEPARSING フラグを使用した IShellItem::GetDisplayName で解析用 (ローカライズされていない) 名前を取得できます。

SHRemoveLocalizedName を呼び出すと、表示名は解析名と同一になります。

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

各言語での呼び出し定義

// SHELL32.dll
#include <windows.h>

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

SHSetLocalizedName = ctypes.windll.shell32.SHSetLocalizedName
SHSetLocalizedName.restype = ctypes.c_int
SHSetLocalizedName.argtypes = [
    wintypes.LPCWSTR,  # pszPath : LPCWSTR
    wintypes.LPCWSTR,  # pszResModule : LPCWSTR
    ctypes.c_int,  # idsRes : INT
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procSHSetLocalizedName = shell32.NewProc("SHSetLocalizedName")
)

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

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

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

foreign import stdcall safe "SHSetLocalizedName"
  c_SHSetLocalizedName :: CWString -> CWString -> Int32 -> IO Int32
-- pszPath : LPCWSTR -> CWString
-- pszResModule : LPCWSTR -> CWString
-- idsRes : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shsetlocalizedname =
  foreign "SHSetLocalizedName"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> returning int32_t)
(* pszPath : LPCWSTR -> (ptr uint16_t) *)
(* pszResModule : LPCWSTR -> (ptr uint16_t) *)
(* idsRes : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("SHSetLocalizedName" shset-localized-name :convention :stdcall) :int32
  (psz-path (:string :encoding :utf-16le))   ; LPCWSTR
  (psz-res-module (:string :encoding :utf-16le))   ; LPCWSTR
  (ids-res :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHSetLocalizedName = Win32::API::More->new('SHELL32',
    'int SHSetLocalizedName(LPCWSTR pszPath, LPCWSTR pszResModule, int idsRes)');
# my $ret = $SHSetLocalizedName->Call($pszPath, $pszResModule, $idsRes);
# pszPath : LPCWSTR -> LPCWSTR
# pszResModule : LPCWSTR -> LPCWSTR
# idsRes : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。