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

CopyFileW

関数
既存ファイルを新しいファイルへコピーする(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CopyFileW(
    LPCWSTR lpExistingFileName,
    LPCWSTR lpNewFileName,
    BOOL bFailIfExists
);

パラメーター

名前方向
lpExistingFileNameLPCWSTRin
lpNewFileNameLPCWSTRin
bFailIfExistsBOOLin

戻り値の型: BOOL

各言語での呼び出し定義

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

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

CopyFileW = ctypes.windll.kernel32.CopyFileW
CopyFileW.restype = wintypes.BOOL
CopyFileW.argtypes = [
    wintypes.LPCWSTR,  # lpExistingFileName : LPCWSTR
    wintypes.LPCWSTR,  # lpNewFileName : LPCWSTR
    wintypes.BOOL,  # bFailIfExists : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CopyFileW = Fiddle::Function.new(
  lib['CopyFileW'],
  [
    Fiddle::TYPE_VOIDP,  # lpExistingFileName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # lpNewFileName : LPCWSTR
    Fiddle::TYPE_INT,  # bFailIfExists : BOOL
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn CopyFileW(
        lpExistingFileName: *const u16,  // LPCWSTR
        lpNewFileName: *const u16,  // LPCWSTR
        bFailIfExists: i32  // BOOL
    ) -> 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 CopyFileW([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, bool bFailIfExists);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CopyFileW' -Namespace Win32 -PassThru
# $api::CopyFileW(lpExistingFileName, lpNewFileName, bFailIfExists)
#uselib "KERNEL32.dll"
#func global CopyFileW "CopyFileW" wptr, wptr, wptr
; CopyFileW lpExistingFileName, lpNewFileName, bFailIfExists   ; 戻り値は stat
; lpExistingFileName : LPCWSTR -> "wptr"
; lpNewFileName : LPCWSTR -> "wptr"
; bFailIfExists : BOOL -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global CopyFileW "CopyFileW" wstr, wstr, int
; res = CopyFileW(lpExistingFileName, lpNewFileName, bFailIfExists)
; lpExistingFileName : LPCWSTR -> "wstr"
; lpNewFileName : LPCWSTR -> "wstr"
; bFailIfExists : BOOL -> "int"
; BOOL CopyFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, BOOL bFailIfExists)
#uselib "KERNEL32.dll"
#cfunc global CopyFileW "CopyFileW" wstr, wstr, int
; res = CopyFileW(lpExistingFileName, lpNewFileName, bFailIfExists)
; lpExistingFileName : LPCWSTR -> "wstr"
; lpNewFileName : LPCWSTR -> "wstr"
; bFailIfExists : BOOL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCopyFileW = kernel32.NewProc("CopyFileW")
)

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