PathBuildRootW
関数ドライブ番号からルートパス文字列を生成する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
LPWSTR PathBuildRootW(
LPWSTR pszRoot,
INT iDrive
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszRoot | LPWSTR | out | 構築されたルートパスを受け取る文字列へのポインターです。このバッファーのサイズは少なくとも4文字分必要です。 |
| iDrive | INT | in | 目的のドライブ番号を示す int 型の変数です。0から25の範囲で指定する必要があります。 |
戻り値の型: LPWSTR
公式ドキュメント
指定されたドライブ番号からルートパスを作成します。(Unicode)
戻り値
型: LPTSTR
構築されたルートパスのアドレスを返します。何らかの理由(たとえば、無効なドライブ番号)で呼び出しが失敗した場合は、szRoot が変更されずに返されます。
解説(Remarks)
メモ
shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして、PathBuildRoot を定義しています。エンコーディング中立のエイリアスの使用を、エンコーディング中立ではないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因となる場合があります。詳細については、Conventions for Function Prototypesを参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
LPWSTR PathBuildRootW(
LPWSTR pszRoot,
INT iDrive
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr PathBuildRootW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszRoot, // LPWSTR out
int iDrive // INT
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PathBuildRootW(
<MarshalAs(UnmanagedType.LPWStr)> pszRoot As System.Text.StringBuilder, ' LPWSTR out
iDrive As Integer ' INT
) As IntPtr
End Function' pszRoot : LPWSTR out
' iDrive : INT
Declare PtrSafe Function PathBuildRootW Lib "shlwapi" ( _
ByVal pszRoot As LongPtr, _
ByVal iDrive As Long) As LongPtr
' 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
PathBuildRootW = ctypes.windll.shlwapi.PathBuildRootW
PathBuildRootW.restype = wintypes.LPWSTR
PathBuildRootW.argtypes = [
wintypes.LPWSTR, # pszRoot : LPWSTR out
ctypes.c_int, # iDrive : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathBuildRootW = Fiddle::Function.new(
lib['PathBuildRootW'],
[
Fiddle::TYPE_VOIDP, # pszRoot : LPWSTR out
Fiddle::TYPE_INT, # iDrive : INT
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn PathBuildRootW(
pszRoot: *mut u16, // LPWSTR out
iDrive: i32 // INT
) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr PathBuildRootW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszRoot, int iDrive);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathBuildRootW' -Namespace Win32 -PassThru
# $api::PathBuildRootW(pszRoot, iDrive)#uselib "SHLWAPI.dll"
#func global PathBuildRootW "PathBuildRootW" wptr, wptr
; PathBuildRootW varptr(pszRoot), iDrive ; 戻り値は stat
; pszRoot : LPWSTR out -> "wptr"
; iDrive : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathBuildRootW "PathBuildRootW" var, int ; res = PathBuildRootW(pszRoot, iDrive) ; pszRoot : LPWSTR out -> "var" ; iDrive : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathBuildRootW "PathBuildRootW" sptr, int ; res = PathBuildRootW(varptr(pszRoot), iDrive) ; pszRoot : LPWSTR out -> "sptr" ; iDrive : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; LPWSTR PathBuildRootW(LPWSTR pszRoot, INT iDrive) #uselib "SHLWAPI.dll" #cfunc global PathBuildRootW "PathBuildRootW" var, int ; res = PathBuildRootW(pszRoot, iDrive) ; pszRoot : LPWSTR out -> "var" ; iDrive : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; LPWSTR PathBuildRootW(LPWSTR pszRoot, INT iDrive) #uselib "SHLWAPI.dll" #cfunc global PathBuildRootW "PathBuildRootW" intptr, int ; res = PathBuildRootW(varptr(pszRoot), iDrive) ; pszRoot : LPWSTR out -> "intptr" ; iDrive : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathBuildRootW = shlwapi.NewProc("PathBuildRootW")
)
// pszRoot (LPWSTR out), iDrive (INT)
r1, _, err := procPathBuildRootW.Call(
uintptr(pszRoot),
uintptr(iDrive),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPWSTRfunction PathBuildRootW(
pszRoot: PWideChar; // LPWSTR out
iDrive: Integer // INT
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'PathBuildRootW';result := DllCall("SHLWAPI\PathBuildRootW"
, "Ptr", pszRoot ; LPWSTR out
, "Int", iDrive ; INT
, "Ptr") ; return: LPWSTR●PathBuildRootW(pszRoot, iDrive) = DLL("SHLWAPI.dll", "char* PathBuildRootW(char*, int)")
# 呼び出し: PathBuildRootW(pszRoot, iDrive)
# pszRoot : LPWSTR out -> "char*"
# iDrive : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shlwapi" fn PathBuildRootW(
pszRoot: [*c]u16, // LPWSTR out
iDrive: i32 // INT
) callconv(std.os.windows.WINAPI) [*c]const u16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc PathBuildRootW(
pszRoot: ptr uint16, # LPWSTR out
iDrive: int32 # INT
): WideCString {.importc: "PathBuildRootW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* PathBuildRootW(
wchar* pszRoot, // LPWSTR out
int iDrive // INT
);ccall((:PathBuildRootW, "SHLWAPI.dll"), stdcall, Cwstring,
(Ptr{UInt16}, Int32),
pszRoot, iDrive)
# pszRoot : LPWSTR out -> Ptr{UInt16}
# iDrive : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
const uint16_t* PathBuildRootW(
uint16_t* pszRoot,
int32_t iDrive);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathBuildRootW(pszRoot, iDrive)
-- pszRoot : LPWSTR out
-- iDrive : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathBuildRootW = lib.func('__stdcall', 'PathBuildRootW', 'void *', ['uint16_t *', 'int32_t']);
// PathBuildRootW(pszRoot, iDrive)
// pszRoot : LPWSTR out -> 'uint16_t *'
// iDrive : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
PathBuildRootW: { parameters: ["buffer", "i32"], result: "pointer" },
});
// lib.symbols.PathBuildRootW(pszRoot, iDrive)
// pszRoot : LPWSTR out -> "buffer"
// iDrive : INT -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const uint16_t* PathBuildRootW(
uint16_t* pszRoot,
int32_t iDrive);
C, "SHLWAPI.dll");
// $ffi->PathBuildRootW(pszRoot, iDrive);
// pszRoot : LPWSTR out
// iDrive : 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 Shlwapi extends StdCallLibrary {
Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.UNICODE_OPTIONS);
Pointer PathBuildRootW(
char[] pszRoot, // LPWSTR out
int iDrive // INT
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shlwapi")]
lib LibSHLWAPI
fun PathBuildRootW = PathBuildRootW(
pszRoot : UInt16*, # LPWSTR out
iDrive : Int32 # INT
) : UInt16*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PathBuildRootWNative = Pointer<Utf16> Function(Pointer<Utf16>, Int32);
typedef PathBuildRootWDart = Pointer<Utf16> Function(Pointer<Utf16>, int);
final PathBuildRootW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<PathBuildRootWNative, PathBuildRootWDart>('PathBuildRootW');
// pszRoot : LPWSTR out -> Pointer<Utf16>
// iDrive : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PathBuildRootW(
pszRoot: PWideChar; // LPWSTR out
iDrive: Integer // INT
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'PathBuildRootW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PathBuildRootW"
c_PathBuildRootW :: CWString -> Int32 -> IO CWString
-- pszRoot : LPWSTR out -> CWString
-- iDrive : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pathbuildrootw =
foreign "PathBuildRootW"
((ptr uint16_t) @-> int32_t @-> returning (ptr uint16_t))
(* pszRoot : LPWSTR out -> (ptr uint16_t) *)
(* iDrive : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("PathBuildRootW" path-build-root-w :convention :stdcall) (:string :encoding :utf-16le)
(psz-root :pointer) ; LPWSTR out
(i-drive :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PathBuildRootW = Win32::API::More->new('SHLWAPI',
'LPWSTR PathBuildRootW(LPWSTR pszRoot, int iDrive)');
# my $ret = $PathBuildRootW->Call($pszRoot, $iDrive);
# pszRoot : LPWSTR out -> LPWSTR
# iDrive : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f PathBuildRootA (ANSI版) — ドライブ番号からルートパス文字列を生成する。