2

我很迷茫,是个新手,所以请耐心等待。

在 C++ 中,函数看起来像这样:

int __stdcall helloworld(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause) {
    strcpy(data, "hello world");
    return 3;
}

我正在尝试在 VB.NET 中复制它,但我已经用char *dataandchar *parms部分碰了壁。

Public Shared Function helloworld(ByVal mWnd As IntPtr, ByVal aWnd As IntPtr, ByRef data As Char, ByRef parms As Char, ByVal show As Boolean, ByVal nopause As Boolean) As Integer
    data = "hello world"
    Return 3
End Function

这导致输出"h",所以我尝试了data(),这导致乱码。然后我在某处读到 VB.NET 中的 C/C++ char 等效项是字节,所以我尝试了data() As Byteand data = System.Text.Encoding.Default.GetBytes("hello world"),这又导致了乱码。

DLL 接收的内容无法更改,因此我需要找到一种方法让 VB.NET 处理它。我的问题是;我如何在 VB.NET 中做到这一点?甚至可以做到吗?

4

1 回答 1

1

经过一些密集的按钮粉碎后,由于 Visual Vincent 的 StringBuilder 建议,我设法做到了:

Imports System.Runtime.InteropServices
Imports System.Text
Public Class MyClass
    <DllExport(CallingConvention.StdCall)>
    Public Shared Function helloworld(ByVal mWnd As IntPtr, ByVal aWnd As IntPtr, ByVal data As StringBuilder, ByVal parms As StringBuilder, ByVal show As Boolean, ByVal nopause As Boolean) As Integer
        data.Append("hello world")
        Return 3
    End Function
End Class

也将 ByRef 更改为 ByVal。

于 2019-11-16T07:14:25.957 回答