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

SHUnicodeToAnsi

関数
Unicode文字列をANSI文字列へ変換する。
DLLSHLWAPI.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

INT SHUnicodeToAnsi(
    LPCWSTR pwszSrc,
    LPSTR pszDst,
    INT cchBuf
);

パラメーター

名前方向説明
pwszSrcLPCWSTRin変換元のUnicode文字列。
pszDstLPSTRout変換後のANSI文字列を受け取るバッファ。
cchBufINTinpszDstのサイズ(文字数)。

戻り値の型: INT

各言語での呼び出し定義

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

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

SHUnicodeToAnsi = ctypes.windll.shlwapi.SHUnicodeToAnsi
SHUnicodeToAnsi.restype = ctypes.c_int
SHUnicodeToAnsi.argtypes = [
    wintypes.LPCWSTR,  # pwszSrc : LPCWSTR
    wintypes.LPSTR,  # pszDst : LPSTR out
    ctypes.c_int,  # cchBuf : INT
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHUnicodeToAnsi = shlwapi.NewProc("SHUnicodeToAnsi")
)

// pwszSrc (LPCWSTR), pszDst (LPSTR out), cchBuf (INT)
r1, _, err := procSHUnicodeToAnsi.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszSrc))),
	uintptr(pszDst),
	uintptr(cchBuf),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function SHUnicodeToAnsi(
  pwszSrc: PWideChar;   // LPCWSTR
  pszDst: PAnsiChar;   // LPSTR out
  cchBuf: Integer   // INT
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'SHUnicodeToAnsi';
result := DllCall("SHLWAPI\SHUnicodeToAnsi"
    , "WStr", pwszSrc   ; LPCWSTR
    , "Ptr", pszDst   ; LPSTR out
    , "Int", cchBuf   ; INT
    , "Int")   ; return: INT
●SHUnicodeToAnsi(pwszSrc, pszDst, cchBuf) = DLL("SHLWAPI.dll", "int SHUnicodeToAnsi(char*, char*, int)")
# 呼び出し: SHUnicodeToAnsi(pwszSrc, pszDst, cchBuf)
# pwszSrc : LPCWSTR -> "char*"
# pszDst : LPSTR out -> "char*"
# cchBuf : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。