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

StrNCatA

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

シグネチャ

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

LPSTR StrNCatA(
    LPSTR psz1,
    LPCSTR psz2,
    INT cchMax
);

パラメーター

名前方向説明
psz1LPSTRinoutpsz2 の文字が追加される、null で終わる文字列へのポインターです。連結後の文字列と終端の null 文字を格納するのに十分な大きさが必要です。
psz2LPCSTRin追加される、null で終わる文字列へのポインターです。
cchMaxINTinpsz2 の先頭から psz1 に追加する文字数です。

戻り値の型: LPSTR

公式ドキュメント

一方の文字列の先頭から指定された数の文字を、もう一方の文字列の末尾に追加します。(ANSI)

戻り値

型: PTSTR

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

解説(Remarks)

セキュリティに関する警告: この関数を誤って使用すると、アプリケーションのセキュリティが損なわれる可能性があります。最初の引数 psz1 は、psz2 と終端の '\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  (ANSI / -A)
#include <windows.h>

LPSTR StrNCatA(
    LPSTR psz1,
    LPCSTR psz2,
    INT cchMax
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr StrNCatA(
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder psz1,   // LPSTR in/out
    [MarshalAs(UnmanagedType.LPStr)] string psz2,   // LPCSTR
    int cchMax   // INT
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function StrNCatA(
    <MarshalAs(UnmanagedType.LPStr)> psz1 As System.Text.StringBuilder,   ' LPSTR in/out
    <MarshalAs(UnmanagedType.LPStr)> psz2 As String,   ' LPCSTR
    cchMax As Integer   ' INT
) As IntPtr
End Function
' psz1 : LPSTR in/out
' psz2 : LPCSTR
' cchMax : INT
Declare PtrSafe Function StrNCatA Lib "shlwapi" ( _
    ByVal psz1 As String, _
    ByVal psz2 As String, _
    ByVal cchMax As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

lib = Fiddle.dlopen('SHLWAPI.dll')
StrNCatA = Fiddle::Function.new(
  lib['StrNCatA'],
  [
    Fiddle::TYPE_VOIDP,  # psz1 : LPSTR in/out
    Fiddle::TYPE_VOIDP,  # psz2 : LPCSTR
    Fiddle::TYPE_INT,  # cchMax : INT
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "shlwapi")]
extern "system" {
    fn StrNCatA(
        psz1: *mut u8,  // LPSTR in/out
        psz2: *const u8,  // LPCSTR
        cchMax: i32  // INT
    ) -> *mut u8;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr StrNCatA([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder psz1, [MarshalAs(UnmanagedType.LPStr)] string psz2, int cchMax);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrNCatA' -Namespace Win32 -PassThru
# $api::StrNCatA(psz1, psz2, cchMax)
#uselib "SHLWAPI.dll"
#func global StrNCatA "StrNCatA" sptr, sptr, sptr
; StrNCatA varptr(psz1), psz2, cchMax   ; 戻り値は stat
; psz1 : LPSTR in/out -> "sptr"
; psz2 : LPCSTR -> "sptr"
; cchMax : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHLWAPI.dll"
#cfunc global StrNCatA "StrNCatA" var, str, int
; res = StrNCatA(psz1, psz2, cchMax)
; psz1 : LPSTR in/out -> "var"
; psz2 : LPCSTR -> "str"
; cchMax : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; LPSTR StrNCatA(LPSTR psz1, LPCSTR psz2, INT cchMax)
#uselib "SHLWAPI.dll"
#cfunc global StrNCatA "StrNCatA" var, str, int
; res = StrNCatA(psz1, psz2, cchMax)
; psz1 : LPSTR in/out -> "var"
; psz2 : LPCSTR -> "str"
; cchMax : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procStrNCatA = shlwapi.NewProc("StrNCatA")
)

// psz1 (LPSTR in/out), psz2 (LPCSTR), cchMax (INT)
r1, _, err := procStrNCatA.Call(
	uintptr(psz1),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(psz2))),
	uintptr(cchMax),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // LPSTR
function StrNCatA(
  psz1: PAnsiChar;   // LPSTR in/out
  psz2: PAnsiChar;   // LPCSTR
  cchMax: Integer   // INT
): PAnsiChar; stdcall;
  external 'SHLWAPI.dll' name 'StrNCatA';
result := DllCall("SHLWAPI\StrNCatA"
    , "Ptr", psz1   ; LPSTR in/out
    , "AStr", psz2   ; LPCSTR
    , "Int", cchMax   ; INT
    , "Ptr")   ; return: LPSTR
●StrNCatA(psz1, psz2, cchMax) = DLL("SHLWAPI.dll", "char* StrNCatA(char*, char*, int)")
# 呼び出し: StrNCatA(psz1, psz2, cchMax)
# psz1 : LPSTR in/out -> "char*"
# psz2 : LPCSTR -> "char*"
# cchMax : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shlwapi" fn StrNCatA(
    psz1: [*c]u8, // LPSTR in/out
    psz2: [*c]const u8, // LPCSTR
    cchMax: i32 // INT
) callconv(std.os.windows.WINAPI) [*c]const u8;
proc StrNCatA(
    psz1: ptr char,  # LPSTR in/out
    psz2: cstring,  # LPCSTR
    cchMax: int32  # INT
): cstring {.importc: "StrNCatA", stdcall, dynlib: "SHLWAPI.dll".}
pragma(lib, "shlwapi");
extern(Windows)
const(char)* StrNCatA(
    char* psz1,   // LPSTR in/out
    const(char)* psz2,   // LPCSTR
    int cchMax   // INT
);
ccall((:StrNCatA, "SHLWAPI.dll"), stdcall, Cstring,
      (Ptr{UInt8}, Cstring, Int32),
      psz1, psz2, cchMax)
# psz1 : LPSTR in/out -> Ptr{UInt8}
# psz2 : LPCSTR -> Cstring
# cchMax : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
const char* StrNCatA(
    char* psz1,
    const char* psz2,
    int32_t cchMax);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrNCatA(psz1, psz2, cchMax)
-- psz1 : LPSTR in/out
-- psz2 : LPCSTR
-- cchMax : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrNCatA = lib.func('__stdcall', 'StrNCatA', 'void *', ['char *', 'str', 'int32_t']);
// StrNCatA(psz1, psz2, cchMax)
// psz1 : LPSTR in/out -> 'char *'
// psz2 : LPCSTR -> 'str'
// cchMax : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  StrNCatA: { parameters: ["buffer", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.StrNCatA(psz1, psz2, cchMax)
// psz1 : LPSTR in/out -> "buffer"
// psz2 : LPCSTR -> "buffer"
// cchMax : INT -> "i32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
const char* StrNCatA(
    char* psz1,
    const char* psz2,
    int32_t cchMax);
C, "SHLWAPI.dll");
// $ffi->StrNCatA(psz1, psz2, cchMax);
// psz1 : LPSTR in/out
// psz2 : LPCSTR
// 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.ASCII_OPTIONS);
    Pointer StrNCatA(
        byte[] psz1,   // LPSTR in/out
        String psz2,   // LPCSTR
        int cchMax   // INT
    );
}
@[Link("shlwapi")]
lib LibSHLWAPI
  fun StrNCatA = StrNCatA(
    psz1 : UInt8*,   # LPSTR in/out
    psz2 : UInt8*,   # LPCSTR
    cchMax : Int32   # INT
  ) : UInt8*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef StrNCatANative = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>, Int32);
