Privileged Instruction?

Privileged Instruction?

Hi my friends.

I have a number of frames in my app and I would like to change the properties of controls by calling a procedure. So, I have this code:

procedure TModifiedFrame.setProperties(ClassName, SomeProperty: string;
Value: Integer);
var
i: integer;
PropInfo: PPropInfo;
Component: TComponent;
begin
for i := 0 to ComponentCount - 1 do
begin
Component := Components[i];
if (Component is TControl) and ((Component.ClassName = trim(ClassName))
or (Trim(ClassName) = 'AllComponents')) then
begin
PropInfo := GetPropInfo(Component.ClassInfo, SomeProperty);
if Assigned(PropInfo) then
SetOrdProp(Component, PropInfo, Integer(Value));
end;
end;
end;

I call it like this: MyFrame.setProperties('TLabel','Height',5) to test it.

I am facing two Qs here:

1. I get a run-time error: Privileged instruction at (addr) at SetOrdProp line. Am I trying to access something I shouldn't?

2. I can see how the GetPropInfo and setOrdProp work with zero-level properties (eg. 'Height', 'Visible', etc) but do they retrieve data for other level of properties such as TextSettings.FontColor?

Thanks

Comments

  1. Ok, I use RTTI and I do it like this:

    var
    context: TRttiContext;
    aProp: TRttiProperty;
    begin
    ....
    if (tmpComponent is TControl) and ((tmpComponent.ClassName = trim(ClassName))
    or (Trim(ClassName) = 'AllComponents')) then
    begin
    prop:=context.GetType(tmpComponent.ClassType).GetProperty(Trim(SomeProperty));
    if Assigned(prop) then
    prop.SetValue(tmpComponent, Value);
    end;
    ....

    The above works for uniquely identified properties such as Height, Text and even FontColor (although it is TextSettings.FontColor). Now, I can't access properties with same names. For example, I want to go to TextSettings.Font.Size but it conflicts with the Size property. Anyone knows how to do this using RTTI?

    ReplyDelete
  2. Just out of curiosity, what version of Delphi are you using?

    ReplyDelete
  3. David Schwartz 10.1 with update 1. Does the demo work in previous versions?

    ReplyDelete

Post a Comment