|
-
Dec 7th, 2001, 12:03 PM
#1
Thread Starter
Member
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.
-
Dec 7th, 2001, 01:00 PM
#2
Thread Starter
Member
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.
-
Dec 7th, 2001, 01:11 PM
#3
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
-
Dec 7th, 2001, 05:20 PM
#4
Thread Starter
Member
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.
-
Dec 7th, 2001, 05:54 PM
#5
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.
-
Dec 10th, 2001, 10:34 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|