SHGetSpecialFolderPathW
関数CSIDLで指定した特殊フォルダーのパス(Unicode)を取得する。
シグネチャ
// SHELL32.dll (Unicode / -W)
#include <windows.h>
BOOL SHGetSpecialFolderPathW(
HWND hwnd, // optional
LPWSTR pszPath,
INT csidl,
BOOL fCreate
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hwnd | HWND | optional | 予約済みです。 |
| pszPath | LPWSTR | out | 指定したフォルダーのドライブとパスを受け取る、null 終端文字列へのポインターです。このバッファーは少なくとも MAX_PATH 文字分のサイズが必要です。 |
| csidl | INT | in | 対象のフォルダーを識別する CSIDL です。仮想フォルダーを指定した場合、この関数は失敗します。 |
| fCreate | BOOL | in | フォルダーが存在しない場合に作成するかどうかを示します。この値が 0 以外の場合、フォルダーが作成されます。この値が 0 の場合、フォルダーは作成されません。 |
戻り値の型: BOOL
公式ドキュメント
SHGetSpecialFolderPath はサポートされていません。代わりに ShGetFolderPath を使用してください。(Unicode)
戻り値
型: 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)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHELL32.dll (Unicode / -W)
#include <windows.h>
BOOL SHGetSpecialFolderPathW(
HWND hwnd, // optional
LPWSTR pszPath,
INT csidl,
BOOL fCreate
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHELL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool SHGetSpecialFolderPathW(
IntPtr hwnd, // HWND optional
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, // LPWSTR out
int csidl, // INT
bool fCreate // BOOL
);<DllImport("SHELL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SHGetSpecialFolderPathW(
hwnd As IntPtr, ' HWND optional
<MarshalAs(UnmanagedType.LPWStr)> pszPath As System.Text.StringBuilder, ' LPWSTR out
csidl As Integer, ' INT
fCreate As Boolean ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hwnd : HWND optional
' pszPath : LPWSTR out
' csidl : INT
' fCreate : BOOL
Declare PtrSafe Function SHGetSpecialFolderPathW Lib "shell32" ( _
ByVal hwnd As LongPtr, _
ByVal pszPath As LongPtr, _
ByVal csidl As Long, _
ByVal fCreate As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SHGetSpecialFolderPathW = ctypes.windll.shell32.SHGetSpecialFolderPathW
SHGetSpecialFolderPathW.restype = wintypes.BOOL
SHGetSpecialFolderPathW.argtypes = [
wintypes.HANDLE, # hwnd : HWND optional
wintypes.LPWSTR, # pszPath : LPWSTR out
ctypes.c_int, # csidl : INT
wintypes.BOOL, # fCreate : BOOL
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHELL32.dll')
SHGetSpecialFolderPathW = Fiddle::Function.new(
lib['SHGetSpecialFolderPathW'],
[
Fiddle::TYPE_VOIDP, # hwnd : HWND optional
Fiddle::TYPE_VOIDP, # pszPath : LPWSTR out
Fiddle::TYPE_INT, # csidl : INT
Fiddle::TYPE_INT, # fCreate : BOOL
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shell32")]
extern "system" {
fn SHGetSpecialFolderPathW(
hwnd: *mut core::ffi::c_void, // HWND optional
pszPath: *mut u16, // LPWSTR 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.Unicode)]
public static extern bool SHGetSpecialFolderPathW(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, int csidl, bool fCreate);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_SHGetSpecialFolderPathW' -Namespace Win32 -PassThru
# $api::SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate)#uselib "SHELL32.dll"
#func global SHGetSpecialFolderPathW "SHGetSpecialFolderPathW" wptr, wptr, wptr, wptr
; SHGetSpecialFolderPathW hwnd, varptr(pszPath), csidl, fCreate ; 戻り値は stat
; hwnd : HWND optional -> "wptr"
; pszPath : LPWSTR out -> "wptr"
; csidl : INT -> "wptr"
; fCreate : BOOL -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHELL32.dll" #cfunc global SHGetSpecialFolderPathW "SHGetSpecialFolderPathW" sptr, var, int, int ; res = SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate) ; hwnd : HWND optional -> "sptr" ; pszPath : LPWSTR out -> "var" ; csidl : INT -> "int" ; fCreate : BOOL -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHELL32.dll" #cfunc global SHGetSpecialFolderPathW "SHGetSpecialFolderPathW" sptr, sptr, int, int ; res = SHGetSpecialFolderPathW(hwnd, varptr(pszPath), csidl, fCreate) ; hwnd : HWND optional -> "sptr" ; pszPath : LPWSTR out -> "sptr" ; csidl : INT -> "int" ; fCreate : BOOL -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL SHGetSpecialFolderPathW(HWND hwnd, LPWSTR pszPath, INT csidl, BOOL fCreate) #uselib "SHELL32.dll" #cfunc global SHGetSpecialFolderPathW "SHGetSpecialFolderPathW" intptr, var, int, int ; res = SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate) ; hwnd : HWND optional -> "intptr" ; pszPath : LPWSTR out -> "var" ; csidl : INT -> "int" ; fCreate : BOOL -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL SHGetSpecialFolderPathW(HWND hwnd, LPWSTR pszPath, INT csidl, BOOL fCreate) #uselib "SHELL32.dll" #cfunc global SHGetSpecialFolderPathW "SHGetSpecialFolderPathW" intptr, intptr, int, int ; res = SHGetSpecialFolderPathW(hwnd, varptr(pszPath), csidl, fCreate) ; hwnd : HWND optional -> "intptr" ; pszPath : LPWSTR out -> "intptr" ; csidl : INT -> "int" ; fCreate : BOOL -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shell32 = windows.NewLazySystemDLL("SHELL32.dll")
procSHGetSpecialFolderPathW = shell32.NewProc("SHGetSpecialFolderPathW")
)
// hwnd (HWND optional), pszPath (LPWSTR out), csidl (INT), fCreate (BOOL)
r1, _, err := procSHGetSpecialFolderPathW.Call(
uintptr(hwnd),
uintptr(pszPath),
uintptr(csidl),
uintptr(fCreate),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SHGetSpecialFolderPathW(
hwnd: THandle; // HWND optional
pszPath: PWideChar; // LPWSTR out
csidl: Integer; // INT
fCreate: BOOL // BOOL
): BOOL; stdcall;
external 'SHELL32.dll' name 'SHGetSpecialFolderPathW';result := DllCall("SHELL32\SHGetSpecialFolderPathW"
, "Ptr", hwnd ; HWND optional
, "Ptr", pszPath ; LPWSTR out
, "Int", csidl ; INT
, "Int", fCreate ; BOOL
, "Int") ; return: BOOL●SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate) = DLL("SHELL32.dll", "bool SHGetSpecialFolderPathW(void*, char*, int, bool)")
# 呼び出し: SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate)
# hwnd : HWND optional -> "void*"
# pszPath : LPWSTR out -> "char*"
# csidl : INT -> "int"
# fCreate : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shell32" fn SHGetSpecialFolderPathW(
hwnd: ?*anyopaque, // HWND optional
pszPath: [*c]u16, // LPWSTR out
csidl: i32, // INT
fCreate: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc SHGetSpecialFolderPathW(
hwnd: pointer, # HWND optional
pszPath: ptr uint16, # LPWSTR out
csidl: int32, # INT
fCreate: int32 # BOOL
): int32 {.importc: "SHGetSpecialFolderPathW", stdcall, dynlib: "SHELL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shell32");
extern(Windows)
int SHGetSpecialFolderPathW(
void* hwnd, // HWND optional
wchar* pszPath, // LPWSTR out
int csidl, // INT
int fCreate // BOOL
);ccall((:SHGetSpecialFolderPathW, "SHELL32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{UInt16}, Int32, Int32),
hwnd, pszPath, csidl, fCreate)
# hwnd : HWND optional -> Ptr{Cvoid}
# pszPath : LPWSTR out -> Ptr{UInt16}
# csidl : INT -> Int32
# fCreate : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t SHGetSpecialFolderPathW(
void* hwnd,
uint16_t* pszPath,
int32_t csidl,
int32_t fCreate);
]]
local shell32 = ffi.load("shell32")
-- shell32.SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate)
-- hwnd : HWND optional
-- pszPath : LPWSTR out
-- csidl : INT
-- fCreate : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const SHGetSpecialFolderPathW = lib.func('__stdcall', 'SHGetSpecialFolderPathW', 'int32_t', ['void *', 'uint16_t *', 'int32_t', 'int32_t']);
// SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate)
// hwnd : HWND optional -> 'void *'
// pszPath : LPWSTR out -> 'uint16_t *'
// csidl : INT -> 'int32_t'
// fCreate : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHELL32.dll", {
SHGetSpecialFolderPathW: { parameters: ["pointer", "buffer", "i32", "i32"], result: "i32" },
});
// lib.symbols.SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate)
// hwnd : HWND optional -> "pointer"
// pszPath : LPWSTR out -> "buffer"
// csidl : INT -> "i32"
// fCreate : BOOL -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SHGetSpecialFolderPathW(
void* hwnd,
uint16_t* pszPath,
int32_t csidl,
int32_t fCreate);
C, "SHELL32.dll");
// $ffi->SHGetSpecialFolderPathW(hwnd, pszPath, csidl, fCreate);
// hwnd : HWND optional
// pszPath : LPWSTR 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.UNICODE_OPTIONS);
boolean SHGetSpecialFolderPathW(
Pointer hwnd, // HWND optional
char[] pszPath, // LPWSTR out
int csidl, // INT
boolean fCreate // BOOL
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shell32")]
lib LibSHELL32
fun SHGetSpecialFolderPathW = SHGetSpecialFolderPathW(
hwnd : Void*, # HWND optional
pszPath : UInt16*, # LPWSTR 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 SHGetSpecialFolderPathWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Int32, Int32);
typedef SHGetSpecialFolderPathWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, int);
final SHGetSpecialFolderPathW = DynamicLibrary.open('SHELL32.dll')
.lookupFunction<SHGetSpecialFolderPathWNative, SHGetSpecialFolderPathWDart>('SHGetSpecialFolderPathW');
// hwnd : HWND optional -> Pointer<Void>
// pszPath : LPWSTR out -> Pointer<Utf16>
// csidl : INT -> Int32
// fCreate : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SHGetSpecialFolderPathW(
hwnd: THandle; // HWND optional
pszPath: PWideChar; // LPWSTR out
csidl: Integer; // INT
fCreate: BOOL // BOOL
): BOOL; stdcall;
external 'SHELL32.dll' name 'SHGetSpecialFolderPathW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SHGetSpecialFolderPathW"
c_SHGetSpecialFolderPathW :: Ptr () -> CWString -> Int32 -> CInt -> IO CInt
-- hwnd : HWND optional -> Ptr ()
-- pszPath : LPWSTR out -> CWString
-- csidl : INT -> Int32
-- fCreate : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let shgetspecialfolderpathw =
foreign "SHGetSpecialFolderPathW"
((ptr void) @-> (ptr uint16_t) @-> int32_t @-> int32_t @-> returning int32_t)
(* hwnd : HWND optional -> (ptr void) *)
(* pszPath : LPWSTR out -> (ptr uint16_t) *)
(* 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 ("SHGetSpecialFolderPathW" shget-special-folder-path-w :convention :stdcall) :int32
(hwnd :pointer) ; HWND optional
(psz-path :pointer) ; LPWSTR out
(csidl :int32) ; INT
(f-create :int32)) ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SHGetSpecialFolderPathW = Win32::API::More->new('SHELL32',
'BOOL SHGetSpecialFolderPathW(HANDLE hwnd, LPWSTR pszPath, int csidl, BOOL fCreate)');
# my $ret = $SHGetSpecialFolderPathW->Call($hwnd, $pszPath, $csidl, $fCreate);
# hwnd : HWND optional -> HANDLE
# pszPath : LPWSTR out -> LPWSTR
# csidl : INT -> int
# fCreate : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f SHGetSpecialFolderPathA (ANSI版) — CSIDLで指定した特殊フォルダーのパス(ANSI)を取得する。