StrCatBuffW
関数バッファサイズを超えないように文字列を連結する。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
LPWSTR StrCatBuffW(
LPWSTR pszDest,
LPCWSTR pszSrc,
INT cchDestBuffSize
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszDest | LPWSTR | inout | null で終わる文字列へのポインター。この関数が正常に返ると、この文字列には元の内容に文字列 pszSrc が追加された内容が格納されます。 |
| pszSrc | LPCWSTR | in | pszDest に追加する文字列へのポインター。 |
| cchDestBuffSize | INT | in | pszDest が指すバッファーのサイズ(文字数)。この値は、結合後の文字列の長さに終端の null 文字を加えた長さ以上である必要があります。バッファーが文字列全体を格納するには小さすぎる場合、文字列は切り詰められます。 |
戻り値の型: LPWSTR
公式ドキュメント
ある文字列の文字をコピーし、別の文字列の末尾に追加します。(Unicode)
戻り値
型: PTSTR
コピー先の文字列へのポインターを返します。
解説(Remarks)
セキュリティに関する警告: この関数を誤って使用すると、アプリケーションのセキュリティが損なわれる可能性があります。最終的な文字列が null で終端されている保証はありません。次のいずれかの代替手段の使用を検討してください: StringCbCat、StringCbCatEx、StringCbCatN、StringCbCatNEx、StringCchCat、StringCchCatEx、StringCchCatN、または StringCchCatNEx。続行する前に、セキュリティに関する考慮事項: Microsoft Windows シェルを確認してください。
メモ
shlwapi.h ヘッダーは StrCatBuff を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不一致が生じてコンパイル エラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は 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 (Unicode / -W)
#include <windows.h>
LPWSTR StrCatBuffW(
LPWSTR pszDest,
LPCWSTR pszSrc,
INT cchDestBuffSize
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr StrCatBuffW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDest, // LPWSTR in/out
[MarshalAs(UnmanagedType.LPWStr)] string pszSrc, // LPCWSTR
int cchDestBuffSize // INT
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function StrCatBuffW(
<MarshalAs(UnmanagedType.LPWStr)> pszDest As System.Text.StringBuilder, ' LPWSTR in/out
<MarshalAs(UnmanagedType.LPWStr)> pszSrc As String, ' LPCWSTR
cchDestBuffSize As Integer ' INT
) As IntPtr
End Function' pszDest : LPWSTR in/out
' pszSrc : LPCWSTR
' cchDestBuffSize : INT
Declare PtrSafe Function StrCatBuffW Lib "shlwapi" ( _
ByVal pszDest As LongPtr, _
ByVal pszSrc As LongPtr, _
ByVal cchDestBuffSize As Long) As LongPtr
' 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
StrCatBuffW = ctypes.windll.shlwapi.StrCatBuffW
StrCatBuffW.restype = wintypes.LPWSTR
StrCatBuffW.argtypes = [
wintypes.LPWSTR, # pszDest : LPWSTR in/out
wintypes.LPCWSTR, # pszSrc : LPCWSTR
ctypes.c_int, # cchDestBuffSize : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
StrCatBuffW = Fiddle::Function.new(
lib['StrCatBuffW'],
[
Fiddle::TYPE_VOIDP, # pszDest : LPWSTR in/out
Fiddle::TYPE_VOIDP, # pszSrc : LPCWSTR
Fiddle::TYPE_INT, # cchDestBuffSize : INT
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn StrCatBuffW(
pszDest: *mut u16, // LPWSTR in/out
pszSrc: *const u16, // LPCWSTR
cchDestBuffSize: i32 // INT
) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr StrCatBuffW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDest, [MarshalAs(UnmanagedType.LPWStr)] string pszSrc, int cchDestBuffSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrCatBuffW' -Namespace Win32 -PassThru
# $api::StrCatBuffW(pszDest, pszSrc, cchDestBuffSize)#uselib "SHLWAPI.dll"
#func global StrCatBuffW "StrCatBuffW" wptr, wptr, wptr
; StrCatBuffW varptr(pszDest), pszSrc, cchDestBuffSize ; 戻り値は stat
; pszDest : LPWSTR in/out -> "wptr"
; pszSrc : LPCWSTR -> "wptr"
; cchDestBuffSize : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global StrCatBuffW "StrCatBuffW" var, wstr, int ; res = StrCatBuffW(pszDest, pszSrc, cchDestBuffSize) ; pszDest : LPWSTR in/out -> "var" ; pszSrc : LPCWSTR -> "wstr" ; cchDestBuffSize : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global StrCatBuffW "StrCatBuffW" sptr, wstr, int ; res = StrCatBuffW(varptr(pszDest), pszSrc, cchDestBuffSize) ; pszDest : LPWSTR in/out -> "sptr" ; pszSrc : LPCWSTR -> "wstr" ; cchDestBuffSize : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; LPWSTR StrCatBuffW(LPWSTR pszDest, LPCWSTR pszSrc, INT cchDestBuffSize) #uselib "SHLWAPI.dll" #cfunc global StrCatBuffW "StrCatBuffW" var, wstr, int ; res = StrCatBuffW(pszDest, pszSrc, cchDestBuffSize) ; pszDest : LPWSTR in/out -> "var" ; pszSrc : LPCWSTR -> "wstr" ; cchDestBuffSize : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; LPWSTR StrCatBuffW(LPWSTR pszDest, LPCWSTR pszSrc, INT cchDestBuffSize) #uselib "SHLWAPI.dll" #cfunc global StrCatBuffW "StrCatBuffW" intptr, wstr, int ; res = StrCatBuffW(varptr(pszDest), pszSrc, cchDestBuffSize) ; pszDest : LPWSTR in/out -> "intptr" ; pszSrc : LPCWSTR -> "wstr" ; cchDestBuffSize : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procStrCatBuffW = shlwapi.NewProc("StrCatBuffW")
)
// pszDest (LPWSTR in/out), pszSrc (LPCWSTR), cchDestBuffSize (INT)
r1, _, err := procStrCatBuffW.Call(
uintptr(pszDest),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszSrc))),
uintptr(cchDestBuffSize),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPWSTRfunction StrCatBuffW(
pszDest: PWideChar; // LPWSTR in/out
pszSrc: PWideChar; // LPCWSTR
cchDestBuffSize: Integer // INT
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrCatBuffW';result := DllCall("SHLWAPI\StrCatBuffW"
, "Ptr", pszDest ; LPWSTR in/out
, "WStr", pszSrc ; LPCWSTR
, "Int", cchDestBuffSize ; INT
, "Ptr") ; return: LPWSTR●StrCatBuffW(pszDest, pszSrc, cchDestBuffSize) = DLL("SHLWAPI.dll", "char* StrCatBuffW(char*, char*, int)")
# 呼び出し: StrCatBuffW(pszDest, pszSrc, cchDestBuffSize)
# pszDest : LPWSTR in/out -> "char*"
# pszSrc : LPCWSTR -> "char*"
# cchDestBuffSize : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "shlwapi" fn StrCatBuffW(
pszDest: [*c]u16, // LPWSTR in/out
pszSrc: [*c]const u16, // LPCWSTR
cchDestBuffSize: i32 // INT
) callconv(std.os.windows.WINAPI) [*c]const u16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc StrCatBuffW(
pszDest: ptr uint16, # LPWSTR in/out
pszSrc: WideCString, # LPCWSTR
cchDestBuffSize: int32 # INT
): WideCString {.importc: "StrCatBuffW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* StrCatBuffW(
wchar* pszDest, // LPWSTR in/out
const(wchar)* pszSrc, // LPCWSTR
int cchDestBuffSize // INT
);ccall((:StrCatBuffW, "SHLWAPI.dll"), stdcall, Cwstring,
(Ptr{UInt16}, Cwstring, Int32),
pszDest, pszSrc, cchDestBuffSize)
# pszDest : LPWSTR in/out -> Ptr{UInt16}
# pszSrc : LPCWSTR -> Cwstring
# cchDestBuffSize : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
const uint16_t* StrCatBuffW(
uint16_t* pszDest,
const uint16_t* pszSrc,
int32_t cchDestBuffSize);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrCatBuffW(pszDest, pszSrc, cchDestBuffSize)
-- pszDest : LPWSTR in/out
-- pszSrc : LPCWSTR
-- cchDestBuffSize : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrCatBuffW = lib.func('__stdcall', 'StrCatBuffW', 'void *', ['uint16_t *', 'str16', 'int32_t']);
// StrCatBuffW(pszDest, pszSrc, cchDestBuffSize)
// pszDest : LPWSTR in/out -> 'uint16_t *'
// pszSrc : LPCWSTR -> 'str16'
// cchDestBuffSize : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
StrCatBuffW: { parameters: ["buffer", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.StrCatBuffW(pszDest, pszSrc, cchDestBuffSize)
// pszDest : LPWSTR in/out -> "buffer"
// pszSrc : LPCWSTR -> "buffer"
// cchDestBuffSize : INT -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const uint16_t* StrCatBuffW(
uint16_t* pszDest,
const uint16_t* pszSrc,
int32_t cchDestBuffSize);
C, "SHLWAPI.dll");
// $ffi->StrCatBuffW(pszDest, pszSrc, cchDestBuffSize);
// pszDest : LPWSTR in/out
// pszSrc : LPCWSTR
// cchDestBuffSize : INT
// 構造体/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, W32APIOptions.UNICODE_OPTIONS);
Pointer StrCatBuffW(
char[] pszDest, // LPWSTR in/out
WString pszSrc, // LPCWSTR
int cchDestBuffSize // INT
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shlwapi")]
lib LibSHLWAPI
fun StrCatBuffW = StrCatBuffW(
pszDest : UInt16*, # LPWSTR in/out
pszSrc : UInt16*, # LPCWSTR
cchDestBuffSize : Int32 # INT
) : UInt16*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef StrCatBuffWNative = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, Int32);
typedef StrCatBuffWDart = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, int);
final StrCatBuffW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<StrCatBuffWNative, StrCatBuffWDart>('StrCatBuffW');
// pszDest : LPWSTR in/out -> Pointer<Utf16>
// pszSrc : LPCWSTR -> Pointer<Utf16>
// cchDestBuffSize : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function StrCatBuffW(
pszDest: PWideChar; // LPWSTR in/out
pszSrc: PWideChar; // LPCWSTR
cchDestBuffSize: Integer // INT
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrCatBuffW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "StrCatBuffW"
c_StrCatBuffW :: CWString -> CWString -> Int32 -> IO CWString
-- pszDest : LPWSTR in/out -> CWString
-- pszSrc : LPCWSTR -> CWString
-- cchDestBuffSize : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let strcatbuffw =
foreign "StrCatBuffW"
((ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> returning (ptr uint16_t))
(* pszDest : LPWSTR in/out -> (ptr uint16_t) *)
(* pszSrc : LPCWSTR -> (ptr uint16_t) *)
(* cchDestBuffSize : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("StrCatBuffW" str-cat-buff-w :convention :stdcall) (:string :encoding :utf-16le)
(psz-dest :pointer) ; LPWSTR in/out
(psz-src (:string :encoding :utf-16le)) ; LPCWSTR
(cch-dest-buff-size :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $StrCatBuffW = Win32::API::More->new('SHLWAPI',
'LPWSTR StrCatBuffW(LPWSTR pszDest, LPCWSTR pszSrc, int cchDestBuffSize)');
# my $ret = $StrCatBuffW->Call($pszDest, $pszSrc, $cchDestBuffSize);
# pszDest : LPWSTR in/out -> LPWSTR
# pszSrc : LPCWSTR -> LPCWSTR
# cchDestBuffSize : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f StrCatBuffA (ANSI版) — バッファサイズを超えないように文字列を連結する。