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

问题描述 投票:0回答:1

我的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参数声明它,但也没有用。

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

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

c++ string vb.net dllimport
1个回答
1
投票

我无法将CharSet设置为ANSI(它不在CharSets可用下),但我通过将其作为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)
© www.soinside.com 2019 - 2024. All rights reserved.