typedef StrNCatADart = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>, int);
final StrNCatA = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<StrNCatANative, StrNCatADart>('StrNCatA');
// psz1 : LPSTR in/out -> Pointer<Utf8>
// psz2 : LPCSTR -> Pointer<Utf8>
// cchMax : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function StrNCatA(
  psz1: PAnsiChar;   // LPSTR in/out
  psz2: PAnsiChar;   // LPCSTR
  cchMax: Integer   // INT
): PAnsiChar; stdcall;
  external 'SHLWAPI.dll' name 'StrNCatA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let strncata =
  foreign "StrNCatA"
    (string @-> string @-> int32_t @-> returning string)
(* psz1 : LPSTR in/out -> string *)
(* psz2 : LPCSTR -> string *)
(* 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 ("StrNCatA" str-ncat-a :convention :stdcall) :string
  (psz1 :pointer)   ; LPSTR in/out
  (psz2 :string)   ; LPCSTR
  (cch-max :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $StrNCatA = Win32::API::More->new('SHLWAPI',
    'LPSTR StrNCatA(LPSTR psz1, LPCSTR psz2, int cchMax)');
# my $ret = $StrNCatA->Call($psz1, $psz2, $cchMax);
# psz1 : LPSTR in/out -> LPSTR
# psz2 : LPCSTR -> LPCSTR
# cchMax : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い