For example, if you want to act as if the user clicked the "close"
button in the corner, you have to make sure that the window is
visible and enabled and that the window even has a "close" button
to begin with!
Failing to observe these rules means that
the application you are manipulating is going to experience
"impossible" conditions.
The window may very well have been disabled
in order to avoid the problem of the unsupported stack frame.
Programmatically closing the window means that you've reintroduced
the bug that the application programmer was trying to avoid.

Similarly, if you want to simulate menu clicks, you need to check
whether the menu item is enabled because a user can't select
a disabled menu item.
Since most programs do not synchronize menu item states
until the menu is opened
(since the only way a user can invoke a menu item is to open the
enclosing menu first),
you need to open the menu before you check whether the item
is enabled.

In the referenced article, the program wanted to set foreground
activation to a particular window,
but it didn't check whether the window was enabled or even visible!
(And even if it were enabled and visible, it might be a
WS_EX_NOACTIVATE window.)

My goal here is not to come up with a comprehensive list of things
to check before simulating every type of action you might want.
That's something you need to figure out on your own:
Look at what you're trying to simulate and think through what
the window manager requires for that action to be accessible to the user.