Re: Undeclared Identifier 'Handle' error

Posted by webmaster Guido on December 20, 2002

In Reply to: Undeclared Identifier 'Handle' error posted by Lontek on December 19, 2002

: I followed the directions for opening an external program here and I get oh so close. But I keep getting an Undeclared identifier 'Handle'error. I found that I had to add 'ShellAPI' in the 'uses' definitions and this took care of the other errors, but not this one.
: Am I perhaps missing a different 'uses' library?
: Or could it be because I am using Delphi2?
: I am finding that very little (that I can find) is written about that 'handle' statement right after Shellexecute(Handle...) Even searching on the internet for about an hour has yielded very little.

Did you try the code from within a routine that is a "member" of a form? Please read on, it will become clear.

Just try the following, in order to start "from scratch":
- create a new test application with a blank form, and add ShellApi to the "uses" clause;
- add a button, and in the OnClick event handler of the button put this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(Handle, 'open', PChar('calc.exe'), nil, nil, SW_SHOW);
end;

The "Handle" in this example is the handle of *Form1*. The code is in fact a shorthand notation for:

ShellExecute(self.Handle, 'open', PChar('calc.exe'), nil, nil, SW_SHOW);

Because "self" refers to the form, we can omit it (the procedure Button1Click is a "member" of Form1). But if you call ShellExecute from a procedure that is not a member of a form, like this:

procedure StartCalculator;
begin
  ShellExecute(Handle, 'open', PChar('calc.exe'), nil, nil, SW_SHOW);
end;

...then it doesn't work, because the variable "Handle" is not declared in the procedure. Conclusion: undeclared identifier.

Please test the example with the button, and let us know in the Forum what happens.

Source code :: Tips