VB.NET DLL中的C++DLL函数复制

C++ DLL function replication in VB.NET DLL

本文关键字:函数 复制 C++DLL 中的 NET DLL VB      更新时间:2023-10-16

我很失落,还是个新手,所以请耐心等待。

在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 *datachar *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++字符等价物是字节,所以我尝试了data() As Bytedata = System.Text.Encoding.Default.GetBytes("hello world"),结果又是胡言乱语。

DLL接收到的内容无法更改,所以我需要找到VB.NET处理它的方法;我如何在VB.NET中做到这一点?能做到吗?

经过一些密集的按钮粉碎,我成功地完成了这项工作,这要归功于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。