Welcome to MSDN Blogs Sign in | Join | Help

Who decides what can be done with an object or a control?

This is one of those things that is obvious to me, but perhaps is not obvious to everyone. An object establishes what can be done with it. Any rights granted by the object go to the creator. The creator can in turn grant rights to others. But if you're a third party to the object/creator relationship, you can't just step in and start messing around without the permission of both the object and the creator.

For example, unless you have permission of the creator of a list view control, you can't go around adding, removing, and changing items in the list view. The creator of the list view decides what goes in it, and at most you can look at it to see what the creator decided to put in it. I say "at most" because you often can't even do that: If the fact that the item is a list view control is not public, then the program that created the list view might decide to use some other control in a future version, and then your code that tries to look at the list view will stop working.

Naturally, any private data associated with a control (such as the LPARAM associated with a list item) is entirely under the purview of the control's creator. The creator of the control decides what the item data means, and that can change from version to version if not explicitly documented. (For example, the meaning of the item data associated with the main list view in Explorer windows has changed at pretty much every major release of Windows.)

Generally speaking, without the permission of the creator of a control and the control itself, you can't do anything to it. You can't hide it, show it, move it, change its scroll bars, mess with its menus (assuming it even uses menus at all), change its text, or destroy it. The code that created the control and the control itself presumably maintain their own state (the control maintains state about itself, and the creator maintains state about what it has done to the control). If you start messing with the control yourself, you may find that your changes seem to work for a while and then are suddenly lost when the control decides to re-synchronize its internal state with its external state. Or worse, things will just be permanently out of sync, and the program will start acting strange. If the control and the control's creator have not provided a way to coordinate your actions with them, then you can't mess with the control.

If you're tempted to go mess with somebody else's controls, think about how you would like it if the tables were turned. How would you feel if somebody wrote a program that took the controls in your program and started messing with them, say adding items to your list box and using the item data to extract information out of your program? What if they started filing bugs against you for changing the way your program operates internally?

Published Tuesday, February 05, 2008 7:00 AM by oldnewthing
Filed under:

Comments

# re: Who decides what can be done with an object or a control?

Tuesday, February 05, 2008 5:34 PM by Michael

If something like "the object you are attempting to [perform actions upon] defines what it can do" is not obvious to a programmer, then they're in the wrong business.

Isn't understanding object core to actual non-trivial programming?

# re: Who decides what can be done with an object or a control?

Tuesday, February 05, 2008 6:23 PM by Merus

And yet how many people dig into the internals of Windows in order to be able to do something? While I think every programmer would understand it when asked, not everyone keeps it in mind.

# re: Who decides what can be done with an object or a control?

Tuesday, February 05, 2008 7:39 PM by Tod

The answer does seem obvious.

However, reality is never so simple.

On far too many occasions I hear about bugs in libraries that I designed. After investigating, it turns out that other teams (who have access to the source code) have altered my libraries in the following ways: 1) changed private methods to be public 2) added new methods to existing interfaces, or 3) added new methods exposing internal structures.

When asked why they do this, I am told various reasons such as that they needed access to internal structures or doing it any other way would take too long etc.

And when I revert my libraries, existing code ceases to compile, so I am tasked to "fix" that too as if it is my fault...

# re: Who decides what can be done with an object or a control?

Tuesday, February 05, 2008 8:54 PM by Burak KALAYCI

> without the permission of the creator of a control and the control itself, you can't do anything to it.

I think that's fundamentally wrong. But, it will be equally wrong to expect it to work with a new version.

Do I need any permission from the creator to remove ads from any site while browsing? If the page does not render nice after the ad picture removed, that will be my problem too. I can't expect the creator to fix it now, or ever.

I won't generally mess with someone else's controls, but I can do it if I want, and I won't need a permission, no. BUT, at the same time, I won't expect any support for my little hack from the creator in the future.

And if the tables were turned, I won't feel obliged to support any hack to my code. If somebody files a bug, I'd just say that this is how things work, this is not a bug, rewrite your code, get a life.

And this is so obvious to me...

I may suspect that some internal info about an object is not being supplied to me (or offered for any price), but to others, which creates an unfair situation. Certain functionality may not be possible without messing with internals of someone else's object. This may be a valid reason to do it. In that case, you may expect the internals will change even just to make your application incompatible (if your assumption was wrong, of course).

I remember Microsoft's defense about DR-DOS not being compatible with MS-DOS for running Windows after checking internal structures. You can't be 100% compatible unless you are identical.

You should try to respect creator's intentions, in order to be able to expect future compatibility.

# re: Who decides what can be done with an object or a control?

Wednesday, February 06, 2008 1:12 AM by Myria

Most of the time, I would agree that you don't want to mess with program internals.  However, sometimes the API is poorly designed, or there is a very useful feature that is missed in the API's design.

Certain functions in ntdll.dll come to mind.  They can do a lot of things Win32 can't.

# re: Who decides what can be done with an object or a control?

Wednesday, February 06, 2008 6:03 AM by ti

Wait, you want to say that lParam of listview items no loner a pidl?

# re: Who decides what can be done with an object or a control?

Wednesday, February 06, 2008 9:44 AM by Dan

Despite all this, I find AutoIt is a fun tool for manipulating other Windows programs.  I made a download monitor that tracked several programs and reported files and file progress and download speed by reading control text from different windows.  Granted it was easy to break (reordering listview columns would be enough) but it was also pretty easy to fix given AutoIt's scripting language syntax.

Sadly, AutoIt doesn't work with some programs, based on exactly the points made in the article.  For example, in XPSP2, the taskbar refuses to be resized by AutoIt to, for example, stretch across multiple monitors.  I'm guessing it blocks the resize in WM_WINDOWPOSCHANGING or whatever.

# re: Who decides what can be done with an object or a control?

Wednesday, February 06, 2008 11:28 AM by JS

The degree to which one Windows application is allowed to mess with another has always surprised me.  I've noticed this ability has been pared back somewhat in Vista, presumably because the "programmers will use their powers for good" maxim continues to be proven false...

New Comments to this post are disabled
 
Page view tracker