Results 1 to 6 of 6

Thread: struct : C++-->VB

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    C2C
    Posts
    42

    Question struct : C++-->VB

    Structure :C++ <--> VB?
    Hi,

    My Vb app needs to call Standard DLL(using C++). I get confused with structure data type conversion. Here are two questions:

    Q(1): Structure in C++:

    struct User
    {
    WORD Id;
    BYTE Spare[2];
    WCHAR Name[64];
    }

    How to convert to VB type?why need Spare here?

    (2) structure in Vb:

    Type Product
    Id as byte
    Name as String
    End type

    How to define this structure in C++?

    Wainting for your instructions. thanks.
    Hai Kuo Ren Yu Yao. Tian Gao Ren Niao Fei.

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    C2C
    Posts
    42
    byte alignment problem? can someone explain what it is and how to solve it?

    Thanks.
    Hai Kuo Ren Yu Yao. Tian Gao Ren Niao Fei.

  3. #3
    jim mcnamara
    Guest
    Code:
    struct User 
    { 
       WORD Id; 
       BYTE Spare[2]; 
      WCHAR Name[64]; 
    } 
    
    In vb is:
    Option Base 0
    Type User
        Integer Id
        spare(2) as Byte
        Name as string *64
    End Type

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    C2C
    Posts
    42
    thx, Jim.

    I need to get an array of User Structure from Dll, so is it the reason that this C++ structure have to declare a spare?(actuallly I just need user id and user name). What if this struct don't have spare[2] elment?

    I think using sapre here wastes memory.

    HOPE you can answer the second question.
    Hai Kuo Ren Yu Yao. Tian Gao Ren Niao Fei.

  5. #5
    jim mcnamara
    Guest
    Wasting memory is NOT the point. You have two choices:
    1. declare it so it works
    2. declare so it doesn't work

    Pick one.

    If you use your struct and pass "e-mulan" to the procedure, the procedure will skip the first bytes of the name and find
    "ulan" instead. As an example.

    Secondly, this isn't 1968 when things ran in 8K core. You can waste 150 bytes on a few data structures and it won't matter.
    Usually the stuff like 'spare' is in there to aligne that data with some internals OS structure we have no knowledge about.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    structure alignment is for speed reasons.
    80386 upwards are 32-bit cpus, meaning they like 32 bit or multiples best. A WORD is only 16-bit, so the string would start at an unaligned position. This requires the cpu to use 2 cycles for reading the data instead of one - an unnecessary time waste. By declaring two bytes (16 bits) between those members you can align them on "DWORD boundaries". You gain a little speed. It actually has got nothing to do with the OS.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width