Microsoft KB Archive/321078

= A string that you pass to an unmanaged Win32 API in Visual Studio .NET or in Visual Studio 2005 returns empty =

Article ID: 321078

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q321078



SYMPTOMS
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.



CAUSE
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 &quot;References&quot; section.



RESOLUTION
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 &quot;kernel32&quot; _ Alias &quot;GetComputerNameW&quot; _ ( 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.</ol>

Visual C# .NET or Visual C# 2005 Console Application Sample
<ol> Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, click Visual C# Projects under Project Types, click Console Application under Templates, and then click OK.

Note In Visual Studio 2005, click Visual C# under Project Types.</li>  Replace the code in Class1 or in Program with the following code: using System.Text; using System.Runtime.InteropServices; namespace SampleStringBuilder {   class Class1 {       [DllImport(&quot;Kernel32&quot;)]public static extern bool GetComputerNameW ([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpBuffer, ref int lpnSize); static void Main(string[] args) {           StringBuilder SB = new StringBuilder(50); int I = SB.MaxCapacity; GetComputerNameW(SB,ref I); System.Console.WriteLine(SB); System.Console.Read; }   } }                      </li> Save, build, and then run the project. Notice that a console window opens and displays the computer name.</li></ol>

<div class="references_section">