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

CreateHardLinkA

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

シグネチャ

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

BOOL CreateHardLinkA(
    LPCSTR lpFileName,
    LPCSTR lpExistingFileName,
    SECURITY_ATTRIBUTES* lpSecurityAttributes   // optional
);

パラメーター

名前方向
lpFileNameLPCSTRin
lpExistingFileNameLPCSTRin
lpSecurityAttributesSECURITY_ATTRIBUTES*optional

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL CreateHardLinkA(
    LPCSTR lpFileName,
    LPCSTR lpExistingFileName,
    SECURITY_ATTRIBUTES* lpSecurityAttributes   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool CreateHardLinkA(
    [MarshalAs(UnmanagedType.LPStr)] string lpFileName,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string lpExistingFileName,   // LPCSTR
    IntPtr lpSecurityAttributes   // SECURITY_ATTRIBUTES* optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateHardLinkA(
    <MarshalAs(UnmanagedType.LPStr)> lpFileName As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> lpExistingFileName As String,   ' LPCSTR
    lpSecurityAttributes As IntPtr   ' SECURITY_ATTRIBUTES* optional
) As Boolean
End Function
' lpFileName : LPCSTR
' lpExistingFileName : LPCSTR
' lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
Declare PtrSafe Function CreateHardLinkA Lib "kernel32" ( _
    ByVal lpFileName As String, _
    ByVal lpExistingFileName As String, _
    ByVal lpSecurityAttributes As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateHardLinkA = ctypes.windll.kernel32.CreateHardLinkA
CreateHardLinkA.restype = wintypes.BOOL
CreateHardLinkA.argtypes = [
    wintypes.LPCSTR,  # lpFileName : LPCSTR
    wintypes.LPCSTR,  # lpExistingFileName : LPCSTR
    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')
CreateHardLinkA = Fiddle::Function.new(
  lib['CreateHardLinkA'],
  [
    Fiddle::TYPE_VOIDP,  # lpFileName : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpExistingFileName : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn CreateHardLinkA(
        lpFileName: *const u8,  // LPCSTR
        lpExistingFileName: *const u8,  // LPCSTR
        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.Ansi, SetLastError = true)]
public static extern bool CreateHardLinkA([MarshalAs(UnmanagedType.LPStr)] string lpFileName, [MarshalAs(UnmanagedType.LPStr)] string lpExistingFileName, IntPtr lpSecurityAttributes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateHardLinkA' -Namespace Win32 -PassThru
# $api::CreateHardLinkA(lpFileName, lpExistingFileName, lpSecurityAttributes)
#uselib "KERNEL32.dll"
#func global CreateHardLinkA "CreateHardLinkA" sptr, sptr, sptr
; CreateHardLinkA lpFileName, lpExistingFileName, varptr(lpSecurityAttributes)   ; 戻り値は stat
; lpFileName : LPCSTR -> "sptr"
; lpExistingFileName : LPCSTR -> "sptr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateHardLinkA "CreateHardLinkA" str, str, var
; res = CreateHardLinkA(lpFileName, lpExistingFileName, lpSecurityAttributes)
; lpFileName : LPCSTR -> "str"
; lpExistingFileName : LPCSTR -> "str"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName, SECURITY_ATTRIBUTES* lpSecurityAttributes)
#uselib "KERNEL32.dll"
#cfunc global CreateHardLinkA "CreateHardLinkA" str, str, var
; res = CreateHardLinkA(lpFileName, lpExistingFileName, lpSecurityAttributes)
; lpFileName : LPCSTR -> "str"
; lpExistingFileName : LPCSTR -> "str"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateHardLinkA = kernel32.NewProc("CreateHardLinkA")
)

// lpFileName (LPCSTR), lpExistingFileName (LPCSTR), lpSecurityAttributes (SECURITY_ATTRIBUTES* optional)
r1, _, err := procCreateHardLinkA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFileName))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpExistingFileName))),
	uintptr(lpSecurityAttributes),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CreateHardLinkA(
  lpFileName: PAnsiChar;   // LPCSTR
  lpExistingFileName: PAnsiChar;   // LPCSTR
  lpSecurityAttributes: Pointer   // SECURITY_ATTRIBUTES* optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'CreateHardLinkA';
result := DllCall("KERNEL32\CreateHardLinkA"
    , "AStr", lpFileName   ; LPCSTR
    , "AStr", lpExistingFileName   ; LPCSTR
    , "Ptr", lpSecurityAttributes   ; SECURITY_ATTRIBUTES* optional
    , "Int")   ; return: BOOL
●CreateHardLinkA(lpFileName, lpExistingFileName, lpSecurityAttributes) = DLL("KERNEL32.dll", "bool CreateHardLinkA(char*, char*, void*)")
# 呼び出し: CreateHardLinkA(lpFileName, lpExistingFileName, lpSecurityAttributes)
# lpFileName : LPCSTR -> "char*"
# lpExistingFileName : LPCSTR -> "char*"
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。