Search code examples
cvb.netvb.net-2010

Declaring a string of a specific length


From Visual Basic I need to call a function in a dynamic link library (DLL) which is implemented in C. The C function has the signature

uint32_t F(char **str);

where str is an output parameter. The array *str is expected to be at least n characters long. On the Visual Basic side the function is declared as

<DllImport("lib.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function F(ByRef str As String) As UInteger
End Function

How do I declare a string of length n in Visual Basic which is compatible with the formal parameter of the C function F?

Edit: The function call works as expected if I declare the actual parameter as for instance

Dim str As String = "          "

assuming n is 10. However, n is a variable.


Solution

  • As mentioned by TnTinMn in one of the comments, declare the actual argument like this:

    Dim str As New String(ChrW(0), n)