Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Typecasting a pointer to a record

Posted on 1998-09-02
15
Medium Priority
?
473 Views
Last Modified: 2010-04-04
A routine is expecting a type PBYTE which is defined:
   PBYTE = ^Byte;
   procedure MyProc (buffer : PBYTE);

I need to pass it a pointer to a packed record:
  PHeader = ^THeader;
  THeader = packed record
  end;

e.g.
   pMyHeader : PHeader;
   MyProc(pMyHeader);

I'ev tried many options for typecasting, and the only one the compiler agrees with is:
   MyProc( @(pMyHeader^) );

This doesn't seem right.
Any one has a better suggestion?
0
Comment
Question by:vang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +3
15 Comments
 

Author Comment

by:vang
ID: 1338451
Edited text of question
0
 

Expert Comment

by:chrismo
ID: 1338452
This seems a bit cleaner:

MyProc(PByte(pMyHeader));
0
 

Author Comment

by:vang
ID: 1338453
Thank you for your quick response.
However, I must reject your propsed answer.
I've tried it before, and I get an "Incompatible types" error message.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:chrismo
ID: 1338454
Is this at run-time, or compile? Mine compiled okay ... maybe I didn't set it up correctly.
0
 

Author Comment

by:vang
ID: 1338455
It is at compile time.
0
 

Expert Comment

by:chrismo
ID: 1338456
Do you have some source you could send? If so, send it to chrismo@workmail.com and I'll see if it compiles for me...
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1338457
vang

I was having the same problema at one stage and I found this on the net helped me out, maybe it will help you too...

unit Unit1;

                              interface

                              uses
                                SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
                                Forms, Dialogs, StdCtrls;

                              type
                                TForm1 = class(TForm)
                                  Button1: TButton;
                                  Button2: TButton;
                                  procedure Button1Click(Sender: TObject);
                                  procedure FormCreate(Sender: TObject);
                                  procedure FormDestroy(Sender: TObject);
                                  procedure Button2Click(Sender: TObject);
                                private
                                  List : TList;
                                end;

                              var
                                Form1: TForm1;

                              implementation

                              {$R *.DFM}

                              type
                                TPersonRec = record
                                  Name: string;
                                  Age: Byte;
                                end;
                                PPersonRec = ^TPersonRec;

                              procedure TForm1.Button1Click(Sender: TObject);
                              var
                                PersonRec : PPersonRec;
                              begin
                                New(PersonRec);
                                try
                                  PersonRec^.Name := 'Rick Rogers';
                                  PersonRec^.Age := 29;
                                  List.Add(PersonRec);
                                except
                                  Dispose(PersonRec);
                                  raise;
                                end;
                              end;

                              procedure TForm1.FormCreate(Sender: TObject);
                              begin
                                List := TList.Create;
                              end;

                              procedure TForm1.FormDestroy(Sender: TObject);
                              var
                                I : Integer;
                              begin
                                for I := 0 to (List.Count - 1) do Dispose(PPersonRec(List[I]));
                                List.Free;
                              end;

                              procedure TForm1.Button2Click(Sender: TObject);
                              var
                                PersonRec : PPersonRec;
                              begin
                                PersonRec := PPersonRec(List[0]);
                                ShowMessage(PersonRec^.Name);
                              end;

                              end.

Its from a post on the forums from Rick Rogers

Later
BoRiS
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1338458
You need to change.....this is actually just a guess....you can not typecast a PByte as a THeader...maybe packed record of PByte  or something similar.....

PBYTE = ^Byte;
      procedure MyProc (buffer : PBYTE); //Change this to procedure MyProc(Buffer : THeader^);
     PHeader = ^THeader;
     THeader = packed record
     end;
      pMyHeader : PHeader;
      MyProc(pMyHeader);

   I'ev tried many options for typecasting, and the only one the compiler agrees with
   is:
      MyProc( pMyHeader ); //or MyProc(@pMuHeader);..not exactly sure which one should be

0
 

Author Comment

by:vang
ID: 1338459
I'm sending...
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1338460
Try "MyProc(pointer(pMyHeader));"

Regards, Madshi.
0
 

Author Comment

by:vang
ID: 1338461
Maby I didn't understand your answer.
Can you please pin point me?
0
 

Author Comment

by:vang
ID: 1338462
Victornet,
"MyProc" is not actualy mine.
I can't change its expected type.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1338463
vang,

>> Maby I didn't understand your answer.
>> Can you please pin point me?

Who are you to talking to? To me? Have you tested my suggestion?

Regards, Madshi.
0
 
LVL 3

Accepted Solution

by:
rickpet earned 200 total points
ID: 1338464
The easiest way is to pass pointers...you can use a pointer to point to any pointer type here's an example...No casting needed.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
 PBYTE = ^Byte;
 PHeader = ^THeader;
 THeader = packed record
   MyValue: integer;
 end;


type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Header: THeader;

implementation

{$R *.DFM}

procedure CallProc(pRecord: pByte);
var
  tmpPointer: pointer;
  tmpHeader: PHeader;
begin
  tmpPointer := pRecord;
  tmpHeader := tmpPointer;
  ShowMessage(IntToStr(tmpHeader^.MyValue));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Header.MyValue := 99;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  tmpPointer: pointer;
  tmpHeader: PHeader;
begin
   tmpPointer := @Header;
   CallProc(tmpPointer);
end;

end.

0
 

Author Comment

by:vang
ID: 1338465
This answer is agreeable, though the code still looks a bit messy, at least to me.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

597 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