Solved

Follow Up: Indy | Records, and Typecasts

Posted on 2004-07-30
4
183 Views
Last Modified: 2010-04-05
Hello there,
This Q is a Follow up of http:Q_21063476.html

I tried to add a field to the TLoginSizes and as soon as I did it the compiler stopped at "TLoginSizes(mr.DataLen).UsernameLen:=Length(Edit1.Text);" with the message "invalid typecast"

type
  TLoginSizes = record
    UsernameLen: Word;
    PasswordLen: Word;
    HexArraylen: Word;     //    New field...
  end;

  TMsgRec = record
    RecType: Byte;
    Command: Byte;
    DataLen: Cardinal;
  end;
0
Comment
Question by:Cerf
  • 3
4 Comments
 
LVL 8

Author Comment

by:Cerf
Comment Utility
This Q has turned into "extremely urgent" for me... Raising to 500p
0
 
LVL 8

Author Comment

by:Cerf
Comment Utility
I see what you wrote previously,

>"it's a kindof an optimization .. I only send one record and the data once with an existing record type
DataLen is a cardinal field - 4 bytes
that TLoginSizes record has two word fields - 2x 2bytes
so the datasize is the same and using that typecast it can represent various things
if you take a look at TRect you'll see a similar thing"

and I guess it is about  the size of cardinal and 3 word fields = 6bytes but I don't really understand what are you doing there...
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
Comment Utility
Use packed records...

TLoginSizes and TMsgRec might appear to be only 6 bytes byt don't have to be, just because the way Delphi aligns these fields. It's not a very safe typecast anyway and will be invalid in future Delphi versions so you better not use it. PErhaps a variant record is a better option. Example:

type
  TYourRecord = record
    case boolean of
      False: (RecType: Byte; Command: Byte; DataLen: Cardinal);
      True: (UsernameLen: Word; PasswordLen: Word; HexArraylen: Word);     //    New field...    
  end;

Or something similar. I don't have Delphi available right now so can't check...

If you're using global variables, you might also think of using the 'absolute' keyword:

var
  LoginSizes: TLoginSizes;
  MsgRec: TMsgRec absolute LoginSizes;

As a final solution, consider using pointers.
0
 
LVL 8

Author Comment

by:Cerf
Comment Utility
Thanks Alex!

I already fixed it changing a bit the comm protocol between the server and the clients to a simplier thing. As a friend told me...
"KISS" keep it simple, stupid :) so I did and works fine.
I only send the header record (TMsgRec) which is fixed size for sure and don't do any typecasting... the rest is interpreted and manipulated by the server. However, your post makes sense.

Thanks for interesting,

Cërf.

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now