c++ - 如何将 VB.Net 字符串作为 char* 传递给 C++ dll?

标签 c++ string vb.net dllimport

我的 VB.Net 程序在 Win CE 设备上运行,并使用制造商提供的 C++ dll,以使用连接的打印机。

他们的文档像这样列出了函数调用:

int Prn_Str(char *fmt, …);

他们使用 dll 的示例应用程序是用 C++ 编写的。下面是一个调用示例:

rc =  Prn_Str((char*)"POS签购单/POS SALES SLIP\n");

所以在我的代码中我声明了这个函数:

<DllImport("VAx_VPOS396_APPAPI.dll", EntryPoint:="Prn_Str", CharSet:=CharSet.Unicode)> _
    Private Shared Function Prn_Str(ByVal txt As String) As Integer
    End Function

然后调用:

Dim printthis as String = "Test"
Prn_Str(printthis)

我的问题是这似乎只传递了字符串的第一个字母/字符——因为打印输出只显示每行/调用的第一个字母。我试过用 StringBuilder 参数声明它,但也没有用。

在这里传递字符串的实际方法是什么?

(附:将整个应用程序从 VB.Net 更改为 C++ 不是一个选项,因为它以前是使用另一个 DLL 为另一个设备编写的,并且有效。我正在尝试快速移植到这个新设备)

最佳答案

我无法将 CharSet 设置为 ANSI(它不在可用的 CharSet 下),但我通过将其作为 ASCII 编码的字节数组传递来解决此问题。

<DllImport("VAx_VPOS396_APPAPI.dll", EntryPoint:="Prn_Str")> _
    Private Shared Function Prn_Str(ByVal txt As Byte()) As Integer
    End Function

和:

Dim printthis as String = "Test"
Dim ascii as new ASCIIEncoding
Dim eb as Byte() = ascii.GetBytes(printtthis)
Prn_Str(eb)

关于c++ - 如何将 VB.Net 字符串作为 char* 传递给 C++ dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163793/

相关文章:

arrays - 将1,2字符串转换为数组[1、2]

c++ - 在结构指针中修改 C 字符串

vb.net - 列出目录中的所有文件夹

c++ - 如何从 POSIX 文件描述符构建 C++ fstream?

c++ - 如何从圆心拟合直线

python - 不支持的格式字符 ";"。想不通

vb.net - 无法从另一个线程向 DataTable 添加行

c++ - 为什么 std::pair 没有迭代器?

android - 在 C++ 中学习使用 ndk+opengl 开发游戏的资源?

c# - 代码隐藏中包含 c# 和 vb.net 的 asp.net Web 应用程序