简体   繁体   English

使用 LSet 从 VB6 到 C#?

[英]Using LSet from VB6 to C#?

I'm currently converting VB6 to C#.我目前正在将 VB6 转换为 C#。 Can you help me convert this code?你能帮我转换这个代码吗?

Here's the VB6 code:这是VB6代码:

'This converts the bytes into test parameters
Sub getParamValues(ByRef TransData() As Byte, ByRef testparam() As Single, startbyte As Integer)
Dim tmpdata As bByteType
Dim convertedvalue As SingleType
Dim i As Integer
Dim bytecounter As Integer
bytecounter = startbyte

'On Error Resume Next
For i = 0 To 9
    tmpdata.bBytes(0) = TransData(bytecounter + 3) 'TransData(0 + 3)
    tmpdata.bBytes(1) = TransData(bytecounter + 2) 'TransData(0 + 2)
    tmpdata.bBytes(2) = TransData(bytecounter + 1) 'TransData(0 + 1)
    tmpdata.bBytes(3) = TransData(bytecounter)     'TransData (0)

    'THIS CODE I WANT TO CONVERT
    LSet convertedvalue = tmpdata 

    testparam(i) = convertedvalue.dResult 'Gets the test parameters
    bytecounter = bytecounter + 4
Next i
End Sub

and this和这个

Private Type bByteType
    bBytes(3) As Byte
End Type

Private Type SingleType
    dResult As Single
End Type

I tried my best to convert this into C#.我尽力将其转换为 C#。 But I'm getting a NullException.但我得到一个 NullException。 I just can't convert the Type from Vb6 into C#.我只是无法将Type从 Vb6 转换为 C#。 So, I tried struct .所以,我尝试了struct But I have no idea how to transfer the bBytes data into tmpdata using C#.但我不知道如何使用 C# 将bBytes数据传输到tmpdata

public void getParamValues(ref byte[] TransData, ref Single[] testparam, int startbyte) 
    {
        bByteType tmpdata = new bByteType();
        SingleType convertedvalue = new SingleType();
        //byte[] bBytes = new byte[4];
        int bytecounter = 0;
        bytecounter = startbyte;

        for (int i = 0; i < 9; i++)
        {
            tmpdata.bBytes[0] = TransData[bytecounter + 3];
            tmpdata.bBytes[1] = TransData[bytecounter + 2];
            tmpdata.bBytes[2] = TransData[bytecounter + 1];
            tmpdata.bBytes[3] = TransData[bytecounter];
            //LSet convertedvalue = tmpdata <--- Supposed to convert to C#                
            testparam[i] = convertedvalue.dResult;
            bytecounter = bytecounter + 4;
        }
    }

public struct bByteType
    {
         //byte[] bBytes = new byte[3];
        public byte[] bBytes;
        public bByteType(byte[] size)
        {
            bBytes = new byte[4];
        }
    }

    struct SingleType
    {
        public Single dResult;
    }

The VB6 code is swapping over the byte order to convert between big-endian and little-endian. VB6 代码正在交换字节顺序以在大端和小端之间进行转换。 LSet in VB6 copies the byte values from one structure (Type ) to another even when the structure definitions are completely different.即使结构定义完全不同,VB6 中的LSet将字节值从一种结构 (Type ) 复制到另一种结构。 The binary data from one variable is copied into the memory space of the other, without regard for the data types specified for the elements.一个变量的二进制数据被复制到另一个变量的内存空间中,而不考虑为元素指定的数据类型。 Gulp!呸!

The best way to do this in C# is something like this answer .在 C# 中执行此操作的最佳方法类似于此答案

It would be much more complicated in C# to copy the byte values from one structure to another - for instance you would need to pin the structures in memory to stop them moving around partway through the operation.在 C# 中将字节值从一个结构复制到另一个结构会复杂得多 - 例如,您需要将结构固定在内存中以阻止它们在操作中途移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM