Board index » delphi » Extract Icon Error

Extract Icon Error

Hi

I have recently moved some Delphi 3 code to Delphi 4, I am trying to extract
the number of icons that an exe file has by using the following code,

var
i:integer;
begin
 i := ExtractIcon(HInstance, pchar('c:\winnt\notepad.exe'), -1);
end;

according to the example by setting the last value as -1 it should return
the number of icons, this works fine in D3 but when trying to compile in D4
a "Constant expression violates subrange bounds" error is given,

does anyone know what I am doing wrong, and how I can correct it,

thanks.

 

Re:Extract Icon Error


On Tue, 19 Jun 2001 12:55:20 +0100, "Merlin" <bcjoyn...@hotmail.com>
wrote:

Quote
>"Constant expression violates subrange bounds"

The error message is 100% correct, in as far as what it is reporting.
If you look at the ExtractIcon import declaration in your ShellApi
unit, you'll see that it is declared as:

function ExtractIcon(hInst: HINST; lpszExeFileName: PChar;
  nIconIndex: UINT): HICON; stdcall;

And UINT is declared in the Windows unit as:

  UINT = LongWord;

The subrange for the LongWord type from the help is documented as:

  0..4294967295

Hence, the -1 constant violates the subrange bounds.

This is the why. The underlying problem is that the ExtractIcon
declaration does not allow for -1, yet the Windows API clearly does
allow for -1. Is this a Delphi or Windows error? Sort of both. The
Windows API declaration clearly shows that the nIconIndex parameter is
a UINT -- an UNSIGNED integer. You can't assign negative values to an
unsigned integer. This is how Delphi implemented the import
declaration. But yet the API documentation also clearly allows -1, so
both Delphi and Windows are wrong to use UINT.

Now, how to fix this? I *think* this will work:

procedure TForm1.Button1Click(Sender: TObject);
var
  IconIndex: UINT;
  TotalIcons: Integer;
begin
  IconIndex := High(UINT);
  TotalIcons := ExtractIcon(Application.Handle,
     PChar(Application.ExeName), IconIndex);
  ShowMessage(Format('Total icons: %d', [TotalIcons]));
end;
--
Rick Rogers (TeamB); Fenestra Technologies, http://www.fenestra.com/
  Use Borland servers; posts via others are not seen by TeamB.
  For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Re:Extract Icon Error


Sorry,

I have now found a fix for this

ExtractIcon(hInstance, PChar(strFile), Cardinal(-1))

thanks

Quote
"Merlin" <bcjoyn...@hotmail.com> wrote in message news:3b2f3cb6$1_1@dnews...
> Hi

> I have recently moved some Delphi 3 code to Delphi 4, I am trying to
extract
> the number of icons that an exe file has by using the following code,

> var
> i:integer;
> begin
>  i := ExtractIcon(HInstance, pchar('c:\winnt\notepad.exe'), -1);
> end;

> according to the example by setting the last value as -1 it should return
> the number of icons, this works fine in D3 but when trying to compile in
D4
> a "Constant expression violates subrange bounds" error is given,

> does anyone know what I am doing wrong, and how I can correct it,

> thanks.

Re:Extract Icon Error


Quote
"Rick Rogers (TeamB)" wrote:
> Now, how to fix this? I *think* this will work:

>   IconIndex := High(UINT);
>   TotalIcons := ExtractIcon(Application.Handle,
>      PChar(Application.ExeName), IconIndex);

Huh? Any reason for not simply typecasting -1? (Not meant retorically, I
really want to learn here.)

And, as a sidenote, do you know if
 - your solution costs extra stackspace
 - or does it get optimized
 - or does the typecast solution in fact take up stackspace too?

Joris

Re:Extract Icon Error


On Tue, 19 Jun 2001 15:54:06 +0200, Joris Van Damme

Quote
<as.van.damme.jo...@planetinternet.be> wrote:
>simply typecasting -1?

Try it.
--
Rick Rogers (TeamB); Fenestra Technologies, http://www.fenestra.com/
  Use Borland servers; posts via others are not seen by TeamB.
  For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Re:Extract Icon Error


Quote
Joris Van Damme wrote:
> And, as a sidenote, do you know if
>  - your solution costs extra stackspace
>  - or does it get optimized
>  - or does the typecast solution in fact take up stackspace too?

Didn't mean stackspace for the call, of course, I was talking about the
workspace for the calling procedure.

Re:Extract Icon Error


Quote
"Rick Rogers (TeamB)" wrote:
> > try it.

This isn't a quote. I read your message saying 'try it', I tried it, it
wasn't a problem. (Would have been surprised otherwise, I use this all
the time, I was - and still am - merely wondering wether you had any
particular reason I should now about.)

Returned here to answer your answer, but oops, the answer was gone...
You leave me feeling like Alice in Wonderland. Does my particular
implementation of ExtractIcon have some strange effect on the borland
newsgroup server? ;-)

Hirus

Re:Extract Icon Error


On Tue, 19 Jun 2001 16:39:24 +0200, Joris Van Damme

Quote
<as.van.damme.jo...@planetinternet.be> wrote:
> Returned here to answer your answer, but oops, the
> answer was gone...

Well, I followed my own advice, and tried it, realized that
typecasting works fine, so I canceled my message shortly after posting
it.
--
Rick Rogers (TeamB); Fenestra Technologies, http://www.fenestra.com/
  Use Borland servers; posts via others are not seen by TeamB.
  For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Re:Extract Icon Error


Quote
"Rick Rogers (TeamB)" wrote:
> so I canceled my message shortly after posting
> it.

That's kinda like almost having a time travelling machine! :-)

Seriously though, Rick, how about the stack issue? I wish I had more
inside knowledge on optimization... so I was kinda hoping you'd comment
on that.

Joris

Re:Extract Icon Error


On Tue, 19 Jun 2001 21:44:53 +0200, Joris Van Damme

Quote
<as.van.damme.jo...@planetinternet.be> wrote:
> so I was kinda hoping you'd comment on that

Sorry, no idea. I would have commented if I had something useful to
add.
--
Rick Rogers (TeamB); Fenestra Technologies, http://www.fenestra.com/
  Use Borland servers; posts via others are not seen by TeamB.
  For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Other Threads