Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a type-user of VB6 as follows:

VB
Private Type OSVERSIONINFO
    dwOSVersionInfoSize             As Long
    dwMajorVersion                  As Long
    dwMinorVersion                  As Long
    dwBuildNumber                   As Long
    dwPlatformId                    As Long
    szCSDVersion                    As String * 128
End Type


I can't convert all that codes to C# code (i think it is the same a struct in C#) at http://www.developerfusion.com/tools/convert/vb-to-csharp/[^], it reports an error. In that type it contains "szCSDVersion" member which is a string buffer with 128 spaces; please help me convert all to C# code. Thanks.
Posted
Updated 18-Oct-12 3:54am
v2

Try:
C#
[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFO
{
   private string szcsdversion = string.Empty.PadLeft(128);
   public long dwOSVersionInfoSize;
   public long dwMajorVersion;
   public long dwMinorVersion;
   public long dwBuildNumber;
   public long dwPlatformId;
   public string szCSDVersion
   {
     get {return szcsdversion;}
     set {szcsdversion = value.PadLeft(128);}
   }
}
 
Share this answer
 
v2
Comments
Andrewpeter 19-Oct-12 4:54am    
Thanks you, it works well. My vote is 5.
Kuthuparakkal 21-Oct-12 1:16am    
y're welcome!
Akinmade Bond 19-Oct-12 8:27am    
+5
Kuthuparakkal 21-Oct-12 1:16am    
thank you
As far as I know the VB6 Long is equivalent to .NET Int32 type, so you should try one of the following:

C#
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct OSVERSIONINFO
{
    public int dwOSVersionInfoSize;
    public int dwMajorVersion;
    public int dwMinorVersion;
    public int dwBuildNumber;
    public int dwPlatformId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string szCSDVersion;
}


C#
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct OSVERSIONINFO
{
    public int dwOSVersionInfoSize;
    public int dwMajorVersion;
    public int dwMinorVersion;
    public int dwBuildNumber;
    public int dwPlatformId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string szCSDVersion;
}


You need to include the System.Runtime.InteropServices namespace.
 
Share this answer
 
Comments
Andrewpeter 19-Oct-12 4:59am    
Yes, thanks you. It's great code. My vote is 5 for you. Kuthuparakkal gave me a solution above, although i accept your answer which is a solution which i need. Have fun!
Member 11964942 7-Sep-15 23:52pm    
how about convert to java? i have same problem.. please..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900