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

SHGetSpecialFolderPathA

関数
CSIDLで指定した特殊フォルダーのパス(ANSI)を取得する。
DLLSHELL32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

BOOL SHGetSpecialFolderPathA(
    HWND hwnd,   // optional
    LPSTR pszPath,
    INT csidl,
    BOOL fCreate
);

パラメーター

名前方向説明
hwndHWNDoptional予約済みです。
pszPathLPSTRout指定したフォルダーのドライブとパスを受け取る、null で終わる文字列へのポインターです。このバッファーのサイズは少なくとも MAX_PATH 文字分必要です。
csidlINTin対象とするフォルダーを識別する CSIDL です。仮想フォルダーを指定した場合、この関数は失敗します。
fCreateBOOLinフォルダーがまだ存在しない場合に作成するかどうかを示します。この値が 0 以外の場合、フォルダーが作成されます。この値が 0 の場合、フォルダーは作成されません。

戻り値の型: BOOL

公式ドキュメント

SHGetSpecialFolderPath はサポートされていません。代わりに ShGetFolderPath を使用してください。(ANSI)

戻り値

型: BOOL

成功した場合は TRUE、それ以外の場合は FALSE です。

解説(Remarks)

この関数を利用するには、Microsoft Internet Explorer 4.0 Desktop Update がインストールされている必要があります。

メモ

shlobj_core.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI バージョンと Unicode バージョンを自動的に選択するエイリアスとして SHGetSpecialFolderPath を定義しています。エンコード中立のエイリアスと、エンコード中立でないコードを混在させて使用すると、不一致が生じてコンパイルエラーや実行時エラーの原因となる場合があります。詳細については、関数プロトタイプの規則 を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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>

BOOL SHGetSpecialFolderPathA(
    HWND hwnd,   // optional
    LPSTR pszPath,
    INT csidl,
    BOOL fCreate
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHELL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool SHGetSpecialFolderPathA(
    IntPtr hwnd,   // HWND optional
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszPath,   // LPSTR out
    int csidl,   // INT
    bool fCreate   // BOOL
);
<DllImport("SHELL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function SHGetSpecialFolderPathA(
    hwnd As IntPtr,   ' HWND optional
    <MarshalAs(UnmanagedType.LPStr)> pszPath As System.Text.StringBuilder,   ' LPSTR out
    csidl As Integer,   ' INT
    fCreate As Boolean   ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hwnd : HWND optional
' pszPath : LPSTR out
' csidl : INT
' fCreate : BOOL
Declare PtrSafe Function SHGetSpecialFolderPathA Lib "shell32" ( _
    ByVal hwnd As LongPtr, _
    ByVal pszPath As String, _
    ByVal csidl As Long, _
    ByVal fCreate As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SHGetSpecialFolderPathA = ctypes.windll.shell32.SHGetSpecialFolderPathA
SHGetSpecialFolderPathA.restype = wintypes.BOOL
SHGetSpecialFolderPathA.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND optional
    wintypes.LPSTR,  # pszPath : LPSTR out
    ctypes.c_int,  # csidl : INT
    wintypes.BOOL,  # fCreate : BOOL
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procSHGetSpecialFolderPathA = shell32.NewProc("SHGetSpecialFolderPathA")
)

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

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

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

foreign import stdcall safe "SHGetSpecialFolderPathA"
  c_SHGetSpecialFolderPathA :: Ptr () -> CString -> Int32 -> CInt -> IO CInt
-- hwnd : HWND optional -> Ptr ()
-- pszPath : LPSTR out -> CString
-- csidl : INT -> Int32
-- fCreate : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

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

関連項目

文字セット違い