PathRelativePathToW
関数ある場所から別の場所への相対パスを生成する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
BOOL PathRelativePathToW(
LPWSTR pszPath,
LPCWSTR pszFrom,
DWORD dwAttrFrom,
LPCWSTR pszTo,
DWORD dwAttrTo
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| pszPath | LPWSTR | out |
| pszFrom | LPCWSTR | in |
| dwAttrFrom | DWORD | in |
| pszTo | LPCWSTR | in |
| dwAttrTo | DWORD | in |
戻り値の型: BOOL
各言語での呼び出し定義
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
BOOL PathRelativePathToW(
LPWSTR pszPath,
LPCWSTR pszFrom,
DWORD dwAttrFrom,
LPCWSTR pszTo,
DWORD dwAttrTo
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool PathRelativePathToW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, // LPWSTR out
[MarshalAs(UnmanagedType.LPWStr)] string pszFrom, // LPCWSTR
uint dwAttrFrom, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] string pszTo, // LPCWSTR
uint dwAttrTo // DWORD
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PathRelativePathToW(
<MarshalAs(UnmanagedType.LPWStr)> pszPath As System.Text.StringBuilder, ' LPWSTR out
<MarshalAs(UnmanagedType.LPWStr)> pszFrom As String, ' LPCWSTR
dwAttrFrom As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPWStr)> pszTo As String, ' LPCWSTR
dwAttrTo As UInteger ' DWORD
) As Boolean
End Function' pszPath : LPWSTR out
' pszFrom : LPCWSTR
' dwAttrFrom : DWORD
' pszTo : LPCWSTR
' dwAttrTo : DWORD
Declare PtrSafe Function PathRelativePathToW Lib "shlwapi" ( _
ByVal pszPath As LongPtr, _
ByVal pszFrom As LongPtr, _
ByVal dwAttrFrom As Long, _
ByVal pszTo As LongPtr, _
ByVal dwAttrTo 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
PathRelativePathToW = ctypes.windll.shlwapi.PathRelativePathToW
PathRelativePathToW.restype = wintypes.BOOL
PathRelativePathToW.argtypes = [
wintypes.LPWSTR, # pszPath : LPWSTR out
wintypes.LPCWSTR, # pszFrom : LPCWSTR
wintypes.DWORD, # dwAttrFrom : DWORD
wintypes.LPCWSTR, # pszTo : LPCWSTR
wintypes.DWORD, # dwAttrTo : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
PathRelativePathToW = Fiddle::Function.new(
lib['PathRelativePathToW'],
[
Fiddle::TYPE_VOIDP, # pszPath : LPWSTR out
Fiddle::TYPE_VOIDP, # pszFrom : LPCWSTR
-Fiddle::TYPE_INT, # dwAttrFrom : DWORD
Fiddle::TYPE_VOIDP, # pszTo : LPCWSTR
-Fiddle::TYPE_INT, # dwAttrTo : DWORD
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn PathRelativePathToW(
pszPath: *mut u16, // LPWSTR out
pszFrom: *const u16, // LPCWSTR
dwAttrFrom: u32, // DWORD
pszTo: *const u16, // LPCWSTR
dwAttrTo: u32 // DWORD
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern bool PathRelativePathToW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, [MarshalAs(UnmanagedType.LPWStr)] string pszFrom, uint dwAttrFrom, [MarshalAs(UnmanagedType.LPWStr)] string pszTo, uint dwAttrTo);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathRelativePathToW' -Namespace Win32 -PassThru
# $api::PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo)#uselib "SHLWAPI.dll"
#func global PathRelativePathToW "PathRelativePathToW" wptr, wptr, wptr, wptr, wptr
; PathRelativePathToW varptr(pszPath), pszFrom, dwAttrFrom, pszTo, dwAttrTo ; 戻り値は stat
; pszPath : LPWSTR out -> "wptr"
; pszFrom : LPCWSTR -> "wptr"
; dwAttrFrom : DWORD -> "wptr"
; pszTo : LPCWSTR -> "wptr"
; dwAttrTo : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global PathRelativePathToW "PathRelativePathToW" var, wstr, int, wstr, int ; res = PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo) ; pszPath : LPWSTR out -> "var" ; pszFrom : LPCWSTR -> "wstr" ; dwAttrFrom : DWORD -> "int" ; pszTo : LPCWSTR -> "wstr" ; dwAttrTo : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global PathRelativePathToW "PathRelativePathToW" sptr, wstr, int, wstr, int ; res = PathRelativePathToW(varptr(pszPath), pszFrom, dwAttrFrom, pszTo, dwAttrTo) ; pszPath : LPWSTR out -> "sptr" ; pszFrom : LPCWSTR -> "wstr" ; dwAttrFrom : DWORD -> "int" ; pszTo : LPCWSTR -> "wstr" ; dwAttrTo : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL PathRelativePathToW(LPWSTR pszPath, LPCWSTR pszFrom, DWORD dwAttrFrom, LPCWSTR pszTo, DWORD dwAttrTo) #uselib "SHLWAPI.dll" #cfunc global PathRelativePathToW "PathRelativePathToW" var, wstr, int, wstr, int ; res = PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo) ; pszPath : LPWSTR out -> "var" ; pszFrom : LPCWSTR -> "wstr" ; dwAttrFrom : DWORD -> "int" ; pszTo : LPCWSTR -> "wstr" ; dwAttrTo : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL PathRelativePathToW(LPWSTR pszPath, LPCWSTR pszFrom, DWORD dwAttrFrom, LPCWSTR pszTo, DWORD dwAttrTo) #uselib "SHLWAPI.dll" #cfunc global PathRelativePathToW "PathRelativePathToW" intptr, wstr, int, wstr, int ; res = PathRelativePathToW(varptr(pszPath), pszFrom, dwAttrFrom, pszTo, dwAttrTo) ; pszPath : LPWSTR out -> "intptr" ; pszFrom : LPCWSTR -> "wstr" ; dwAttrFrom : DWORD -> "int" ; pszTo : LPCWSTR -> "wstr" ; dwAttrTo : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procPathRelativePathToW = shlwapi.NewProc("PathRelativePathToW")
)
// pszPath (LPWSTR out), pszFrom (LPCWSTR), dwAttrFrom (DWORD), pszTo (LPCWSTR), dwAttrTo (DWORD)
r1, _, err := procPathRelativePathToW.Call(
uintptr(pszPath),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszFrom))),
uintptr(dwAttrFrom),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszTo))),
uintptr(dwAttrTo),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction PathRelativePathToW(
pszPath: PWideChar; // LPWSTR out
pszFrom: PWideChar; // LPCWSTR
dwAttrFrom: DWORD; // DWORD
pszTo: PWideChar; // LPCWSTR
dwAttrTo: DWORD // DWORD
): BOOL; stdcall;
external 'SHLWAPI.dll' name 'PathRelativePathToW';result := DllCall("SHLWAPI\PathRelativePathToW"
, "Ptr", pszPath ; LPWSTR out
, "WStr", pszFrom ; LPCWSTR
, "UInt", dwAttrFrom ; DWORD
, "WStr", pszTo ; LPCWSTR
, "UInt", dwAttrTo ; DWORD
, "Int") ; return: BOOL●PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo) = DLL("SHLWAPI.dll", "bool PathRelativePathToW(char*, char*, dword, char*, dword)")
# 呼び出し: PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo)
# pszPath : LPWSTR out -> "char*"
# pszFrom : LPCWSTR -> "char*"
# dwAttrFrom : DWORD -> "dword"
# pszTo : LPCWSTR -> "char*"
# dwAttrTo : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。