Find Component as TWincontrol at runtime

Hi,

I need to run a procedure, that takes a control for the first parameter.  The control is created at runtime.

Here's my procedure:

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
var
  lRequired: Integer;
begin
  lRequired := Integer(Required);
  SendMessage(aControl.Handle, BCM_SETSHIELD, 0, lRequired);
end;


Here's how I'm calling it it my program:

  SetElevationRequiredState( TWinControl(FindComponent('G4_Button7')), True);


When I run it in the debugger, I get something like:  "First chance exception at $7C812AEB. Exception class EAccessViolation with message 'Access violation at address 004BDF0F in module 'runtime.exe'. Read of address 000001B4'. Process runtime.exe (3224)"

How can I run this procedure using the control as the first parameter?
LVL 3
light_bulbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
you did't check for nil

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
var
  lRequired: Integer;
begin
  if aControl <> nil then
  begin
    lRequired := Integer(Required);
    SendMessage(aControl.Handle, BCM_SETSHIELD, 0, lRequired);
  end;
end;
Geert GOracle dbaCommented:
you could also directly cast the boolean
or use a const array

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
begin
  if aControl <> nil then
    SendMessage(aControl.Handle, BCM_SETSHIELD, 0, Integer(Required));
end;

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
const IRequired: array[Boolean] of integer = (0, 1);
begin
  if aControl <> nil then
    SendMessage(aControl.Handle, BCM_SETSHIELD, 0, IRequred[Required]);
end;
Geert GOracle dbaCommented:
and are you sure the button is allways a TWinControl
(not a TSpeedButton) ?

procedure TForm1.Test;
var aComponent: TComponent;
begin
  aComponent := FindComponent('G4_Button7');
  if aComponent is TWinControl then
    SetElevationRequiredState(TWinControl(aComponent), True);
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

light_bulbAuthor Commented:
I think your third answer may at least get me on the right track here.  The control I wanted to send the message to does not descend from TWinControl, thus my problem: It doesn't have a handle and can't receive messages directly.

What I'm trying to do here is send a message that will enable the UAC shield for a control in Vista.

I wonder if there's a way to send a message to such a control?
Geert GOracle dbaCommented:
it depends
if it's a Delphi descendant of TControl you can do

aControl.Perform(Msg: Cardinal; WParam, LParam: Longint): Longint;

light_bulbAuthor Commented:
It not, apparently, I can't use the Perform method with it.

Thanks for your help.  I can see with this custom component that it not going to do what I want.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.