(急)vb 调用c++ dll 时 wchar_t 应该怎么对应?

ymhdyao 2019-07-01 08:14:15
vb 调用c++ dll 时 wchar_t 应该怎么对应?
...全文
684 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dd_zhouqian 2020-07-20
  • 打赏
  • 举报
回复
好的,我试下!
舉杯邀明月 2020-07-18
  • 打赏
  • 举报
回复
其实用不着那么麻烦。
API声明中,把wchar_t参数声明为 byval wc as long ,
在VB6调用时,用 strptr(字符串变量或常量) 传递就行了。


比如6楼所说的 MessageBoxW ,在声明时,第2个和第3个参数都改为 byval xx as long

然后调用时,这样即可:
MessageBox 0, strptr("消息提示内容"), strptr("消息标题"), 64
dd_zhouqian 2020-07-15
  • 打赏
  • 举报
回复
引用 6 楼 现在还是人类 的回复:
如果你实在想把Unicode的字符串传给API,可以用字节数组来传数据。 比如先将某个字符串转换为Unicode编码到字节数组中,然后再把这个字节数组传递过去,如:

Private Declare Function MessageBox Lib "user32" Alias "MessageBoxW" (ByVal hwnd As Long, _
                                                                      ByRef lpText As Any, _
                                                                      ByRef lpCaption As Any, _
                                                                      ByVal wType As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, _
                                                             ByVal dwFlags As Long, _
                                                             ByVal lpMultiByteStr As String, _
                                                             ByVal cchMultiByte As Long, _
                                                             ByRef lpWideCharStr As Any, _
                                                             ByVal cchWideChar As Long) As Long
Private Const CP_ACP = 0  '  default to ANSI code page

Private Sub Command1_Click()
    Dim str_message As String, _
        str_title As String, _
        array_message() As Byte, _
        attay_title() As Byte
        
    str_message = "这是 UNICODE 提示信息,请您确定。"
    str_title = "UNICODE 标题"
    array_message = ASCII2UNICODE(str_message)
    attay_title = ASCII2UNICODE(str_title)
    MessageBox 0, array_message(0), attay_title(0), 64

End Sub

Private Function ASCII2UNICODE(ByVal in_str_ascii As String) As Byte()
    Dim wLength As Long, _
        out_buff() As Byte
    wLength = MultiByteToWideChar(CP_ACP, 0, in_str_ascii, -1, vbNull, 0) ' 取得要转换的宽字符空间大小
    ReDim out_buff(wLength)
    MultiByteToWideChar CP_ACP, 0, in_str_ascii, -1, out_buff(0), wLength
    ASCII2UNICODE = out_buff
End Function

可用,谢谢.
milaoshu1020 2019-07-18
  • 打赏
  • 举报
回复
可以写个适配DLL,用标准DLL或者ActiveX DLL;
milaoshu1020 2019-07-18
  • 打赏
  • 举报
回复
可以写个适配DLL,用标准DLL或者ActiveX DLL;
ymhdyao 2019-07-18
  • 打赏
  • 举报
回复
VC代码不能改,不是我开发的
现在还是人类 2019-07-18
  • 打赏
  • 举报
回复
如果你实在想把Unicode的字符串传给API,可以用字节数组来传数据。 比如先将某个字符串转换为Unicode编码到字节数组中,然后再把这个字节数组传递过去,如:

Private Declare Function MessageBox Lib "user32" Alias "MessageBoxW" (ByVal hwnd As Long, _
                                                                      ByRef lpText As Any, _
                                                                      ByRef lpCaption As Any, _
                                                                      ByVal wType As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, _
                                                             ByVal dwFlags As Long, _
                                                             ByVal lpMultiByteStr As String, _
                                                             ByVal cchMultiByte As Long, _
                                                             ByRef lpWideCharStr As Any, _
                                                             ByVal cchWideChar As Long) As Long
Private Const CP_ACP = 0  '  default to ANSI code page

Private Sub Command1_Click()
    Dim str_message As String, _
        str_title As String, _
        array_message() As Byte, _
        attay_title() As Byte
        
    str_message = "这是 UNICODE 提示信息,请您确定。"
    str_title = "UNICODE 标题"
    array_message = ASCII2UNICODE(str_message)
    attay_title = ASCII2UNICODE(str_title)
    MessageBox 0, array_message(0), attay_title(0), 64

End Sub

Private Function ASCII2UNICODE(ByVal in_str_ascii As String) As Byte()
    Dim wLength As Long, _
        out_buff() As Byte
    wLength = MultiByteToWideChar(CP_ACP, 0, in_str_ascii, -1, vbNull, 0) ' 取得要转换的宽字符空间大小
    ReDim out_buff(wLength)
    MultiByteToWideChar CP_ACP, 0, in_str_ascii, -1, out_buff(0), wLength
    ASCII2UNICODE = out_buff
End Function

现在还是人类 2019-07-15
  • 打赏
  • 举报
回复
VC对接VB的字符串可以用BSTR类型,你可以搜索 BSTR 转 wchar_t
InkHin 2019-07-14
  • 打赏
  • 举报
回复
string不就行了。至于转换的百度win32api。

1,485

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