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

SysAllocStringLen

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

シグネチャ

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

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

パラメーター

名前方向
strInLPCWSTRinoptional
uiDWORDin

戻り値の型: LPWSTR

各言語での呼び出し定義

// 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)。