StrCpyW
関数文字列を別のバッファにコピーする。
シグネチャ
// SHLWAPI.dll
#include <windows.h>
LPWSTR StrCpyW(
LPWSTR psz1,
LPCWSTR psz2
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| psz1 | LPWSTR | out | この関数が正常に終了したときに、コピーされた文字列を受け取るバッファへのポインター。この文字列は null 終端であることが保証されません。 |
| psz2 | LPCWSTR | in | null 終端のコピー元文字列へのポインター。 |
戻り値の型: LPWSTR
公式ドキュメント
ある文字列を別の文字列にコピーします。(StrCpyW)
戻り値
型: PTSTR
psz1 へのポインターを返します。
解説(Remarks)
セキュリティに関する警告: この関数を誤って使用すると、アプリケーションのセキュリティが損なわれる可能性があります。第 1 引数 psz1 は、psz2 と終端の '\0' を保持できる十分な大きさである必要があります。そうでない場合、バッファー オーバーランが発生する可能性があります。バッファー オーバーランは、アクセス違反が発生するとアプリケーションに対するサービス拒否攻撃につながる可能性があります。最悪の場合、特に psz1 がスタックベースのバッファーであるときは、バッファー オーバーランによって攻撃者がプロセスに実行可能なコードを挿入できる可能性があります。次のいずれかの代替手段の使用を検討してください: StringCbCopy、StringCbCopyEx、StringCbCopyN、StringCbCopyNEx、StringCchCopy、StringCchCopyEx、StringCchCopyN、または StringCchCopyNEx。続行する前に、セキュリティに関する考慮事項: Microsoft Windows シェル を確認してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は 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
#include <windows.h>
LPWSTR StrCpyW(
LPWSTR psz1,
LPCWSTR psz2
);[DllImport("SHLWAPI.dll", ExactSpelling = true)]
static extern IntPtr StrCpyW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder psz1, // LPWSTR out
[MarshalAs(UnmanagedType.LPWStr)] string psz2 // LPCWSTR
);<DllImport("SHLWAPI.dll", ExactSpelling:=True)>
Public Shared Function StrCpyW(
<MarshalAs(UnmanagedType.LPWStr)> psz1 As System.Text.StringBuilder, ' LPWSTR out
<MarshalAs(UnmanagedType.LPWStr)> psz2 As String ' LPCWSTR
) As IntPtr
End Function' psz1 : LPWSTR out
' psz2 : LPCWSTR
Declare PtrSafe Function StrCpyW Lib "shlwapi" ( _
ByVal psz1 As LongPtr, _
ByVal psz2 As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
StrCpyW = ctypes.windll.shlwapi.StrCpyW
StrCpyW.restype = wintypes.LPWSTR
StrCpyW.argtypes = [
wintypes.LPWSTR, # psz1 : LPWSTR out
wintypes.LPCWSTR, # psz2 : LPCWSTR
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
StrCpyW = Fiddle::Function.new(
lib['StrCpyW'],
[
Fiddle::TYPE_VOIDP, # psz1 : LPWSTR out
Fiddle::TYPE_VOIDP, # psz2 : LPCWSTR
],
Fiddle::TYPE_VOIDP)#[link(name = "shlwapi")]
extern "system" {
fn StrCpyW(
psz1: *mut u16, // LPWSTR out
psz2: *const u16 // LPCWSTR
) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll")]
public static extern IntPtr StrCpyW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder psz1, [MarshalAs(UnmanagedType.LPWStr)] string psz2);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrCpyW' -Namespace Win32 -PassThru
# $api::StrCpyW(psz1, psz2)#uselib "SHLWAPI.dll"
#func global StrCpyW "StrCpyW" sptr, sptr
; StrCpyW varptr(psz1), psz2 ; 戻り値は stat
; psz1 : LPWSTR out -> "sptr"
; psz2 : LPCWSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global StrCpyW "StrCpyW" var, wstr ; res = StrCpyW(psz1, psz2) ; psz1 : LPWSTR out -> "var" ; psz2 : LPCWSTR -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global StrCpyW "StrCpyW" sptr, wstr ; res = StrCpyW(varptr(psz1), psz2) ; psz1 : LPWSTR out -> "sptr" ; psz2 : LPCWSTR -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; LPWSTR StrCpyW(LPWSTR psz1, LPCWSTR psz2) #uselib "SHLWAPI.dll" #cfunc global StrCpyW "StrCpyW" var, wstr ; res = StrCpyW(psz1, psz2) ; psz1 : LPWSTR out -> "var" ; psz2 : LPCWSTR -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; LPWSTR StrCpyW(LPWSTR psz1, LPCWSTR psz2) #uselib "SHLWAPI.dll" #cfunc global StrCpyW "StrCpyW" intptr, wstr ; res = StrCpyW(varptr(psz1), psz2) ; psz1 : LPWSTR out -> "intptr" ; psz2 : LPCWSTR -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procStrCpyW = shlwapi.NewProc("StrCpyW")
)
// psz1 (LPWSTR out), psz2 (LPCWSTR)
r1, _, err := procStrCpyW.Call(
uintptr(psz1),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(psz2))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPWSTRfunction StrCpyW(
psz1: PWideChar; // LPWSTR out
psz2: PWideChar // LPCWSTR
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrCpyW';result := DllCall("SHLWAPI\StrCpyW"
, "Ptr", psz1 ; LPWSTR out
, "WStr", psz2 ; LPCWSTR
, "Ptr") ; return: LPWSTR●StrCpyW(psz1, psz2) = DLL("SHLWAPI.dll", "char* StrCpyW(char*, char*)")
# 呼び出し: StrCpyW(psz1, psz2)
# psz1 : LPWSTR out -> "char*"
# psz2 : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn StrCpyW(
psz1: [*c]u16, // LPWSTR out
psz2: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) [*c]const u16;proc StrCpyW(
psz1: ptr uint16, # LPWSTR out
psz2: WideCString # LPCWSTR
): WideCString {.importc: "StrCpyW", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* StrCpyW(
wchar* psz1, // LPWSTR out
const(wchar)* psz2 // LPCWSTR
);ccall((:StrCpyW, "SHLWAPI.dll"), stdcall, Cwstring,
(Ptr{UInt16}, Cwstring),
psz1, psz2)
# psz1 : LPWSTR out -> Ptr{UInt16}
# psz2 : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
const uint16_t* StrCpyW(
uint16_t* psz1,
const uint16_t* psz2);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrCpyW(psz1, psz2)
-- psz1 : LPWSTR out
-- psz2 : LPCWSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrCpyW = lib.func('__stdcall', 'StrCpyW', 'void *', ['uint16_t *', 'str16']);
// StrCpyW(psz1, psz2)
// psz1 : LPWSTR out -> 'uint16_t *'
// psz2 : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
StrCpyW: { parameters: ["buffer", "buffer"], result: "pointer" },
});
// lib.symbols.StrCpyW(psz1, psz2)
// psz1 : LPWSTR out -> "buffer"
// psz2 : LPCWSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const uint16_t* StrCpyW(
uint16_t* psz1,
const uint16_t* psz2);
C, "SHLWAPI.dll");
// $ffi->StrCpyW(psz1, psz2);
// psz1 : LPWSTR out
// psz2 : LPCWSTR
// 構造体/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);
Pointer StrCpyW(
char[] psz1, // LPWSTR out
WString psz2 // LPCWSTR
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun StrCpyW = StrCpyW(
psz1 : UInt16*, # LPWSTR out
psz2 : UInt16* # LPCWSTR
) : UInt16*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef StrCpyWNative = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>);
typedef StrCpyWDart = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>);
final StrCpyW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<StrCpyWNative, StrCpyWDart>('StrCpyW');
// psz1 : LPWSTR out -> Pointer<Utf16>
// psz2 : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function StrCpyW(
psz1: PWideChar; // LPWSTR out
psz2: PWideChar // LPCWSTR
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrCpyW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "StrCpyW"
c_StrCpyW :: CWString -> CWString -> IO CWString
-- psz1 : LPWSTR out -> CWString
-- psz2 : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let strcpyw =
foreign "StrCpyW"
((ptr uint16_t) @-> (ptr uint16_t) @-> returning (ptr uint16_t))
(* psz1 : LPWSTR out -> (ptr uint16_t) *)
(* psz2 : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("StrCpyW" str-cpy-w :convention :stdcall) (:string :encoding :utf-16le)
(psz1 :pointer) ; LPWSTR out
(psz2 (:string :encoding :utf-16le))) ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $StrCpyW = Win32::API::More->new('SHLWAPI',
'LPWSTR StrCpyW(LPWSTR psz1, LPCWSTR psz2)');
# my $ret = $StrCpyW->Call($psz1, $psz2);
# psz1 : LPWSTR out -> LPWSTR
# psz2 : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。