如何转换“字符串* 128" VB6到C#代码? [英] How to convert "string*128" of VB6 to C# code?

查看:223
本文介绍了如何转换“字符串* 128" VB6到C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB6的类型用户,如下所示:

I have a type-user of VB6 as follows:

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.

推荐答案

尝试:
[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);}
   }
}


据我所知,VB6 Long 等效于.NET Int32 类型,因此您应该尝试以下之一:

As far as I know the VB6 Long is equivalent to .NET Int32 type, so you should try one of the following:

[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;
}





[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;
}



您需要包括 System.Runtime.InteropServices 命名空间.



You need to include the System.Runtime.InteropServices namespace.


这篇关于如何转换“字符串* 128" VB6到C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