Win32 API 日本語リファレンス
ホームStorage.FileSystem › CreateHardLinkW

CreateHardLinkW

関数
既存ファイルへのハードリンクを作成する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL CreateHardLinkW(
    LPCWSTR lpFileName,
    LPCWSTR lpExistingFileName,
    SECURITY_ATTRIBUTES* lpSecurityAttributes   // optional
);

パラメーター

名前方向
lpFileNameLPCWSTRin
lpExistingFileNameLPCWSTRin
lpSecurityAttributesSECURITY_ATTRIBUTES*optional

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL CreateHardLinkW(
    LPCWSTR lpFileName,
    LPCWSTR lpExistingFileName,
    SECURITY_ATTRIBUTES* lpSecurityAttributes   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CreateHardLinkW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName,   // LPCWSTR
    IntPtr lpSecurityAttributes   // SECURITY_ATTRIBUTES* optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateHardLinkW(
    <MarshalAs(UnmanagedType.LPWStr)> lpFileName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> lpExistingFileName As String,   ' LPCWSTR
    lpSecurityAttributes As IntPtr   ' SECURITY_ATTRIBUTES* optional
) As Boolean
End Function
' lpFileName : LPCWSTR
' lpExistingFileName : LPCWSTR
' lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
Declare PtrSafe Function CreateHardLinkW Lib "kernel32" ( _
    ByVal lpFileName As LongPtr, _
    ByVal lpExistingFileName As LongPtr, _
    ByVal lpSecurityAttributes As LongPtr) 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

CreateHardLinkW = ctypes.windll.kernel32.CreateHardLinkW
CreateHardLinkW.restype = wintypes.BOOL
CreateHardLinkW.argtypes = [
    wintypes.LPCWSTR,  # lpFileName : LPCWSTR
    wintypes.LPCWSTR,  # lpExistingFileName : LPCWSTR
    ctypes.c_void_p,  # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CreateHardLinkW = Fiddle::Function.new(
  lib['CreateHardLinkW'],
  [
    Fiddle::TYPE_VOIDP,  # lpFileName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpExistingFileName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn CreateHardLinkW(
        lpFileName: *const u16,  // LPCWSTR
        lpExistingFileName: *const u16,  // LPCWSTR
        lpSecurityAttributes: *mut SECURITY_ATTRIBUTES  // SECURITY_ATTRIBUTES* optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CreateHardLinkW([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, IntPtr lpSecurityAttributes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateHardLinkW' -Namespace Win32 -PassThru
# $api::CreateHardLinkW(lpFileName, lpExistingFileName, lpSecurityAttributes)
#uselib "KERNEL32.dll"
#func global CreateHardLinkW "CreateHardLinkW" wptr, wptr, wptr
; CreateHardLinkW lpFileName, lpExistingFileName, varptr(lpSecurityAttributes)   ; 戻り値は stat
; lpFileName : LPCWSTR -> "wptr"
; lpExistingFileName : LPCWSTR -> "wptr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateHardLinkW "CreateHardLinkW" wstr, wstr, var
; res = CreateHardLinkW(lpFileName, lpExistingFileName, lpSecurityAttributes)
; lpFileName : LPCWSTR -> "wstr"
; lpExistingFileName : LPCWSTR -> "wstr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName, SECURITY_ATTRIBUTES* lpSecurityAttributes)
#uselib "KERNEL32.dll"
#cfunc global CreateHardLinkW "CreateHardLinkW" wstr, wstr, var
; res = CreateHardLinkW(lpFileName, lpExistingFileName, lpSecurityAttributes)
; lpFileName : LPCWSTR -> "wstr"
; lpExistingFileName : LPCWSTR -> "wstr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateHardLinkW = kernel32.NewProc("CreateHardLinkW")
)

// lpFileName (LPCWSTR), lpExistingFileName (LPCWSTR), lpSecurityAttributes (SECURITY_ATTRIBUTES* optional)
r1, _, err := procCreateHardLinkW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFileName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpExistingFileName))),
	uintptr(lpSecurityAttributes),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CreateHardLinkW(
  lpFileName: PWideChar;   // LPCWSTR
  lpExistingFileName: PWideChar;   // LPCWSTR
  lpSecurityAttributes: Pointer   // SECURITY_ATTRIBUTES* optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'CreateHardLinkW';
result := DllCall("KERNEL32\CreateHardLinkW"
    , "WStr", lpFileName   ; LPCWSTR
    , "WStr", lpExistingFileName   ; LPCWSTR
    , "Ptr", lpSecurityAttributes   ; SECURITY_ATTRIBUTES* optional
    , "Int")   ; return: BOOL
●CreateHardLinkW(lpFileName, lpExistingFileName, lpSecurityAttributes) = DLL("KERNEL32.dll", "bool CreateHardLinkW(char*, char*, void*)")
# 呼び出し: CreateHardLinkW(lpFileName, lpExistingFileName, lpSecurityAttributes)
# lpFileName : LPCWSTR -> "char*"
# lpExistingFileName : LPCWSTR -> "char*"
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。