Board index » delphi » Delphi2 - pointer via pointer,...

Delphi2 - pointer via pointer,...

Hi there!

I just want to say thanks to all of you who answered me about 'pointer via
pointer'...

p2^:=@B really works when B is variable, but when B is procedure, it does
not...

But here is how I did it:

type tMyProc = procedure (x,y:word);

...
procedure GetMyProc (pMyProc :pointer);
var p,d:dword;
begin
     pMyProc^:=@MyRealProc; <-- this won't work, compiler gives me:
                'Operator not applicable on this operand type' error...
but, this will:
     p=dword(@MyRealProc);
     d=dword(pMyProc);
     asm
        push eax
        push esi
        mov eax,p
        mov esi,d
        mov [esi],eax
        pop esi
        pop eax
        end;
end;

If I declare pMyProc as tMyProc (not pointer), then at
pMyProc^:=@MyRealProc, compiler gives me 'Not enough actual parameters'
error...

That's it... Using asm is not very ellegant solution, but atleast it works
:)

--

------------------------------------------------------------------------
                             Matija Tomaskovic
               <<< Mataya software - company in progress >>>

 mtoma...@barok.foi.hr               Matija Tomaskovic, Matije Gupca 25
 mtom...@jagor.srce.hr                Jalkovec, 42000 Varazdin, Croatia
 http://barok.foi.hr/~mtomasko     voice/modem/fax: ++385 (0)42 261-617
------------------------------------------------------------------------

 

Re:Delphi2 - pointer via pointer,...


Quote
Matija Tomaskovic wrote:
> procedure GetMyProc (pMyProc :pointer);
> var p,d:dword;
> begin
>      pMyProc^:=@MyRealProc; <-- this won't work, compiler gives me:
>                 'Operator not applicable on this operand type' error...

Have you tried:

    pointer (pMyProc^) := @MyRealProc;

This works.

Regards,

Erik.

--
Need a custom component? Late on a project? Could use an util?
DOS Device driver? A VxD? NT drivers or services?
Applications of any kind?
Low rates, fast delivery!

When responding to news postings, please CC a copy to my email address.
Thanks.
Erik Sperling Johansen <e...@info-pro.no>

Re:Delphi2 - pointer via pointer,...


Quote
Matija Tomaskovic wrote:
> p2^:=@B really works when B is variable, but when B is procedure, it does
> not...

> type tMyProc = procedure (x,y:word);

> ...
> procedure GetMyProc (pMyProc :pointer);
> var p,d:dword;
> begin
>      pMyProc^:=@MyRealProc; <-- this won't work, compiler gives me:
>                 'Operator not applicable on this operand type' error...
> but, this will:
>      p=dword(@MyRealProc);
>      d=dword(pMyProc);
>      asm
>         push eax
>         push esi
>         mov eax,p
>         mov esi,d
>         mov [esi],eax
>         pop esi
>         pop eax
>         end;
> end;

> If I declare pMyProc as tMyProc (not pointer), then at
> pMyProc^:=@MyRealProc, compiler gives me 'Not enough actual parameters'
> error...

Whoa there! I seem to have missed the start of this thread. However,
you shouldn't need to resort to ASM to do this. I'm not entirely clear
on what you are trying to do, but:

a) you cannot dereference p if p is declared as "pointer", because the
   compiler needs to know what p is a "pointer to".
e.g.
var
  p1: pointer;
  p2: ^integer;
begin
  Writeln( p1^ );  // This is an error
  Writeln( p2^ );  // This is OK; prints an integer

b) Procedural variables are a special kind of pointer.
Given your tMyProc type, you can do this:

procedure Foo(x1, x2: word);
begin
...
end;

var
  MyProc: tMyProc;
begin
  MyProc := Foo;
  MyProc(100,200);  // Same as Foo(100,200)

This works because Foo()'s declaration is 100% compatible with
your tMyProc type (including the calling convention). When
dealing with procedural variables, it is also important to
realise that:

p := @MyProc;  // p is assigned the contents of MyProc,
               // in this case, the address of Foo()
q := @@MyProc; // q is assigned the address of the
               // MyProc variable

Chris.

Re:Delphi2 - pointer via pointer,...


Quote
mtoma...@barok.foi.hr (Matija Tomaskovic) wrote:

I think maybe I'm missing something from the original post that you
referred to, but anyway...

Quote
>p2^:=@B really works when B is variable, but when B is procedure, it does
>not...

>But here is how I did it:
>type tMyProc = procedure (x,y:word);

>procedure GetMyProc (pMyProc :pointer);
>begin
>     pMyProc^:=@MyRealProc; <-- this won't work, compiler gives me:
>            'Operator not applicable on this operand type' error...

That's because you are trying to dereference a pointer type.  If you have a
PInteger and dereference it, that's fine because the compiler knows that
the underlying data begin pointed to is an integer.  But what type does a
"pointer" type point to?  The compiler doesn't know what to do with that.

Try this:

type
  pMyProc = ^tMyProc;
  tMyProc = procedure (x,y:word);

procedure MyRealProc(x,y:word);
begin
  { whatever }
end;

procedure GetMyProc (MyProc :pointer);
begin
  { case the pointer to the appropriate type and then dereference }
  pMyProc(MyProc)^:=@MyRealProc;
end;

Quote
>If I declare pMyProc as tMyProc (not pointer), then at
>pMyProc^:=@MyRealProc, compiler gives me 'Not enough actual parameters'
>error...

Right, because you can't dereference something that isn't a pointer.  How
about this instead:

procedure GetMyProc (MyProc :tMyProc);
var p,d:dword;
begin
     @MyProc:=@MyRealProc;
end;

Regards,
Brad
Free Delphi Stuff:  http://www.pobox.com/~bstowers/delphi/
bstow...@pobox.com (My return address is intentionally invalid; delete ".Remove-This-Spam-Blocker" for real address)

Other Threads