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

SHGetSpecialFolderLocation

関数
CSIDLで指定した特殊フォルダーのPIDLを取得する。
DLLSHELL32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

HRESULT SHGetSpecialFolderLocation(
    HWND hwnd,   // optional
    INT csidl,
    ITEMIDLIST** ppidl
);

パラメーター

名前方向説明
hwndHWNDoptional予約済みです。
csidlINTin対象のフォルダーを識別する CSIDL 値です。
ppidlITEMIDLIST**out名前空間のルート(デスクトップ)を基準としたフォルダーの位置を指定する PIDL です。返された IDList を CoTaskMemFree を使用して解放するのは、呼び出し側アプリケーションの責任です。

戻り値の型: HRESULT

公式ドキュメント

SHGetSpecialFolderLocation はサポートされておらず、将来変更または利用できなくなる可能性があります。代わりに SHGetFolderLocation を使用してください。

戻り値

型: HRESULT

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

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

各言語での呼び出し定義

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

HRESULT SHGetSpecialFolderLocation(
    HWND hwnd,   // optional
    INT csidl,
    ITEMIDLIST** ppidl
);
[DllImport("SHELL32.dll", ExactSpelling = true)]
static extern int SHGetSpecialFolderLocation(
    IntPtr hwnd,   // HWND optional
    int csidl,   // INT
    IntPtr ppidl   // ITEMIDLIST** out
);
<DllImport("SHELL32.dll", ExactSpelling:=True)>
Public Shared Function SHGetSpecialFolderLocation(
    hwnd As IntPtr,   ' HWND optional
    csidl As Integer,   ' INT
    ppidl As IntPtr   ' ITEMIDLIST** out
) As Integer
End Function
' hwnd : HWND optional
' csidl : INT
' ppidl : ITEMIDLIST** out
Declare PtrSafe Function SHGetSpecialFolderLocation Lib "shell32" ( _
    ByVal hwnd As LongPtr, _
    ByVal csidl As Long, _
    ByVal ppidl As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SHGetSpecialFolderLocation = ctypes.windll.shell32.SHGetSpecialFolderLocation
SHGetSpecialFolderLocation.restype = ctypes.c_int
SHGetSpecialFolderLocation.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND optional
    ctypes.c_int,  # csidl : INT
    ctypes.c_void_p,  # ppidl : ITEMIDLIST** out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
SHGetSpecialFolderLocation = Fiddle::Function.new(
  lib['SHGetSpecialFolderLocation'],
  [
    Fiddle::TYPE_VOIDP,  # hwnd : HWND optional
    Fiddle::TYPE_INT,  # csidl : INT
    Fiddle::TYPE_VOIDP,  # ppidl : ITEMIDLIST** out
  ],
  Fiddle::TYPE_INT)
#[link(name = "shell32")]
extern "system" {
    fn SHGetSpecialFolderLocation(
        hwnd: *mut core::ffi::c_void,  // HWND optional
        csidl: i32,  // INT
        ppidl: *mut *mut ITEMIDLIST  // ITEMIDLIST** out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHELL32.dll")]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, IntPtr ppidl);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_SHGetSpecialFolderLocation' -Namespace Win32 -PassThru
# $api::SHGetSpecialFolderLocation(hwnd, csidl, ppidl)
#uselib "SHELL32.dll"
#func global SHGetSpecialFolderLocation "SHGetSpecialFolderLocation" sptr, sptr, sptr
; SHGetSpecialFolderLocation hwnd, csidl, varptr(ppidl)   ; 戻り値は stat
; hwnd : HWND optional -> "sptr"
; csidl : INT -> "sptr"
; ppidl : ITEMIDLIST** out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHELL32.dll"
#cfunc global SHGetSpecialFolderLocation "SHGetSpecialFolderLocation" sptr, int, var
; res = SHGetSpecialFolderLocation(hwnd, csidl, ppidl)
; hwnd : HWND optional -> "sptr"
; csidl : INT -> "int"
; ppidl : ITEMIDLIST** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT SHGetSpecialFolderLocation(HWND hwnd, INT csidl, ITEMIDLIST** ppidl)
#uselib "SHELL32.dll"
#cfunc global SHGetSpecialFolderLocation "SHGetSpecialFolderLocation" intptr, int, var
; res = SHGetSpecialFolderLocation(hwnd, csidl, ppidl)
; hwnd : HWND optional -> "intptr"
; csidl : INT -> "int"
; ppidl : ITEMIDLIST** out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procSHGetSpecialFolderLocation = shell32.NewProc("SHGetSpecialFolderLocation")
)

// hwnd (HWND optional), csidl (INT), ppidl (ITEMIDLIST** out)
r1, _, err := procSHGetSpecialFolderLocation.Call(
	uintptr(hwnd),
	uintptr(csidl),
	uintptr(ppidl),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SHGetSpecialFolderLocation(
  hwnd: THandle;   // HWND optional
  csidl: Integer;   // INT
  ppidl: Pointer   // ITEMIDLIST** out
): Integer; stdcall;
  external 'SHELL32.dll' name 'SHGetSpecialFolderLocation';
result := DllCall("SHELL32\SHGetSpecialFolderLocation"
    , "Ptr", hwnd   ; HWND optional
    , "Int", csidl   ; INT
    , "Ptr", ppidl   ; ITEMIDLIST** out
    , "Int")   ; return: HRESULT
●SHGetSpecialFolderLocation(hwnd, csidl, ppidl) = DLL("SHELL32.dll", "int SHGetSpecialFolderLocation(void*, int, void*)")
# 呼び出し: SHGetSpecialFolderLocation(hwnd, csidl, ppidl)
# hwnd : HWND optional -> "void*"
# csidl : INT -> "int"
# ppidl : ITEMIDLIST** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef SHGetSpecialFolderLocationNative = Int32 Function(Pointer<Void>, Int32, Pointer<Void>);
typedef SHGetSpecialFolderLocationDart = int Function(Pointer<Void>, int, Pointer<Void>);
final SHGetSpecialFolderLocation = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<SHGetSpecialFolderLocationNative, SHGetSpecialFolderLocationDart>('SHGetSpecialFolderLocation');
// hwnd : HWND optional -> Pointer<Void>
// csidl : INT -> Int32
// ppidl : ITEMIDLIST** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHGetSpecialFolderLocation(
  hwnd: THandle;   // HWND optional
  csidl: Integer;   // INT
  ppidl: Pointer   // ITEMIDLIST** out
): Integer; stdcall;
  external 'SHELL32.dll' name 'SHGetSpecialFolderLocation';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SHGetSpecialFolderLocation"
  c_SHGetSpecialFolderLocation :: Ptr () -> Int32 -> Ptr () -> IO Int32
-- hwnd : HWND optional -> Ptr ()
-- csidl : INT -> Int32
-- ppidl : ITEMIDLIST** out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shgetspecialfolderlocation =
  foreign "SHGetSpecialFolderLocation"
    ((ptr void) @-> int32_t @-> (ptr void) @-> returning int32_t)
(* hwnd : HWND optional -> (ptr void) *)
(* csidl : INT -> int32_t *)
(* ppidl : ITEMIDLIST** out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("SHGetSpecialFolderLocation" shget-special-folder-location :convention :stdcall) :int32
  (hwnd :pointer)   ; HWND optional
  (csidl :int32)   ; INT
  (ppidl :pointer))   ; ITEMIDLIST** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHGetSpecialFolderLocation = Win32::API::More->new('SHELL32',
    'int SHGetSpecialFolderLocation(HANDLE hwnd, int csidl, LPVOID ppidl)');
# my $ret = $SHGetSpecialFolderLocation->Call($hwnd, $csidl, $ppidl);
# hwnd : HWND optional -> HANDLE
# csidl : INT -> int
# ppidl : ITEMIDLIST** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型