tags:

views:

358

answers:

6

In other words, can I count on a control ID as a reliable identifier?

From some reading I've done, it sounds like .NET controls can have control IDs that change with every run, is this so for Win32 apps as well, or are they something that's hardcoded in the source?

The window/control in question is actually an Internet Explorer dialog if that helps.

+3  A: 

The answer is "it depends on the circumstances, but in the majority of programs, these will not change across multiple executions." In general, a control ID or resource ID will be the same across every execution of the same program.

In most implementations, resources are stored in the resource section of the PE executable and are assigned a resource ID within that data structure. Usually, the developer specifies the resource in a .rc file.

The exceptional case is via APIs such as CreateDialogIndirect() which allow IDs specified through the API at runtime. Such dynamic creation is uncommon, however. Consistency of resource IDs and control IDs is the expected condition, so even in the case of the CreateXXXIndirect() API, users of the API would be ill-advised to chose a varying ID.

Heath Hunnicutt
That said, I do not belive that holds true for Win32 dialogs created from .NET - .NET apps (apparently) use some kind of string based property system for accessing (.net wrappers of) dialog controls as such I believe the actual controls have dynamically assigned ids.
Chris Becke
+1  A: 

It depends on the control. Controls that are dynamically created could have a different ID every time they are created. Controls based on static dialog resources will have static IDs.

Even if a dialog is backed by a dialog resource template, controls can be added dynamically at runtime and those controls could have dynamically generated IDs.

sean e
+4  A: 

In general win32 dialog resource IDs do not change when you run the app. However, they are internal implementation details and as such they are subject to change whenever an update (patch, service pack, major release) to the application is made.

In general all of IE's dialogs use hard-coded control IDs. There may be some that are dynamic. If you give the specific control I might be able to give you a better answer.

jeffamaphone
+1 for the "update" note - in some apps, apparently the IDs are re-generated on each (production?) build.
Piskvor
+1  A: 

Depending on your intent, it may be acceptable to store and reuse it for future lookups or traces; but in general it isn't safe.

If the window is based upon a dialog template, items declared in the template generally don't change. It is perfectly safe under typical circumstances to use these as identifiers.

Sometimes the window class name or a portion of it can be used as an identifier instead, depending on the window host.

If the dialog is one of the standard prompts from internet explorer, you may want to use text stored in adjacent controls or the dialog caption as additional verification info (if localized versions of IE are not an issue). If the dialog is a window that embeds an instance of MSHTML/IE; none of these options may be viable- but you can use OLE accessibility to get at the document shown and then browse the DOM from there.

meklarian
+2  A: 

As a general rule, no they don't change between runs. Control IDs are usually specified with a dialog template, and that's a static resource compiled into an .exe or .dll. Controls can also be created using a regular call to CreateWindow or CreateWindowEx. In such cases, the ID is usually a constant, but it could be anything in principal (even a random value).

In any case, if you're planning to muck around with a dialog in another application, then you are asking for trouble. Control IDs can and do change between different versions of a program.

Peter Ruderman
They may not change between runs but they certainly change between releases (and service packs and potentially monthly updates).
Larry Osterman
That's what I was trying to say in the last line. Anyway, thanks for the clarification, Larry. I'm a big fan. :)
Peter Ruderman
+2  A: 

Microsoft has spent years trying to deal with applications which embed assumptions about internal windows implementation details. One of the biggest causes of compatibility problems for IE8 was caused by applications which made assumptions about the window order of IE controls. When the UI was changed in IE8, the controls moved and a number of browser plugins broke hideously.

In general you should never ever make assumptions about controls in an application that you didn't write - your code WILL break in the future (not might break, will break).

Larry Osterman