DEPHI 我不会,VB和C++我会,我也用过winsock控件.在C++里也有和你一样的问题,这个主要是编译时的数据对齐问题,肯定是你编译是设置成字对齐了.我看你的结构用了字节成员,如字对齐的话,在编译时它不是占一个字节,而是2个字节.

解决方案 »

  1.   

    zitjubiz: 你好! 
        我是今天才到这个论坛的,正好看到一个我以前碰到过的类似问题,所以我就回答了,希望对提问的人有所帮助,我不清楚这里的规矩,是不是不能随便回答.另外,我问一下,你是谁啊?是不是斑竹?
      

  2.   

    我在VB程序中定义了相应的结构却不能正确的接收
    public type TRealData
        tag as byte
        GateWayID as integer
        CCUID as byte
        DPUID as byte
        CHID as byte
        Alevel as byte
        Value as single
        ValueS as  String*20
        AlarmTS as String*20
        AlarmIS as String*20
    end type  
      

  3.   

    public type TRealData
        tag as byte
        GateWayID as Long
        CCUID as byte
        DPUID as byte
        CHID as byte
        Alevel as byte
        Value as single
        ValueS as Byte(20)
        AlarmTS as Byte(20)
        AlarmIS as Byte(20)
    end type  同时你可能要用到LPWSTRtoBSTR这个函数(我的贴子里有)
      

  4.   

    Bardo:
    能否给出LPWSTRtoBSTR函数呀
      

  5.   

    Private Declare Sub CopyMemory Lib "kernel32" Alias _
            "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As _
            Long, ByVal cbCopy As Long)
            
    Private Declare Function lstrlen Lib "kernel32" Alias _
            "lstrlenA" (ByVal lpString As String) As Long
    Private Function BSTRtoLPSTR(sBSTR As String, b() As Byte, lpsz As Long) As Long' Input: a nonempty BSTR string
    ' Input: **undimensioned** byte array b()
    ' Output: Fills byte array b() with ANSI char string
    ' Output: Fills lpsz with a pointer to b() array
    ' Returns byte count, not including terminating null
    ' Original BSTR is not affectedDim cBytes As Long
    Dim sABSTR As StringcBytes = LenB(sBSTR)' ReDim array, with space for terminating null
    ReDim b(1 To cBytes + 2) As Byte' Convert to ANSI
    sABSTR = StrConv(sBSTR, vbFromUnicode)' Point to BSTR char array
    lpsz = StrPtr(sABSTR)' Copy the array
    CopyMemory b(1), ByVal lpsz, cBytes + 2' Point lpsz to new array
    lpsz = VarPtr(b(1))' Return byte count
    BSTRtoLPSTR = cBytesEnd FunctionPrivate Function BytePhyStrToHex(BytePhyStr() As Byte) As String'Input:Byte array of MAC
    'Output:Hex string of MACDim TempStr As String
    Dim TempByteStr As String
    Dim i As Integer
     
     'The MAC Length only is 6 bytes, so we only get first 6 bytes.
     
     For i = 1 To 6
         TempByteStr = Hex(BytePhyStr(i))
         If Len(TempByteStr) = 1 Then
            TempByteStr = "0" & TempByteStr
         End If
         TempStr = TempStr & TempByteStr
     Next i
     
     BytePhyStrToHex = TempStrEnd FunctionPrivate Function LPSTRtoBSTR(ByVal lpsz As Long) As String' Input: a valid LPSTR pointer lpsz
    ' Output: a sBSTR with the same character arrayDim cChars As Long' Get number of characters in lpsz
    cChars = lstrlen(lpsz)' Initialize string
    LPSTRtoBSTR = String$(cChars, 0)' Copy string
    CopyMemory ByVal StrPtr(LPSTRtoBSTR), ByVal lpsz, cChars' Convert to Unicode
    LPSTRtoBSTR = Trim0(StrConv(LPSTRtoBSTR, vbUnicode))End FunctionPrivate Function Trim0(sName As String) As String
       ' Right trim string at first null.
       Dim x As Integer
       x = InStr(sName, vbNullChar)
       If x > 0 Then Trim0 = Left$(sName, x - 1) Else Trim0 = sName
    End Function其实,这个在 MSDN 中就有!!!        
      

  6.   

    抱歉代码中多贴了这么一个函数。Private Function BytePhyStrToHex(BytePhyStr() As Byte) As String'Input:Byte array of MAC
    'Output:Hex string of MACDim TempStr As String
    Dim TempByteStr As String
    Dim i As Integer
     
     'The MAC Length only is 6 bytes, so we only get first 6 bytes.
     
     For i = 1 To 6
         TempByteStr = Hex(BytePhyStr(i))
         If Len(TempByteStr) = 1 Then
            TempByteStr = "0" & TempByteStr
         End If
         TempStr = TempStr & TempByteStr
     Next i
     
     BytePhyStrToHex = TempStrEnd Function