从Visual Basic中,我需要在用C实现的动态链接库(DLL)中调用一个函数。C函数具有签名
uint32_t F(char **str);
其中str是输出参数。数组*str
的长度至少应为n个字符。在Visual Basic上,该函数声明为
<DllImport("lib.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function F(ByRef str As String) As UInteger
End Function
如何在Visual Basic中声明与C函数F的形式参数兼容的长度为n的字符串?
编辑:如果我将实际参数声明为例如,该函数调用将按预期工作
Dim str As String = " "
假定n为10。但是,n是变量。
VB中的String
实际上是C端的BSTR
,它是Windows API中的结构。
在C端,您使用Windows功能,例如SysAllocString
来制造BSTR,并使用SysReleaseString
释放内存。
您将无法(轻松地)在C和VB之间与uint32_t F(char **str);
进行互操作。
您可以用n个空格填充字符串。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = ""
Dim n As Integer = 10
Dim paddedStr = s.PadRight(n)
Debug.Print(paddedStr.Length.ToString) 'Prints 10
End Sub