这和vb处理数珠的,变量的机制有关。我是用的字符串,这样更好
Public Type SENDFILE
    ToAddress  As String * 32
    FileSize     As Long
    FileTime    As SYSTEMTIME
    FileNameLen  As Long
    FileName    As String * 64
End Type
Public Type SENDFILE_REQ
    Command     As Long
    lParam      As Long
    wParam      As Long
    Reserve     As Long
    MyAddr      As String * 32
    SF      As SENDFILE
End Type
还有哦
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

解决方案 »

  1.   

    典型的内存空洞现象的例子内存空洞就是长度对齐,一般的在c中struct a
    {
    int i;
    char c;
    };
    sizeof( struct a ) = 8而不等于5,因为char 型变量被对齐了,但是只能使用一个字节,出现了三个空洞。在vb中我不知道如何获得type类型的长度,可以简单的把不是4(或者2,或者8)的整数倍的长度凑齐对应的长度。消息结构一:(总长度22+N个字节)
       字节1-4:消息体总长度 AS LONG
       。  5-8: ... 
       。  8-12: ...
       。
       字节13:N的长度
              14: 用于对齐,无用
              15: 用于对齐,无用
              16: 用于对齐,无用
       。
       。
       。
       字节25-(25+N):变长字符串
      

  2.   

    可是问题在于Type test
      Totallength as long
      MSGID as long
      MSG() as byte
    End Type '///////////////////
      dim a as test
      a.Totallength=12
      a.MSGID=1
      Redim a.MSG(3) as byte
      a.msg="abcd"
     假定对TEST赋值完毕后
      '///////////////////  dim DataSend(lenb(test)-1) as byte  call CopyMemory(byval varptr(DataSend(0)),byval varptr(test),lenb(test))  DataSend(0)、DataSend(1)的值应该都为0
      可是在我的程序中却为0、1
      现在发现是在这段代码之前的某处。有一条个变量的值为1。SO DataSend(1)的值为1
     如果将那个变量的值赋为N,则DATASEND(1)的值随之变动。
      

  3.   

    上面代码的这两行有错误:
      dim DataSend(lenb(test)-1) as byte  call CopyMemory(byval varptr(DataSend(0)),byval varptr(test),lenb(test))
    应该是:
      Redim DataSend(lenb(test)-1) as byte  call CopyMemory(byval varptr(DataSend(0)),byval varptr(test)-3,lenb(test))
      

  4.   

    我发现lenb(a)=12,说明a.msg是一个地址,所以copymemory需要分两部
    1) dim ttt as string
       ttt = "fdsafdsafdsafdsa"
       a.msg = ttt
    2) copymemory( byval varptr(datasend(0)), bybal varptr( test ), 8 )
    3) copymemory( byval varptr(datasend(8)), byval varptr( ttt ), len( ttt ) )
      

  5.   

    你定义的type在c语言中相当于这个
    struct test
    {
       long a;
       long b;
       char *c;
    };
    struct test a;
    char t[ 10 ] = "demo";
    a.a = 12
    a.b = 88;
    a.c = t;
    copymemory 后是把t的地址传送过去了,在c中解决方法是
    struct test
    {
        long a;
        long b;
        char t[ 10 ];
    }
    struct test a;
    a.a = 18;
    a.b = 88;
    memset( a.c, 0, 10 );
    strcat( a.c, "demo" );
    copymemory( ... );但愿我说的没错。
      

  6.   

    不是吧
    Type test
      Totallength as long   '= 4*Byte
      MSGID as long         '= 4*Byte
      MSG() as byte         'UNKNOW NOW
    End Type
    //////////
      Redim a.MSG(3) as byte  'NOW =4*Byte
      a.msg="abcd"
    //////
    所以lenb(a)等于12啊
      

  7.   

    问题是。使用COPYMEM从结构拷贝数据到数组中时会出错。
      

  8.   

    这和 Vb的处理变量的机制有关,我很多时候用汇编来处理这些东西的,封dll
    Type test
      Totallength as long
      MSGID as long
      MSG as string 
    End Type
      

  9.   

    以前我用winsock发送数据是也碰到这个问题,后来我将错就错的解决了,看来不止我一个人碰到这样的困惑。
      

  10.   

    C中的Sizeof和VB中的Len相似,Len应该可以得到TYPE的大小。
      

  11.   

    把call CopyMemory(byval varptr(DataSend(0)),byval varptr(test),lenb(test))
    改为
    call CopyMemory(DataSend(0),test,len(test))
    试一下Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)