A string that you pass to an unmanaged Win32 API in Visual Studio .NET or in Visual Studio 2005 returns empty (321078)
The information in this article applies to:
- Microsoft Visual Basic 2005 Express Edition
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual C# 2005, Express Edition
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
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 "References" 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 "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.
Visual C# .NET or Visual C# 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 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. - Replace the code in Class1 or in Program with the following code:
using System.Text;
using System.Runtime.InteropServices;
namespace SampleStringBuilder
{
class Class1
{
[DllImport("Kernel32")]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();
}
}
}
- Save, build, and then run the project. Notice that a console window opens and displays the computer name.
REFERENCES
For more information about the String and the StringBuilder classes, visit the following MSDN Web sites:
Modification Type: | Major | Last Reviewed: | 1/20/2006 |
---|
Keywords: | kbtshoot kbvs2005swept kbvs2005applies kbAPI kbprb kbString KB321078 |
---|
|