Win32 API 日本語リファレンス
ホームFoundation › SysAllocStringLen

SysAllocStringLen

関数
指定文字数のBSTR文字列を割り当ててコピーする。
DLLOLEAUT32.dll呼出規約winapi

シグネチャ

// OLEAUT32.dll
#include <windows.h>

LPWSTR SysAllocStringLen(
    LPCWSTR strIn,   // optional
    DWORD ui
);

パラメーター

名前方向説明
strInLPCWSTRinoptional入力文字列。
uiDWORDinコピーする文字数。その後に null 文字が配置され、合計で ui プラス 1 文字が割り当てられます。

戻り値の型: LPWSTR

公式ドキュメント

新しい文字列を割り当て、渡された文字列から指定された数の文字をコピーし、末尾に null 終端文字を付加します。

戻り値

文字列のコピー。操作を完了するためのメモリが不足している場合は NULL

解説(Remarks)

文字列には埋め込みの null 文字を含めることができ、NULL で終わる必要はありません。返された文字列は、後で SysFreeString で解放してください。strInNULL でない場合、strIn に割り当てられたメモリは少なくとも ui 文字分の長さが必要です。

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

各言語での呼び出し定義

// OLEAUT32.dll
#include <windows.h>

LPWSTR SysAllocStringLen(
    LPCWSTR strIn,   // optional
    DWORD ui
);
[DllImport("OLEAUT32.dll", ExactSpelling = true)]
static extern IntPtr SysAllocStringLen(
    [MarshalAs(UnmanagedType.LPWStr)] string strIn,   // LPCWSTR optional
    uint ui   // DWORD
);
<DllImport("OLEAUT32.dll", ExactSpelling:=True)>
Public Shared Function SysAllocStringLen(
    <MarshalAs(UnmanagedType.LPWStr)> strIn As String,   ' LPCWSTR optional
    ui As UInteger   ' DWORD
) As IntPtr
End Function
' strIn : LPCWSTR optional
' ui : DWORD
Declare PtrSafe Function SysAllocStringLen Lib "oleaut32" ( _
    ByVal strIn As LongPtr, _
    ByVal ui As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SysAllocStringLen = ctypes.windll.oleaut32.SysAllocStringLen
SysAllocStringLen.restype = wintypes.LPWSTR
SysAllocStringLen.argtypes = [
    wintypes.LPCWSTR,  # strIn : LPCWSTR optional
    wintypes.DWORD,  # ui : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('OLEAUT32.dll')
SysAllocStringLen = Fiddle::Function.new(
  lib['SysAllocStringLen'],
  [
    Fiddle::TYPE_VOIDP,  # strIn : LPCWSTR optional
    -Fiddle::TYPE_INT,  # ui : DWORD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "oleaut32")]
extern "system" {
    fn SysAllocStringLen(
        strIn: *const u16,  // LPCWSTR optional
        ui: u32  // DWORD
    ) -> *mut u16;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("OLEAUT32.dll")]
public static extern IntPtr SysAllocStringLen([MarshalAs(UnmanagedType.LPWStr)] string strIn, uint ui);
"@
$api = Add-Type -MemberDefinition $sig -Name 'OLEAUT32_SysAllocStringLen' -Namespace Win32 -PassThru
# $api::SysAllocStringLen(strIn, ui)
#uselib "OLEAUT32.dll"
#func global SysAllocStringLen "SysAllocStringLen" sptr, sptr
; SysAllocStringLen strIn, ui   ; 戻り値は stat
; strIn : LPCWSTR optional -> "sptr"
; ui : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "OLEAUT32.dll"
#cfunc global SysAllocStringLen "SysAllocStringLen" wstr, int
; res = SysAllocStringLen(strIn, ui)
; strIn : LPCWSTR optional -> "wstr"
; ui : DWORD -> "int"
; LPWSTR SysAllocStringLen(LPCWSTR strIn, DWORD ui)
#uselib "OLEAUT32.dll"
#cfunc global SysAllocStringLen "SysAllocStringLen" wstr, int
; res = SysAllocStringLen(strIn, ui)
; strIn : LPCWSTR optional -> "wstr"
; ui : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	oleaut32 = windows.NewLazySystemDLL("OLEAUT32.dll")
	procSysAllocStringLen = oleaut32.NewProc("SysAllocStringLen")
)

// strIn (LPCWSTR optional), ui (DWORD)
r1, _, err := procSysAllocStringLen.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(strIn))),
	uintptr(ui),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // LPWSTR
function SysAllocStringLen(
  strIn: PWideChar;   // LPCWSTR optional
  ui: DWORD   // DWORD
): PWideChar; stdcall;
  external 'OLEAUT32.dll' name 'SysAllocStringLen';
result := DllCall("OLEAUT32\SysAllocStringLen"
    , "WStr", strIn   ; LPCWSTR optional
    , "UInt", ui   ; DWORD
    , "Ptr")   ; return: LPWSTR
●SysAllocStringLen(strIn, ui) = DLL("OLEAUT32.dll", "char* SysAllocStringLen(char*, dword)")
# 呼び出し: SysAllocStringLen(strIn, ui)
# strIn : LPCWSTR optional -> "char*"
# ui : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "oleaut32" fn SysAllocStringLen(
    strIn: [*c]const u16, // LPCWSTR optional
    ui: u32 // DWORD
) callconv(std.os.windows.WINAPI) [*c]const u16;
proc SysAllocStringLen(
    strIn: WideCString,  # LPCWSTR optional
    ui: uint32  # DWORD
): WideCString {.importc: "SysAllocStringLen", stdcall, dynlib: "OLEAUT32.dll".}
pragma(lib, "oleaut32");
extern(Windows)
const(wchar)* SysAllocStringLen(
    const(wchar)* strIn,   // LPCWSTR optional
    uint ui   // DWORD
);
ccall((:SysAllocStringLen, "OLEAUT32.dll"), stdcall, Cwstring,
      (Cwstring, UInt32),
      strIn, ui)
