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

OemToCharW

関数
OEM文字列をUnicode文字セットへ変換する。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// USER32.dll  (Unicode / -W)
#include <windows.h>

BOOL OemToCharW(
    LPCSTR pSrc,
    LPWSTR pDst
);

パラメーター

名前方向
pSrcLPCSTRin
pDstLPWSTRout

戻り値の型: BOOL

各言語での呼び出し定義

// USER32.dll  (Unicode / -W)
#include <windows.h>

BOOL OemToCharW(
    LPCSTR pSrc,
    LPWSTR pDst
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool OemToCharW(
    [MarshalAs(UnmanagedType.LPStr)] string pSrc,   // LPCSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pDst   // LPWSTR out
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function OemToCharW(
    <MarshalAs(UnmanagedType.LPStr)> pSrc As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPWStr)> pDst As System.Text.StringBuilder   ' LPWSTR out
) As Boolean
End Function
' pSrc : LPCSTR
' pDst : LPWSTR out
Declare PtrSafe Function OemToCharW Lib "user32" ( _
    ByVal pSrc As String, _
    ByVal pDst As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

OemToCharW = ctypes.windll.user32.OemToCharW
OemToCharW.restype = wintypes.BOOL
OemToCharW.argtypes = [
    wintypes.LPCSTR,  # pSrc : LPCSTR
    wintypes.LPWSTR,  # pDst : LPWSTR out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
OemToCharW = Fiddle::Function.new(
  lib['OemToCharW'],
  [
    Fiddle::TYPE_VOIDP,  # pSrc : LPCSTR
    Fiddle::TYPE_VOIDP,  # pDst : LPWSTR out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn OemToCharW(
        pSrc: *const u8,  // LPCSTR
        pDst: *mut u16  // LPWSTR out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool OemToCharW([MarshalAs(UnmanagedType.LPStr)] string pSrc, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pDst);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_OemToCharW' -Namespace Win32 -PassThru
# $api::OemToCharW(pSrc, pDst)
#uselib "USER32.dll"
#func global OemToCharW "OemToCharW" wptr, wptr
; OemToCharW pSrc, varptr(pDst)   ; 戻り値は stat
; pSrc : LPCSTR -> "wptr"
; pDst : LPWSTR out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global OemToCharW "OemToCharW" str, var
; res = OemToCharW(pSrc, pDst)
; pSrc : LPCSTR -> "str"
; pDst : LPWSTR out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL OemToCharW(LPCSTR pSrc, LPWSTR pDst)
#uselib "USER32.dll"
#cfunc global OemToCharW "OemToCharW" str, var
; res = OemToCharW(pSrc, pDst)
; pSrc : LPCSTR -> "str"
; pDst : LPWSTR out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procOemToCharW = user32.NewProc("OemToCharW")
)

// pSrc (LPCSTR), pDst (LPWSTR out)
r1, _, err := procOemToCharW.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pSrc))),
	uintptr(pDst),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function OemToCharW(
  pSrc: PAnsiChar;   // LPCSTR
  pDst: PWideChar   // LPWSTR out
): BOOL; stdcall;
  external 'USER32.dll' name 'OemToCharW';
result := DllCall("USER32\OemToCharW"
    , "AStr", pSrc   ; LPCSTR
    , "Ptr", pDst   ; LPWSTR out
    , "Int")   ; return: BOOL
●OemToCharW(pSrc, pDst) = DLL("USER32.dll", "bool OemToCharW(char*, char*)")
# 呼び出し: OemToCharW(pSrc, pDst)
# pSrc : LPCSTR -> "char*"
# pDst : LPWSTR out -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。