When you pass a String data type variable to a Win32 application programming interface (API) as an out parameter, the string that the Win32 function call returns does not change.
This problem occurs because you cannot modify Visual Basic .NET, Visual Basic 2005, and Visual C# strings. By default, you cannot change the String class after the String class is created. For more information about the String class, see the "References" section.
If you send a string to a Win32 API function, and if the function modifies or populates the string, use the StringBuilder class instead of the String data type.
For example, the following code samples demonstrate how to use the StringBuilder class with an out parameter of a Win32 API function call.
Visual Basic .NET or Visual Basic 2005 Console Application Sample
Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
On the File menu, point to New, and then click Project.
In the New Project dialog box, click Visual Basic Projects under Project Types, click Console Application under Templates, and then click OK.
Note In Visual Studio 2005, click Visual Basic under Project Types.
Replace the code in Module1 with the following code:
Imports System.Text
Imports System.Runtime.InteropServices
Module Module1
Public Declare Sub GetComputerName Lib "kernel32" _
Alias "GetComputerNameW" _
(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpBuffer As StringBuilder, _
ByRef nSize As Integer)
Sub Main()
Dim len As Integer, s As StringBuilder = New StringBuilder(50)
len = s.MaxCapacity
GetComputerName(s, len)
Console.WriteLine(s)
Console.Read()
End Sub
End Module
Save, build, and then run the project. Notice that a console window opens and displays the computer name.