When trying to use a P/Invoke declaration made in a VB.NET assembly from C# I noticed that string
arguments become ref string
arguments.
A closer inspection reveals that e.g.
Public Declare Unicode Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueW" ( _
ByVal hKey As IntPtr, ByVal lpValueName As String) As UInteger
is compiled to
[DllImport(...)]public static extern uint RegDeleteValue(
IntPtr hKey, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpValueName);
On MSDN I read: "VBByRefStr: A value that enables Visual Basic .NET to change a string in unmanaged code, and have the results reflected in managed code. This value is supported only for platform invoke. This is default value in Visual Basic for ByVal strings."
I still don't get it. Why is it only string lpValueName
in C# (see pinvoke.net - edit: for RegDeleteKey as Damien_The_Unbeliever pointed out, signature is the same like RegDeleteValue) but strange VBByRefStr
in VB.NET? Should I declare with <MarshalAs(UnmanagedType.LPWStr)>
in VB.NET to avoid ref
in C#? Or does that have any adverse effects?