Win32 API 日本語リファレンス
ホームUI.Shell › StrNCatW

StrNCatW

関数
指定最大長まで文字列を連結する。
DLLSHLWAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

LPWSTR StrNCatW(
    LPWSTR psz1,
    LPCWSTR psz2,
    INT cchMax
);

パラメーター

名前方向説明
psz1LPWSTRinoutこの関数が psz2 の文字を追加する先となる、null 終端文字列へのポインターです。結合後の文字列に終端の null 文字を加えたものを格納できるだけの十分な大きさが必要です。
psz2LPCWSTRin追加される null 終端文字列へのポインターです。
cchMaxINTinpsz2 の先頭から psz1 に追加する文字数です。

戻り値の型: LPWSTR

公式ドキュメント

ある文字列の先頭から指定された数の文字を、別の文字列の末尾に追加します。(Unicode)

戻り値

型: PTSTR

結合後の文字列を保持する psz1 へのポインターを返します。

解説(Remarks)

セキュリティに関する警告: この関数を誤って使用すると、アプリケーションのセキュリティが損なわれるおそれがあります。第 1 引数 psz1psz2 と終端の '\0' を格納できるだけの十分な大きさが必要です。そうでない場合、バッファー オーバーランが発生する可能性があります。バッファー オーバーランは、アクセス違反が発生した場合にアプリケーションに対するサービス拒否攻撃につながる可能性があります。最悪の場合、特に psz1 がスタック ベースのバッファーであるときには、攻撃者が実行可能コードをプロセスに注入できる可能性があります。最後の引数 cchMaxpsz1 にコピーする文字数であり、必ずしも psz1 のバイト単位のサイズではないことに注意してください。次のいずれかの代替手段の使用を検討してください。StringCbCatStringCbCatExStringCbCatNStringCbCatNExStringCchCatStringCchCatExStringCchCatN、または StringCchCatNEx です。続行する前に セキュリティに関する考慮事項: Microsoft Windows シェル を確認してください。

メモ

shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして StrNCat を定義しています。エンコーディング ニュートラルなエイリアスの使用と、エンコーディング ニュートラルでないコードを混在させると、不一致が生じてコンパイル エラーまたは実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

