• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Access violation exception when modifying tray icon!

Hi,

I have a problem when modifying my tray icon! The strange thing is, that it works at some point in my program, but at another point excatly the same code fragment does not work!!!
Another thing is, that it does work in Delphi 3, but not in Delphi 7!

The exception is: Access viloation at address ... Write of address ...

It appears when excecuting the following code (the exception does not appear when removing this code):

    ImageList1.GetIcon(1,Icon);
    tnid.hIcon := Icon.Handle;

There is an ImageList with a icon at index 1. I can read the icon at a different point of my program.

The icon is initialized with:

  HMainIcon                := LoadIcon(MainInstance, 'MAINICON');

  Shell_NotifyIcon(NIM_DELETE, @tnid);

  tnid.cbSize              := sizeof(TNotifyIconData);
  tnid.Wnd                 := handle;
  tnid.uID                 := 123;
  tnid.uFlags              := NIF_MESSAGE or NIF_ICON or NIF_TIP;
  tnid.uCallbackMessage    := WM_NOTIFYICON;
  tnid.hIcon               := HMainIcon;

  Shell_NotifyIcon(NIM_ADD, @tnid);


Thanks for your help!
0
Bohne
Asked:
Bohne
  • 3
  • 2
1 Solution
 
andrewjbCommented:
In

ImageList1.GetIcon(1,Icon);

Has 'Icon' been created properly? GetIcon needs an existing object to put the answer into.
0
 
tkalchevCommented:
How much icons do you have in the imagelist ?
ImageList1.GetIcon(1,Icon) loads the Second icon from the ImageList. Is it possible that you have only One icon inside ?
0
 
BohneAuthor Commented:
... I had a variable "var Icon: TIcon;". This was definitively not correct! So I removed it and I don't create it now. And it works! ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
andrewjbCommented:
Why a 'C' ?? Seems spot on to me.. Ho hum.
0
 
BohneAuthor Commented:
...you told me "GetIcon needs an existing object to put the answer into".
But it did not work as I declared it as an object! It did not work until I removed the declaration! So your answer was not more than "Average". And "C" means "Average"...

I didn't want to offend you...
0
 
andrewjbCommented:
Just to be clear on what's going on... you must have 'Icon' declared somewhere else. GetIcon retrieves the image you want and puts it into the object supplied. Your original code would have worked if you'd done something like...

var Icon : TIcon;
begin
  Icon := TIcon.Create( ... );
  ImageList1.GetIcon( 1 , Icon );
...
end;

The reason it wasn't working was that you're 'Icon' variable was declared as local, but was never attached to an instantiated object.

What I said was completely correct - GetIcon needs an existing object to put its answer into (as opposed to a pointer variable which it could set to point to some new TIcon that GetIcon creates for you internally - which isn't how it works).

Anyway. Not to worry.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now