NonSoft

Win32Apiによる文字コード変換のサンプル(VB6)


 サンプル(ソース/コード)
<このサンプルの概要>
以下のWin32Apiを使用したサンプルソースです。
(1)WideCharToMultiByte
(2)MultiByteToWideChar

VB.NETでApiを宣言(Declare)する場合はVB.NET用に変換する必要があります。
変換方法のサンプルはこちらです。VB6とVB.NETのAPI宣言(Declare)の違いを比較したサンプル

C#.NETでApiを宣言(Declare)する場合はC#.NET用に変換する必要があります。
変換方法のサンプルはこちらです。VB6とC#.NETのAPI宣言(Declare)の違いを比較したサンプル

その他のサンプルソースはこちらです。
Win32Api定義のサンプル サンプルソース

'**********************************************************************
' 機能名    : modWCMB.bas
' 機能説明  : WideCharToMultiByteとMultiByteToWideCharによる
'           : 文字コード変換
' 備考      :
' 著作権    : Copyright(C) 2007 のん All rights reserved
'           : このプログラムは、日本国著作権法および国際条約により保護
'           : されています。このプログラムを転載する場合は著作権所有者
'           : の許可が必要となります。
'**********************************************************************
Declare Function WideCharToMultiByte Lib "kernel32" _
    (ByVal CodePage As Long, _
     ByVal dwFlags As Long, _
     ByVal lpWideCharStr As Long, _
     ByVal cchWideChar As Long, _
     ByRef lpMultiByteStr As Any, _
     ByVal cchMultiByte As Long, _
     ByVal lpDefaultChar As String, _
     ByVal lpUsedDefaultChar As Long) As Long
Declare Function MultiByteToWideChar Lib "kernel32" _
    (ByVal CodePage As Long, _
     ByVal dwFlags As Long, _
     ByRef lpMultiByteStr As Any, _
     ByVal cchMultiByte As Long, _
     ByVal lpWideCharStr As Long, _
     ByVal cchWideChar As Long) As Long

'---Shift-JIS関係
' 関数名    : WCMB_Encode_SJIS
' 返り値    : SJIS文字データ
' 引き数    : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データをSJISに変換する
' 備考      :
Public Function WCMB_Encode_SJIS(ByRef strUni As String) As Byte()
    WCMB_Encode_SJIS = WCMB_Encode(932, strUni)
End Function

' 関数名    : WCMB_Decode_SJIS
' 返り値    : UNICODE文字列
' 引き数    : bytIn : SJIS文字データ
' 機能説明  : SJIS文字データをUNICODEに変換する
' 備考      :
Public Function WCMB_Decode_SJIS(ByRef bytIn() As Byte) As String
    WCMB_Decode_SJIS = WCMB_Decode(932, bytIn)
End Function

'---JIS関係
' 関数名    : WCMB_Encode_JIS
' 返り値    : JIS文字データ
' 引き数    : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データをJISに変換する
' 備考      : 半角カタカナ有り
Public Function WCMB_Encode_JIS(ByRef strUni As String) As Byte()
    WCMB_Encode_JIS = WCMB_Encode(50221, strUni)
End Function

' 関数名    : WCMB_Decode_JIS
' 返り値    : UNICODE文字列
' 引き数    : bytIn : JIS文字データ
' 機能説明  : JIS文字データをUNICODEに変換する
' 備考      : 半角カタカナ有り
Public Function WCMB_Decode_JIS(ByRef bytIn() As Byte) As String
    WCMB_Decode_JIS = WCMB_Decode(50221, bytIn)
End Function

' 関数名    : WCMB_Encode_JIS2
' 返り値    : JIS文字データ
' 引き数    : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データをJISに変換する
' 備考      : 半角カタカナ無し
Public Function WCMB_Encode_JIS2(ByRef strUni As String) As Byte()
    WCMB_Encode_JIS2 = WCMB_Encode(50220, strUni)
End Function

' 関数名    : WCMB_Decode_JIS2
' 返り値    : UNICODE文字列
' 引き数    : bytIn : JIS文字データ
' 機能説明  : JIS文字データをUNICODEに変換する
' 備考      : 半角カタカナ無し
Public Function WCMB_Decode_JIS2(ByRef bytIn() As Byte) As String
    WCMB_Decode_JIS2 = WCMB_Decode(50220, bytIn)
End Function

