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

SysAllocStringByteLen

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

シグネチャ

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

LPWSTR SysAllocStringByteLen(
    LPCSTR psz,   // optional
    DWORD len
);

パラメーター

名前方向
pszLPCSTRinoptional
lenDWORDin

戻り値の型: LPWSTR

各言語での呼び出し定義

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

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

SysAllocStringByteLen = ctypes.windll.oleaut32.SysAllocStringByteLen
SysAllocStringByteLen.restype = wintypes.LPWSTR
SysAllocStringByteLen.argtypes = [
    wintypes.LPCSTR,  # psz : LPCSTR optional
    wintypes.DWORD,  # len : DWORD
]
require 'fiddle'
require 'fiddle/import'

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

var (
	oleaut32 = windows.NewLazySystemDLL("OLEAUT32.dll")
	procSysAllocStringByteLen = oleaut32.NewProc("SysAllocStringByteLen")
)

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