?
Solved

Access violation exception when modifying tray icon!

Posted on 2003-03-29
6
Medium Priority
?
366 Views
Last Modified: 2010-04-04
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
Comment
Question by:Bohne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 100 total points
ID: 8237723
In

ImageList1.GetIcon(1,Icon);

Has 'Icon' been created properly? GetIcon needs an existing object to put the answer into.
0
 
LVL 9

Expert Comment

by:tkalchev
ID: 8238994
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
 

Author Comment

by:Bohne
ID: 8246529
... 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.

 
LVL 12

Expert Comment

by:andrewjb
ID: 8246549
Why a 'C' ?? Seems spot on to me.. Ho hum.
0
 

Author Comment

by:Bohne
ID: 8248369
...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
 
LVL 12

Expert Comment

by:andrewjb
ID: 8252475
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question