LPWSTR StrNCatW(
    LPWSTR psz1,
    LPCWSTR psz2,
    INT cchMax
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr StrNCatW(
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder psz1,   // LPWSTR in/out
    [MarshalAs(UnmanagedType.LPWStr)] string psz2,   // LPCWSTR
    int cchMax   // INT
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function StrNCatW(
    <MarshalAs(UnmanagedType.LPWStr)> psz1 As System.Text.StringBuilder,   ' LPWSTR in/out
    <MarshalAs(UnmanagedType.LPWStr)> psz2 As String,   ' LPCWSTR
    cchMax As Integer   ' INT
) As IntPtr
End Function
' psz1 : LPWSTR in/out
' psz2 : LPCWSTR
' cchMax : INT
Declare PtrSafe Function StrNCatW Lib "shlwapi" ( _
    ByVal psz1 As LongPtr, _
    ByVal psz2 As LongPtr, _
    ByVal cchMax 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

StrNCatW = ctypes.windll.shlwapi.StrNCatW
StrNCatW.restype = wintypes.LPWSTR
StrNCatW.argtypes = [
    wintypes.LPWSTR,  # psz1 : LPWSTR in/out
    wintypes.LPCWSTR,  # psz2 : LPCWSTR
    ctypes.c_int,  # cchMax : INT
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
StrNCatW = Fiddle::Function.new(
  lib['StrNCatW'],
  [
    Fiddle::TYPE_VOIDP,  # psz1 : LPWSTR in/out
    Fiddle::TYPE_VOIDP,  # psz2 : LPCWSTR
    Fiddle::TYPE_INT,  # cchMax : INT
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "shlwapi")]
extern "system" {
    fn StrNCatW(
        psz1: *mut u16,  // LPWSTR in/out
        psz2: *const u16,  // LPCWSTR
        cchMax: 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 StrNCatW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder psz1, [MarshalAs(UnmanagedType.LPWStr)] string psz2, int cchMax);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrNCatW' -Namespace Win32 -PassThru
# $api::StrNCatW(psz1, psz2, cchMax)
#uselib "SHLWAPI.dll"
#func global StrNCatW "StrNCatW" wptr, wptr, wptr
; StrNCatW varptr(psz1), psz2, cchMax   ; 戻り値は stat
; psz1 : LPWSTR in/out -> "wptr"
; psz2 : LPCWSTR -> "wptr"
; cchMax : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHLWAPI.dll"
#cfunc global StrNCatW "StrNCatW" var, wstr, int
; res = StrNCatW(psz1, psz2, cchMax)
; psz1 : LPWSTR in/out -> "var"
; psz2 : LPCWSTR -> "wstr"
; cchMax : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; LPWSTR StrNCatW(LPWSTR psz1, LPCWSTR psz2, INT cchMax)
#uselib "SHLWAPI.dll"
#cfunc global StrNCatW "StrNCatW" var, wstr, int
; res = StrNCatW(psz1, psz2, cchMax)
; psz1 : LPWSTR in/out -> "var"
; psz2 : LPCWSTR -> "wstr"
; cchMax : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procStrNCatW = shlwapi.NewProc("StrNCatW")
)

// psz1 (LPWSTR in/out), psz2 (LPCWSTR), cchMax (INT)
r1, _, err := procStrNCatW.Call(
	uintptr(psz1),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(psz2))),
	uintptr(cchMax),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // LPWSTR
function StrNCatW(
  psz1: PWideChar;   // LPWSTR in/out
  psz2: PWideChar;   // LPCWSTR
  cchMax: Integer   // INT
): PWideChar; stdcall;
  external 'SHLWAPI.dll' name 'StrNCatW';
result := DllCall("SHLWAPI\StrNCatW"
    , "Ptr", psz1   ; LPWSTR in/out
    , "WStr", psz2   ; LPCWSTR
    , "Int", cchMax   ; INT
    , "Ptr")   ; return: LPWSTR
●StrNCatW(psz1, psz2, cchMax) = DLL("SHLWAPI.dll", "char* StrNCatW(char*, char*, int)")
# 呼び出し: StrNCatW(psz1, psz2, cchMax)
# psz1 : LPWSTR in/out -> "char*"
# psz2 : LPCWSTR -> "char*"
# cchMax : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "shlwapi" fn StrNCatW(
    psz1: [*c]u16, // LPWSTR in/out
    psz2: [*c]const u16, // LPCWSTR
    cchMax: i32 // INT
) callconv(std.os.windows.WINAPI) [*c]const u16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc StrNCatW(
    psz1: ptr uint16,  # LPWSTR in/out
    psz2: WideCString,  # LPCWSTR
    cchMax: int32  # INT
): WideCString {.importc: "StrNCatW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* StrNCatW(
    wchar* psz1,   // LPWSTR in/out
    const(wchar)* psz2,   // LPCWSTR
    int cchMax   // INT
);
ccall((:StrNCatW, "SHLWAPI.dll"), stdcall, Cwstring,
      (Ptr{UInt16}, Cwstring, Int32),
      psz1, psz2, cchMax)
# psz1 : LPWSTR in/out -> Ptr{UInt16}
# psz2 : LPCWSTR -> Cwstring
# cchMax : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
const uint16_t* StrNCatW(
    uint16_t* psz1,
    const uint16_t* psz2,
    int32_t cchMax);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrNCatW(psz1, psz2, cchMax)
-- psz1 : LPWSTR in/out
-- psz2 : LPCWSTR
-- cchMax : 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 StrNCatW = lib.func('__stdcall', 'StrNCatW', 'void *', ['uint16_t *', 'str16', 'int32_t']);
// StrNCatW(psz1, psz2, cchMax)
// psz1 : LPWSTR in/out -> 'uint16_t *'
// psz2 : LPCWSTR -> 'str16'
// cchMax : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  StrNCatW: { parameters: ["buffer", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.StrNCatW(psz1, psz2, cchMax)
// psz1 : LPWSTR in/out -> "buffer"
// psz2 : LPCWSTR -> "buffer"
// cchMax : INT -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
const uint16_t* StrNCatW(
    uint16_t* psz1,
    const uint16_t* psz2,
    int32_t cchMax);
C, "SHLWAPI.dll");
// $ffi->StrNCatW(psz1, psz2, cchMax);
// psz1 : LPWSTR in/out
// psz2 : LPCWSTR
// cchMax : 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 StrNCatW(
        char[] psz1,   // LPWSTR in/out
        WString psz2,   // LPCWSTR
        int cchMax   // INT
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shlwapi")]
lib LibSHLWAPI
  fun StrNCatW = StrNCatW(
    psz1 : UInt16*,   # LPWSTR in/out
    psz2 : UInt16*,   # LPCWSTR
    cchMax : 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 StrNCatWNative = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, Int32);
typedef StrNCatWDart = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, int);
final StrNCatW = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<StrNCatWNative, StrNCatWDart>('StrNCatW');
// psz1 : LPWSTR in/out -> Pointer<Utf16>
// psz2 : LPCWSTR -> Pointer<Utf16>
// cchMax : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function StrNCatW(
  psz1: PWideChar;   // LPWSTR in/out
  psz2: PWideChar;   // LPCWSTR
  cchMax: Integer   // INT
): PWideChar; stdcall;
  external 'SHLWAPI.dll' name 'StrNCatW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "StrNCatW"
  c_StrNCatW :: CWString -> CWString -> Int32 -> IO CWString
-- psz1 : LPWSTR in/out -> CWString
-- psz2 : LPCWSTR -> CWString
-- cchMax : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let strncatw =
  foreign "StrNCatW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> returning (ptr uint16_t))
(* psz1 : LPWSTR in/out -> (ptr uint16_t) *)
(* psz2 : LPCWSTR -> (ptr uint16_t) *)
(* cchMax : 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 ("StrNCatW" str-ncat-w :convention :stdcall) (:string :encoding :utf-16le)
  (psz1 :pointer)   ; LPWSTR in/out
  (psz2 (:string :encoding :utf-16le))   ; LPCWSTR
  (cch-max :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $StrNCatW = Win32::API::More->new('SHLWAPI',
    'LPWSTR StrNCatW(LPWSTR psz1, LPCWSTR psz2, int cchMax)');
# my $ret = $StrNCatW->Call($psz1, $psz2, $cchMax);
# psz1 : LPWSTR in/out -> LPWSTR
# psz2 : LPCWSTR -> LPCWSTR
# cchMax : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い