且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将字符串从VB.NET传递给const char *到C ++编程的DLL

更新时间:2023-02-12 16:20:07

您必须在VB.NET代码中删除ByRef声明,并仅传递如下字符串参数:

You must remove the ByRef declaration in your VB.NET code, and just pass string parameters like this:

Function AlmacenarPedidoLipigas(<MarshalAs(UnmanagedType.LPStr)> telefono As String,
...

传递ByRef对于输出输出参数很有用,即被调用方可以修改参数,并且这些修改会反映给调用方,在此情况并非如此. (此外,在C/C ++中,此类参数往往需要更高级别的指针间接寻址.)

Passing ByRef can be useful for inout-output parameters, i.e. the callee can modify the parameter and these modifications are reflected to the caller, which is not the case here. (Moreover, such parameters tend to require an additional level of pointer indirection in C/C++.)

还请注意,从.NET Unicode字符串到"ANSI"字符串的转换可能有损;您可能要在C ++中将const wchar_t*用于Unicode UTF-16字符串,并在VB.NET中使用<MarshalAs(UnmanagedType.LPWStr)>.

Note also that the conversion from .NET Unicode strings to "ANSI" strings can be lossy; you may want to use const wchar_t* in C++ for Unicode UTF-16 strings, and <MarshalAs(UnmanagedType.LPWStr)> in VB.NET.