StrCpyNW
関数指定最大長まで文字列をコピーする。
シグネチャ
// SHLWAPI.dll
#include <windows.h>
LPWSTR StrCpyNW(
LPWSTR pszDst,
LPCWSTR pszSrc,
INT cchMax
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszDst | LPWSTR | out | この関数が正常に返ったときに、コピーされた文字列を受け取るバッファーへのポインターです。このバッファーは、コピーされる文字を保持するのに十分なサイズでなければなりません。この文字列は null 終端されることが保証されません。 |
| pszSrc | LPCWSTR | in | null 終端されたコピー元文字列へのポインターです。 |
| cchMax | INT | in | コピーする文字数で、終端の null 文字を含みます。 |
戻り値の型: LPWSTR
公式ドキュメント
ある文字列の先頭から指定された数の文字を別の文字列にコピーします。注 この関数および StrNCpy マクロは使用しないでください。
戻り値
型: PTSTR
pszDst へのポインターを返します。
解説(Remarks)
セキュリティ警告: この関数を誤って使用すると、アプリケーションのセキュリティが損なわれる可能性があります。コピーされた文字列は null 終端されることが保証されません。次のいずれかの代替手段の使用を検討してください。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 StrCpyNW(
LPWSTR pszDst,
LPCWSTR pszSrc,
INT cchMax
);[DllImport("SHLWAPI.dll", ExactSpelling = true)]
static extern IntPtr StrCpyNW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDst, // LPWSTR out
[MarshalAs(UnmanagedType.LPWStr)] string pszSrc, // LPCWSTR
int cchMax // INT
);<DllImport("SHLWAPI.dll", ExactSpelling:=True)>
Public Shared Function StrCpyNW(
<MarshalAs(UnmanagedType.LPWStr)> pszDst As System.Text.StringBuilder, ' LPWSTR out
<MarshalAs(UnmanagedType.LPWStr)> pszSrc As String, ' LPCWSTR
cchMax As Integer ' INT
) As IntPtr
End Function' pszDst : LPWSTR out
' pszSrc : LPCWSTR
' cchMax : INT
Declare PtrSafe Function StrCpyNW Lib "shlwapi" ( _
ByVal pszDst As LongPtr, _
ByVal pszSrc As LongPtr, _
ByVal cchMax As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
StrCpyNW = ctypes.windll.shlwapi.StrCpyNW
StrCpyNW.restype = wintypes.LPWSTR
StrCpyNW.argtypes = [
wintypes.LPWSTR, # pszDst : LPWSTR out
wintypes.LPCWSTR, # pszSrc : LPCWSTR
ctypes.c_int, # cchMax : INT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
StrCpyNW = Fiddle::Function.new(
lib['StrCpyNW'],
[
Fiddle::TYPE_VOIDP, # pszDst : LPWSTR out
Fiddle::TYPE_VOIDP, # pszSrc : LPCWSTR
Fiddle::TYPE_INT, # cchMax : INT
],
Fiddle::TYPE_VOIDP)#[link(name = "shlwapi")]
extern "system" {
fn StrCpyNW(
pszDst: *mut u16, // LPWSTR out
pszSrc: *const u16, // LPCWSTR
cchMax: i32 // INT
) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll")]
public static extern IntPtr StrCpyNW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDst, [MarshalAs(UnmanagedType.LPWStr)] string pszSrc, int cchMax);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrCpyNW' -Namespace Win32 -PassThru
# $api::StrCpyNW(pszDst, pszSrc, cchMax)#uselib "SHLWAPI.dll"
#func global StrCpyNW "StrCpyNW" sptr, sptr, sptr
; StrCpyNW varptr(pszDst), pszSrc, cchMax ; 戻り値は stat
; pszDst : LPWSTR out -> "sptr"
; pszSrc : LPCWSTR -> "sptr"
; cchMax : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SHLWAPI.dll" #cfunc global StrCpyNW "StrCpyNW" var, wstr, int ; res = StrCpyNW(pszDst, pszSrc, cchMax) ; pszDst : LPWSTR out -> "var" ; pszSrc : LPCWSTR -> "wstr" ; cchMax : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SHLWAPI.dll" #cfunc global StrCpyNW "StrCpyNW" sptr, wstr, int ; res = StrCpyNW(varptr(pszDst), pszSrc, cchMax) ; pszDst : LPWSTR out -> "sptr" ; pszSrc : LPCWSTR -> "wstr" ; cchMax : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; LPWSTR StrCpyNW(LPWSTR pszDst, LPCWSTR pszSrc, INT cchMax) #uselib "SHLWAPI.dll" #cfunc global StrCpyNW "StrCpyNW" var, wstr, int ; res = StrCpyNW(pszDst, pszSrc, cchMax) ; pszDst : LPWSTR out -> "var" ; pszSrc : LPCWSTR -> "wstr" ; cchMax : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; LPWSTR StrCpyNW(LPWSTR pszDst, LPCWSTR pszSrc, INT cchMax) #uselib "SHLWAPI.dll" #cfunc global StrCpyNW "StrCpyNW" intptr, wstr, int ; res = StrCpyNW(varptr(pszDst), pszSrc, cchMax) ; pszDst : LPWSTR out -> "intptr" ; pszSrc : LPCWSTR -> "wstr" ; cchMax : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procStrCpyNW = shlwapi.NewProc("StrCpyNW")
)
// pszDst (LPWSTR out), pszSrc (LPCWSTR), cchMax (INT)
r1, _, err := procStrCpyNW.Call(
uintptr(pszDst),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszSrc))),
uintptr(cchMax),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // LPWSTRfunction StrCpyNW(
pszDst: PWideChar; // LPWSTR out
pszSrc: PWideChar; // LPCWSTR
cchMax: Integer // INT
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrCpyNW';result := DllCall("SHLWAPI\StrCpyNW"
, "Ptr", pszDst ; LPWSTR out
, "WStr", pszSrc ; LPCWSTR
, "Int", cchMax ; INT
, "Ptr") ; return: LPWSTR●StrCpyNW(pszDst, pszSrc, cchMax) = DLL("SHLWAPI.dll", "char* StrCpyNW(char*, char*, int)")
# 呼び出し: StrCpyNW(pszDst, pszSrc, cchMax)
# pszDst : LPWSTR out -> "char*"
# pszSrc : LPCWSTR -> "char*"
# cchMax : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn StrCpyNW(
pszDst: [*c]u16, // LPWSTR out
pszSrc: [*c]const u16, // LPCWSTR
cchMax: i32 // INT
) callconv(std.os.windows.WINAPI) [*c]const u16;proc StrCpyNW(
pszDst: ptr uint16, # LPWSTR out
pszSrc: WideCString, # LPCWSTR
cchMax: int32 # INT
): WideCString {.importc: "StrCpyNW", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
const(wchar)* StrCpyNW(
wchar* pszDst, // LPWSTR out
const(wchar)* pszSrc, // LPCWSTR
int cchMax // INT
);ccall((:StrCpyNW, "SHLWAPI.dll"), stdcall, Cwstring,
(Ptr{UInt16}, Cwstring, Int32),
pszDst, pszSrc, cchMax)
# pszDst : LPWSTR out -> Ptr{UInt16}
# pszSrc : LPCWSTR -> Cwstring
# cchMax : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
const uint16_t* StrCpyNW(
uint16_t* pszDst,
const uint16_t* pszSrc,
int32_t cchMax);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrCpyNW(pszDst, pszSrc, cchMax)
-- pszDst : LPWSTR out
-- pszSrc : LPCWSTR
-- cchMax : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrCpyNW = lib.func('__stdcall', 'StrCpyNW', 'void *', ['uint16_t *', 'str16', 'int32_t']);
// StrCpyNW(pszDst, pszSrc, cchMax)
// pszDst : LPWSTR out -> 'uint16_t *'
// pszSrc : LPCWSTR -> 'str16'
// cchMax : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
StrCpyNW: { parameters: ["buffer", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.StrCpyNW(pszDst, pszSrc, cchMax)
// pszDst : LPWSTR out -> "buffer"
// pszSrc : LPCWSTR -> "buffer"
// cchMax : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
const uint16_t* StrCpyNW(
uint16_t* pszDst,
const uint16_t* pszSrc,
int32_t cchMax);
C, "SHLWAPI.dll");
// $ffi->StrCpyNW(pszDst, pszSrc, cchMax);
// pszDst : LPWSTR out
// pszSrc : 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);
Pointer StrCpyNW(
char[] pszDst, // LPWSTR out
WString pszSrc, // LPCWSTR
int cchMax // INT
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun StrCpyNW = StrCpyNW(
pszDst : UInt16*, # LPWSTR out
pszSrc : 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 StrCpyNWNative = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, Int32);
typedef StrCpyNWDart = Pointer<Utf16> Function(Pointer<Utf16>, Pointer<Utf16>, int);
final StrCpyNW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<StrCpyNWNative, StrCpyNWDart>('StrCpyNW');
// pszDst : LPWSTR out -> Pointer<Utf16>
// pszSrc : LPCWSTR -> Pointer<Utf16>
// cchMax : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function StrCpyNW(
pszDst: PWideChar; // LPWSTR out
pszSrc: PWideChar; // LPCWSTR
cchMax: Integer // INT
): PWideChar; stdcall;
external 'SHLWAPI.dll' name 'StrCpyNW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "StrCpyNW"
c_StrCpyNW :: CWString -> CWString -> Int32 -> IO CWString
-- pszDst : LPWSTR out -> CWString
-- pszSrc : LPCWSTR -> CWString
-- cchMax : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let strcpynw =
foreign "StrCpyNW"
((ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> returning (ptr uint16_t))
(* pszDst : LPWSTR out -> (ptr uint16_t) *)
(* pszSrc : 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 ("StrCpyNW" str-cpy-nw :convention :stdcall) (:string :encoding :utf-16le)
(psz-dst :pointer) ; LPWSTR out
(psz-src (: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 $StrCpyNW = Win32::API::More->new('SHLWAPI',
'LPWSTR StrCpyNW(LPWSTR pszDst, LPCWSTR pszSrc, int cchMax)');
# my $ret = $StrCpyNW->Call($pszDst, $pszSrc, $cchMax);
# pszDst : LPWSTR out -> LPWSTR
# pszSrc : LPCWSTR -> LPCWSTR
# cchMax : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。