'---EUC関係
' 関数名    : WCMB_Encode_EUC
' 返り値    : EUC文字データ
' 引き数    : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データをEUCに変換する
' 備考      :
Public Function WCMB_Encode_EUC(ByRef strUni As String) As Byte()
    'WCMB_Encode_EUC = WCMB_Encode(51932, strUni)
    MsgBox "EUCはサポートされていません", vbCritical
End Function

' 関数名    : WCMB_Decode_EUC
' 返り値    : UNICODE文字列
' 引き数    : bytIn : EUC文字データ
' 機能説明  : EUC文字データをUNICODEに変換する
' 備考      :
Public Function WCMB_Decode_EUC(ByRef bytIn() As Byte) As String
    'WCMB_Decode_EUC = WCMB_Decode(51932, bytIn)
    MsgBox "EUCはサポートされていません", vbCritical
End Function

'---UTF-7関係
' 関数名    : WCMB_Encode_UTF7
' 返り値    : UTF7文字データ
' 引き数    : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データをUTF7に変換する
' 備考      :
Public Function WCMB_Encode_UTF7(ByRef strUni As String) As Byte()
    WCMB_Encode_UTF7 = WCMB_Encode(65000, strUni)
End Function

' 関数名    : WCMB_Decode_UTF7
' 返り値    : UNICODE文字列
' 引き数    : bytIn : UTF7文字データ
' 機能説明  : UTF7文字データをUNICODEに変換する
' 備考      :
Public Function WCMB_Decode_UTF7(ByRef bytIn() As Byte) As String
    WCMB_Decode_UTF7 = WCMB_Decode(65000, bytIn)
End Function

'---UTF-8関係
' 関数名    : WCMB_Encode_UTF8
' 返り値    : UTF8文字データ
' 引き数    : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データをUTF8に変換する
' 備考      :
Public Function WCMB_Encode_UTF8(ByRef strUni As String) As Byte()
    WCMB_Encode_UTF8 = WCMB_Encode(65001, strUni)
End Function

' 関数名    : WCMB_Decode_UTF8
' 返り値    : UNICODE文字列
' 引き数    : bytIn : UTF8文字データ
' 機能説明  : UTF8文字データをUNICODEに変換する
' 備考      :
Public Function WCMB_Decode_UTF8(ByRef bytIn() As Byte) As String
    WCMB_Decode_UTF8 = WCMB_Decode(65001, bytIn)
End Function

'---共通関数
' 関数名    : WCMB_Encode
' 返り値    : 出力文字データ
' 引き数    : cp    : 出力文字データのコードページ番号
'           : strUni: UNICODE文字データ
' 機能説明  : UNICODE文字データを指定のコードに変換する
' 備考      :
Private Function WCMB_Encode(ByVal cp As Long, ByRef strUni As String) As Byte()
    On Error GoTo ErrHandler

    Dim lngUniLen As Long
    Dim lngBufLen As Long
    Dim lngRtn As Long
    Dim bytOut() As Byte

    lngUniLen = Len(strUni)
    If lngUniLen = 0 Then Exit Function
    lngBufLen = lngUniLen * 5
    ReDim bytOut(lngBufLen - 1)
    lngRtn = WideCharToMultiByte _
        (cp, 0, StrPtr(strUni), lngUniLen, bytOut(0), lngBufLen, vbNullString, 0)
    If lngRtn Then
        ReDim Preserve bytOut(lngRtn - 1)
        WCMB_Encode = bytOut
    End If
    
    Exit Function
ErrHandler:
    Debug.Print "文字コード変換エラー:" & Err.Description
End Function

' 関数名    : WCMB_Decode
' 返り値    : UNICODE文字列
' 引き数    : cp    : 入力文字データのコードページ番号
'           : bytIn : 入力文字データ
' 機能説明  : 入力文字データをUNICODEに変換する
' 備考      :
Private Function WCMB_Decode(ByVal cp As Long, ByRef bytIn() As Byte) As String
    On Error GoTo ErrHandler

    Dim lngInSize As Long
    Dim strBuf As String
    Dim lngBufLen As Long
    Dim lngRtn As Long

    lngInSize = UBound(bytIn) + 1
    lngBufLen = lngInSize * 5
    strBuf = String$(lngBufLen, vbNullChar)
    lngRtn = MultiByteToWideChar _
        (cp, 0, bytIn(0), lngInSize, StrPtr(strBuf), lngBufLen)
    If lngRtn Then
        WCMB_Decode = Left$(strBuf, lngRtn)
    End If

    Exit Function
ErrHandler:
    Debug.Print "文字コード変換エラー:" & Err.Description
End Function