In Delphi, how do you call a private property read function from inside an asm statement

Viewed 114

This line of Pascal code:

gh := GCanvas.Handle

(where GCanvas is a variable of type TCanvas)

converts to this in assembly language (as viewed in the CPU window):

mov eax,[GCanvas]
call TCanvas.GetHandle
mov [gh],eax

GetHandle is the private function that returns the value of FHandle defined in Graphics.pas

How can I access the TCanvas Handle property inside an asm statement? I tried the call as above but the compiler returns "Undeclared identifier 'GetHandle'" So how do you access properties where the value is returned by a function call?

Any help, please?

2 Answers

If the property accessor method is PRIVATE then the simple answer is: You can't. The compiler simply doesn't know that symbol at that point in your code. Even from Pascal, you can't, except by going via RTTI and that is probably too cumbersome to do from assembly.

The way to do it is to provide a "gateway" function that calls on to the property (which in turn calls the private function):

FUNCTION GetHandle(C : TCanvas) : HDC; Register;  // Must be a Global function //
  BEGIN
    Result:=C.Handle
  END;

and then from your assembly code:

VAR GCanvas : TCanvas;
VAR gh : THandle;

ASM
        MOV     EAX,GCanvas
        CALL    GetHandle
        MOV     [gh],EAX
END

If you make the GetHandle function a method of a class, you need to pass "Self" (the instance of the class) in EDX to the function as well.

I also found this this morning - if I end the asm statement to go back to Pascal and use the line

GCanvas.Handle;

this calls the GetHandle function and the result is left in eax - then enter a new asm statement and eax can be stored or pushed as needed.

Related