# strIn : LPCWSTR optional -> Cwstring
# ui : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
const uint16_t* SysAllocStringLen(
    const uint16_t* strIn,
    uint32_t ui);
]]
local oleaut32 = ffi.load("oleaut32")
-- oleaut32.SysAllocStringLen(strIn, ui)
-- strIn : LPCWSTR optional
-- ui : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('OLEAUT32.dll');
const SysAllocStringLen = lib.func('__stdcall', 'SysAllocStringLen', 'void *', ['str16', 'uint32_t']);
// SysAllocStringLen(strIn, ui)
// strIn : LPCWSTR optional -> 'str16'
// ui : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("OLEAUT32.dll", {
  SysAllocStringLen: { parameters: ["buffer", "u32"], result: "pointer" },
});
// lib.symbols.SysAllocStringLen(strIn, ui)
// strIn : LPCWSTR optional -> "buffer"
// ui : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
const uint16_t* SysAllocStringLen(
    const uint16_t* strIn,
    uint32_t ui);
C, "OLEAUT32.dll");
// $ffi->SysAllocStringLen(strIn, ui);
// strIn : LPCWSTR optional
// ui : DWORD
// 構造体/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 Oleaut32 extends StdCallLibrary {
    Oleaut32 INSTANCE = Native.load("oleaut32", Oleaut32.class);
    Pointer SysAllocStringLen(
        WString strIn,   // LPCWSTR optional
        int ui   // DWORD
    );
}
@[Link("oleaut32")]
lib LibOLEAUT32
  fun SysAllocStringLen = SysAllocStringLen(
    strIn : UInt16*,   # LPCWSTR optional
    ui : UInt32   # DWORD
  ) : UInt16*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SysAllocStringLenNative = Pointer<Utf16> Function(Pointer<Utf16>, Uint32);
typedef SysAllocStringLenDart = Pointer<Utf16> Function(Pointer<Utf16>, int);
final SysAllocStringLen = DynamicLibrary.open('OLEAUT32.dll')
    .lookupFunction<SysAllocStringLenNative, SysAllocStringLenDart>('SysAllocStringLen');
// strIn : LPCWSTR optional -> Pointer<Utf16>
// ui : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SysAllocStringLen(
  strIn: PWideChar;   // LPCWSTR optional
  ui: DWORD   // DWORD
): PWideChar; stdcall;
  external 'OLEAUT32.dll' name 'SysAllocStringLen';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SysAllocStringLen"
  c_SysAllocStringLen :: CWString -> Word32 -> IO CWString
-- strIn : LPCWSTR optional -> CWString
-- ui : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let sysallocstringlen =
  foreign "SysAllocStringLen"
    ((ptr uint16_t) @-> uint32_t @-> returning (ptr uint16_t))
(* strIn : LPCWSTR optional -> (ptr uint16_t) *)
(* ui : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library oleaut32 (t "OLEAUT32.dll"))
(cffi:use-foreign-library oleaut32)

(cffi:defcfun ("SysAllocStringLen" sys-alloc-string-len :convention :stdcall) (:string :encoding :utf-16le)
  (str-in (:string :encoding :utf-16le))   ; LPCWSTR optional
  (ui :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SysAllocStringLen = Win32::API::More->new('OLEAUT32',
    'LPWSTR SysAllocStringLen(LPCWSTR strIn, DWORD ui)');
# my $ret = $SysAllocStringLen->Call($strIn, $ui);
# strIn : LPCWSTR optional -> LPCWSTR
# ui : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。