[Win32] Addresses

Started by
10 comments, last by Ectara 10 years, 10 months ago

As an example, the SendMessage(hWnd, CB_GETCURSEL, 0, 0) function returns the selected index of a combo box.
If my purpose is to read this index multiple times, I should call this function multiple times. Or don't I?

Instead, is there an address where these kind of stuff are stored so I can and use it for making things easier?

Advertisement

No, Windows does not offer you access to the address of the internal variables it uses to implement the functionality of the various controls. That's a good thing, as some of these may correspond directly to internal variables, while others are probably inferred and are calculated based on other variables. Even strings are returned by copying rather than by addresses. You can always just retrieve the value at the beginning of a function and cache it for the rest of the function.

No, Windows does not offer you access to the address of the internal variables it uses to implement the functionality of the various controls. That's a good thing, as some of these may correspond directly to internal variables, while others are probably inferred and are calculated based on other variables. Even strings are returned by copying rather than by addresses. You can always just retrieve the value at the beginning of a function and cache it for the rest of the function.

This, and how it works internally may change between versions of Windows. It is horrific to see programmers get a handle from a Windows function, subtract a magic number from it, and dereference it to get data that they're looking for out of thin air. This will essentially do that, after all is compiled and shipped, and people trying to figure out why it doesn't work on Windows 9 will come looking for you.

No, Windows does not offer you access to the address of the internal variables it uses to implement the functionality of the various controls. That's a good thing, as some of these may correspond directly to internal variables, while others are probably inferred and are calculated based on other variables. Even strings are returned by copying rather than by addresses. You can always just retrieve the value at the beginning of a function and cache it for the rest of the function.

This, and how it works internally may change between versions of Windows. It is horrific to see programmers get a handle from a Windows function, subtract a magic number from it, and dereference it to get data that they're looking for out of thin air. This will essentially do that, after all is compiled and shipped, and people trying to figure out why it doesn't work on Windows 9 will come looking for you.

That is insane! Has anyone ever written code like that? I know that handles are supposed to behave as opaque identifiers under Windows but do people actually go hunting information by dereferencing it? This.. like.. misses the entire point of handles and component encapsulation. (although I have to agree I have seen better interfacing mechanisms than messages)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

If using ATL, atlctrls.h has a nice helper method for getting the selection.

If not, then you can easily write your own little helper method that at least saves you from having to pass in all of those arguments.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms



No, Windows does not offer you access to the address of the internal variables it uses to implement the functionality of the various controls. That's a good thing, as some of these may correspond directly to internal variables, while others are probably inferred and are calculated based on other variables. Even strings are returned by copying rather than by addresses. You can always just retrieve the value at the beginning of a function and cache it for the rest of the function.

This, and how it works internally may change between versions of Windows. It is horrific to see programmers get a handle from a Windows function, subtract a magic number from it, and dereference it to get data that they're looking for out of thin air. This will essentially do that, after all is compiled and shipped, and people trying to figure out why it doesn't work on Windows 9 will come looking for you.

That is insane! Has anyone ever written code like that? I know that handles are supposed to behave as opaque identifiers under Windows but do people actually go hunting information by dereferencing it? This.. like.. misses the entire point of handles and component encapsulation. (although I have to agree I have seen better interfacing mechanisms than messages)
Yeah, I've seen crazy code like that. People would rather offset from an unknown pointer than use calls to established interfaces, and a lot of people offset from a pointer to an object to get to private member variables and virtual functions. Additionally, see Raymond Chen's take on it:
http://blogs.msdn.com/b/oldnewthing/archive/2003/12/23/45481.aspx

Windows also uses handles to abstract objects so they can be relocated internally whenever it feels the need, doesn't it?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Khatharr, on 28 May 2013 - 21:53, said:
Windows also uses handles to abstract objects so they can be relocated internally whenever it feels the need, doesn't it?

Properly, yes. For the most part, it is a complete abstraction, perhaps an index into a pointer table, that allows for reallocation or reimplementation. Some nefarious APIs don't follow this, but most do.

http://blogs.msdn.com/b/oldnewthing/archive/2003/12/23/45481.aspx

Wow. I don't know what is worse, the developers doing crazy stuff like that, or microsoft encouraging them by patching the OS for it to still work...

http://blogs.msdn.com/b/oldnewthing/archive/2003/12/23/45481.aspx

Wow. I don't know what is worse, the developers doing crazy stuff like that, or microsoft encouraging them by patching the OS for it to still work...

It's less encouragement to continue, and more wanting to sell copies of Windows because people will blame Windows and refuse to update if their old software no longer works. No matter how important it is to teach a lesson, in the end, if it means losing sales to do it, it probably isn't worth it.

This topic is closed to new replies.

Advertisement