VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

Hi
This might be me lacking some fundamental knowledge again….

I suspect the problem that I need help with is that I don’t understand how a VBA If __ And __ Then code line behaves in this form:
If (Big number) And (other big number) Then

This is a bit related to some other threads where I am messing with API stuff, but I think I am perhaps stuck on a specific bit of
If …… And logic, although maybe understanding/ seeing the actual code line might help:
The VBA If __ And __ Then logic that I am having trouble with is coming from this actual code line
If lngStyle And mcWSVISIBLE Then
, where I figured out already that both variables are big numbers, and the second is a fixed number, so the line can also be written
If lngStyle And 268435456 Then

The full coding from which the actual code line comes, I have got from here
https://eileenslounge.com/viewtopic.php ... 78#p321978
API: Get Class name of a running app

(In the meantime, I have that full coding mostly all sussed out , I think…. I think I know what it is doing and how, but I am just stuck on figuring the logic of that one line )
I have done a lot of Debug.Print research to see what typical number values are actually typically finding their way into that code line: As I mentioned, the second number is a fixed big number, 268435456; The first number is typically big, positive or negative
So to try and isolate the problem to investigate it, I have a test range, which includes some of those numbers typically finding their way into that code line. https://i.postimg.cc/KznwpLTM/sample-fo ... -logic.jpg
sample for strange If And logic.JPG
The test range shown in that last screenshot is in the second worksheet "MiscTests", of the uploaded workbook:
I apply the pseudo logic of
If (B Value) And (C Value) Then put "True" in D
The results in column C are so far completely constant with the behaviour of the actual code line, when those actual numbers find their way into the actual code line
For the actual sample range in the uploaded file, either of the following two codings, (which are in the second worksheet’s code module) will get those results.

Code: Select all

 Option Explicit
Sub TestStrangeIfAndLogic()
Range("D2:D20").Clear
Dim Rw As Long
    For Rw = 2 To 20
        If Range("B" & Rw).Value And Range("C" & Rw).Value Then Let Range("D" & Rw) = "True?"
    Next Rw
End Sub
Sub TestStrangeIfAndLogicArr()
Range("D2:D20").Clear
Dim Rw As Long, arrRws() As Variant: Let arrRws() = Range("A1:C20").Value
    For Rw = 2 To 20
        If arrRws(Rw, 2) And arrRws(Rw, 3) Then Let Range("D" & Rw) = "True?"
    Next Rw
End Sub
For the actual full coding from which that line is based on, the right final results are got,and all is well with the final results, so that code line is doing what it is supposed to, but it is the logic of how it gets those results that I don’t understand.

What am I missing?

Alan
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 17 Nov 2024, 21:51, edited 2 times in total.
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16919
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Strange? VBA code line… If And Then … logic with 2 big numbers

Post by ChrisGreaves »

DocAElstein wrote:
14 Nov 2024, 15:53
This might be me lacking some fundamental knowledge again….
I have been finding that a lack of fundamental knowledge has been the root cause of every problem in my life. That may not help you,m but it sure helps me every day!
I suspect the problem that I need help with is that I don’t understand how a VBA If __ And __ Then code line behaves in this form: ...
My first thought is that in VBA, TRUE is often known as a numeric value "negative one"
But you might want to start by pasting some of your numeric values into this snippet:-

Code: Select all

Sub test()
    If -1 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If
End Sub
Cheers, Chris
Last edited by ChrisGreaves on 14 Nov 2024, 17:43, edited 1 time in total.
Never panic in a room that holds a computer.

User avatar
HansV
Administrator
Posts: 80207
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Strange? VBA code line… If And Then … logic with 2 big numbers

Post by HansV »

The And operator in VBA applied to numbers performs a bitwise AND of the binary representation of the numbers.

1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0

Also in VBA

If condition Then this Else that

executes this if condition is anything but 0, and that if condition is 0.

Let's take a simple example:

4 in binary is 100
3 in binary is 011
So 4 And 3 evaluates to 000 = 0 which acts as False in If ... Then

4 in binary is 100
6 in binary is 110
So 4 And 6 evaluates to 100 = 4 which acts as True in If ... Then
Best wishes,
Hans

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: Strange? VBA code line… If And Then … logic with 2 big numbers

Post by DocAElstein »

Hi Chris,
I have often thought getting the very fundamentals not fully understood is the source of all life on the planet. If all the fundamentals were all perfectly known then we would all be finished. Lack of fundamental knowledge might be responsible for life. Someone decided to start life to get at all knowledge.
Or maybe some words to all that approximate effect is the answer to all…


I tried your idea, just in a slightly different way, applying this to my test range of numbers

Code: Select all

 Sub Chris1() '  https://eileenslounge.com/viewtopic.php?p=322154#p322154
Dim Rung As Range
    For Each Rung In Range("B2:C15")
        If Rung.Value Then
         Let Rung.Offset(0, 4) = "Trueish"
        Else
         Let Rung.Offset(0, 4) = "Falseisch"
        End If
    Next Rung
    
End Sub 

Everything was Trueish : https://i.postimg.cc/0Q01Prkw/All-Trueish.jpg

I tried something very similar to what you said as well

Code: Select all

 Sub Chris2()
' Column C
Debug.Print 268435456,
    If 268435456 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If
Debug.Print

' Column B
Debug.Print -2080374717,
    If -2080374717 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print -1811939328,
    If -1811939328 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 79691776,
    If 79691776 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 382664704,
    If 382664704 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print -2046820352,
    If -2046820352 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print -1778384896,
    If -1778384896 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 114229248,
    If 114229248 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 213909504,
    If 213909504 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 886013952,
    If 886013952 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print -1946157056,
    If -1946157056 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 349110272,
    If 349110272 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

Debug.Print 885981184,
    If 885981184 Then
        Debug.Print "true"
    Else
        Debug.Print "false"
    End If

End Sub
Results, all true again

Code: Select all

 268435456    true

-2080374717   true
-1811939328   true
 79691776     true
 382664704    true
-2046820352   true
-1778384896   true
 114229248    true
 213909504    true
 886013952    true
-1946157056   true
 349110272    true
 885981184    true
_._________________________
HansV wrote:
14 Nov 2024, 17:05
The And operator in VBA applied to numbers ....
Thanks Hans

…… I am going now to think about that….. As that chap said as he left the Tent…. I may be some time….
If I don’t come back, then the Bavarian Winter got me before I was finished... :snow:
Alan
By the way, that coding you pointed me to was very useful. I tried to do something just like that myself the day or two before, but did not quite get it, so thanks again for that
Last edited by DocAElstein on 14 Nov 2024, 17:50, edited 1 time in total.
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16919
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Strange? VBA code line… If And Then … logic with 2 big numbers

Post by ChrisGreaves »

DocAElstein wrote:
14 Nov 2024, 17:28
If all the fundamentals were all perfectly known then we would all be finished.
My high-school Physics teacher Mister Puzey conveyed all of Physics to us in one sentence: All there is, is Energy, and all we have is Time and Space.
I forget now how we spent the rest of fourth, and fifth year. :grin:

Hans's explanation of IF LOGIC is (as usual) way better than mine.
Cheers, Chris
Never panic in a room that holds a computer.

User avatar
SpeakEasy
5StarLounger
Posts: 753
Joined: 27 Jun 2021, 10:46

Re: Strange? VBA code line… If And Then … logic with 2 (big) numbers

Post by SpeakEasy »

>in VBA, TRUE is often known as a numeric value "negative one

Not quite true; that's more of an artifact of the internal representations of numbers and the data types that VBA supports. In VBA, FALSE is 0& and anything not FALSE is TRUE, so anything not 0& is TRUE ( and the logical internal representation of NOT 0& is &HFFFFFFFF (I.e all the bits in the four bytes are set to 1) which VBA treats as -1 ( since longs are signed values)

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: Strange? VBA code line… If And Then … logic with 2 (big) numbers

Post by DocAElstein »

I suppose Chris’s defence might be he said "known as"
I have heard others say things like in "VBA true seems to be -1", and I remember that when hearing that, it always was setting some thought of caution off in my mind, like, … "Hello, something sounds a bit dodgy/ uncertain there"
Maybe there is a small advancement for us and VBA mankind here?…… In VBA it’s something of an artefact that True is -1


…(..Getting the rest clear in my mind is going unusually much easier and quicker than I expected, I will probably be finished by dinnertime and it's not even started snowing yet, ( -Some sun flowers hidden behind my old VW Bus are in fact still looking OK. They are all late this year, - in the next bit of my occupied safety zone, a bit further down the road, a row of Sun Flowers have still barely flowered yet, maybe they will be there by Xmas ? ) ....) .....

One other small thing I was not expecting, and I am not sure if is an artefact or needs some other description/ explanation: So far any two negative numbers in the VBA And always give me True. In my current musings that is easy to take care of by extending the binary number in string character form comparison* ( * aka the VBA’s Bit wise performing ) to include the first sign character which is 1 for negative number I believe. So then at that first position from the left, 1 and 1 in the same horizontal position when two negative binary numbers are considered, gives the first excuse to take the answer as True
That gets me the correct result for that case of both negative numbers in the VBA And, but I am not sure if some better explanation/ description would be more appropriate, or informative/ enlightenuing, for how/ why both negative numbers in the VBA And should always give True.?
My initial guess had been to ignore the first sign character when doing the Bit wise performing
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
HansV
Administrator
Posts: 80207
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Strange? VBA code line… If And Then … logic with 2 (big) numbers

Post by HansV »

The first bit of a negative number is always 1, so Anding two negative numbers results in a negative number too.
Any non-zero number is treated as True in If ... Then.
Best wishes,
Hans

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

2's flippin compliment +1 ?? (- sounds like the name of a trendy pop group)

Post by DocAElstein »

DocAElstein wrote:
15 Nov 2024, 07:14
.....Getting the rest clear in my mind is going unusually much easier and quicker than I expected, I will probably be finished by dinnertime ...
I didn’t die in the snow, or get food poisoning from my dinner either, .. but it took a while longer.
I didn’t quite have the full story….
….My thoughts so far had been, (which a quick bit of internet research seemed initially to confirm was OK) , .. I needed to set my pair of decimal numbers up in a binary form like this, ( I know actual numbers would be longer – I am just showing shorter ones for clarity )

Code: Select all

0 0 1 1 0 1
0 0 1 0 1 1 
and if those last two numbers where negative, it might look like

Code: Select all

1 0 1 1 0 1
1 0 1 0 1 1 
Then I would just need to look for any position where both numbers have a 1, and then that is the True situation


To get it clear in my mind I thought I would try to make a function/ functions to mimic the VBA And working in the situation / code line that sparked this all off
I thought I had a simple job to do, _just two fairly simple things:
_(a) a function to convert big decimal numbers to binary ,
_(b) a function to find if at any position in the two binary number representation both numbers had a 1, which would then be the True situation

I did it quite quick

_ The decimal to binary even I could figure out from my school maths memory, - the exact details of the decimal to binary conversion function I did will bore the socks off all the smart people here, but it’s basically all based on the school maths way of converting a decimal to binary….
….Take the decimal number and divide it by a number got from a high power of 2;
___ If that division comes out >= 1 , then you have your first binary digit, starting from the left of 1; then you subtract that number got from a high power of 2 from the decimal number , so the decimal number under consideration is now smaller
___ Else when that division comes out < 1 , then you have your first binary digit of 0 starting from the left. You leave the decimal number at the size it is.
Now you do the same again using the next power of 2 down as the denominator to get the next binary digit to the right. Eventually you are down to (2 ^0) to get the last binary digit at the right
_A function to check for the true situation was very simple. Go left to right, and consider the corresponding same digit in each binary number. At any first find of both having a 1, and you are true.

Hmm…. I thought it was too easy to be True,- pun intended there. It ‘aint that easy to be getting True in the VBA And situation that we have been considering.
It came down to my binary representation not always being correct, even though my first function was OK, or rather it was doing what I intended it to do. (In the second function there was not much that could be wrong).

( Its Chris Greaves fault. ( he is innocent, but as he is in this thread, I take the opportunity to blame him as , a) he knows I love him really, and b) he sometimes telly me stories related to important things in the development of computing and he forgot to tell me the story of why some bored computer mathematician came up one day with the crazy idea of the 2s compliment binary representation of negative numbers ) )

The problem was: As everyone except me knows, in most computer binary stuff, to show the binary representation of any negative decimal number, you take what the binary would be if the same decimal number was positive, then invert all numbers , then finally do a binary add of 1 to the inverted number.

So I thought about that , and then modified my decimal to binary function to allow for the 2s Compliment codswallop , and all was well, :)


It was not all wasted effort. I think looking at the ( correct ) binary numbers used / found in codings like that one , might reveal something interesting about that API Hwnd handle number thingy:
I already got the point that the Hwnd is a temporary number , semi - randomly generated to give any running software an identification number to find a window.
I think I also figured it won’t help someone knowing it to find a window in another running software, as that running software will get given a new set of Hwnds – ( this is a point that some tutorials I recently seen seem to have missed )
But the number is possibly generated in such a way that looking at the (correct) binary representation, allows you to know something, for example, what type of window, or what level, or whatever. I am guessing that knowing that sort of thing is what the author of that coding was exploiting in that code line, whose workings was the main subject/ cause of this thread,
If lngStyle And mcWSVISIBLE Then

I will have to have a good think about that, but much later. … I am going outside again…. and again..... I may be some time…. its not the snow this time, but characteristically for a windy day this time of year, there is a lot of leaves from the forest that is my garden, to sweep away…


Alan




Edit: I had a wild dream after I laid down for a long kip after collecting leaves. I was in a new trendy pop group, 2 stunning lady singers up front and I was at the back on my drum kit. We were top of the charts doing really well. We called ourselves, "2’s flippin’ compliment + 1". Unfortunately I woke up just before the TV interview where they would ask me where the Band’s name originated. So at least you won’t get attacked by the Music Press Bots trying to find me….
Last edited by DocAElstein on 17 Nov 2024, 22:36, edited 1 time in total.
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

Hi
I think I can wrap this one up nicely: I have the code line that this all started from,
If lngStyle And mcWSVISIBLE Then
,understood, at least enough for now.

The WSVISIBLE or similar constant names, is usually a number 268435456, ( a round number actually in binary bits and pieces - (2^28) ) It is to do with the visibility of a window. Approximately said, its probably to do with the main windows we see.

Based on all the musings, and finally getting my binaries the right way around , ( or upside down depending on your 2's compliment way of thinking), I can use the functions on my original sample data in this compact coding to get a summarised set of results.
Coding

Code: Select all

 Sub hWndVisibleExperiments()   '    https://eileenslounge.com/viewtopic.php?p=322270#p322270
Debug.Print " -/+ 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1  0     ----- N as in (2^N)"
Debug.Print "  " & Replace(StrConv(String:=NumberInBinary2sCompliment(268435456), Conversion:=vbUnicode), Chr(0), "  ") & "   -----   WSVISIBLE  is typically a decimal constant 268435456 = 2^28 "   '
Dim Rung As Range
    For Each Rung In Range("B2:B15")
    Debug.Print "  " & Replace(StrConv(String:=NumberInBinary2sCompliment(Rung.Value2), Conversion:=vbUnicode), Chr(0), "  ") & NumbersInVBAIf_And_Then(Rung.Value2, 268435456) '     https://www.eileenslounge.com/viewtopic.php?p=297329#p297329      https://www.vbforums.com/showthread.php?526299#post3252316
    
    Next Rung
End Sub
That will get these immediate window
Results

Code: Select all

 -/+ 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1  0     ----- N as in (2^N)
  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0     -----   WSVISIBLE  is typically a decimal constant 268435456 = 2^28 
  1  0  0  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  1  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  1  1  Falsch
  1  0  0  1  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Wahr
  1  0  0  0  0  1  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  0  0  0  0  0  1  0  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  0  0  0  1  0  1  1  0  1  1  0  0  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Wahr
  1  0  0  0  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  1  0  0  1  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Wahr
  0  0  0  0  0  1  1  0  1  1  0  0  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  0  0  0  0  1  1  0  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  0  0  1  1  0  1  0  0  1  1  0  0  1  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Wahr
  1  0  0  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Falsch
  0  0  0  1  0  1  0  0  1  1  0  0  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Wahr
  0  0  1  1  0  1  0  0  1  1  0  0  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  Wahr
What that is saying in words is… When a semi random number is generated for the handle of a main visible window, the algorithm or whatever computer innards generates it, makes sure it has a 1 in the binary of that number at the position for the power of 28. We can exploit that knowledge to get all the handles of the main visible windows, which is what that code line is doing. So now I know how its doing it, :)

( All the functions, test data ranges etc. is in the spredsheet and code module of worksheet MiscTests in the uploaded file)
Alan
You do not have the required permissions to view the files attached to this post.
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
SpeakEasy
5StarLounger
Posts: 753
Joined: 27 Jun 2021, 10:46

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by SpeakEasy »

>2s compliment .. some bored computer mathematician (or as you say elsewhere 'some computer dick thought it up')

2s compliment makes basic maths operation (addition and subtraction) much easier and more direct on our binary-orientated friends. It also eliminates a duplicate 0.

>might reveal something interesting about that API Hwnd handle number thingy
>Hwnd is a temporary number , semi - randomly generated to give any running software an identification number to find a window

It is temporary in the sense that when a window is destroyed the hWnd is also destroyed (or, at least, no longer points to anything valid). But it remains in existence as long as the window does, so I am not sure I'd really call it 'temporary', or at least no more temporary than a window. And it is far from random.

>it won’t help someone knowing [the hWnd] to find a window in another running software

One of the core features of a win32 window is that it has a unique hWnd - which means that you can find any window in any application if you know it's hWnd. But one should note that some controls are windowless (which means their parent does all the hard work of managing any interactions with them, and you cannot directly access those controls).

>this is a point that some tutorials I recently seen seem to have missed
Becasue they are right and you are wrong. I think the confusion may have arisen because the various FindWindow family of API functions cannot take you directly to windows lower down in the hierarchy, you have to walk it. But NONE of those functions take an hWnd as a parameter - indeed their entire point is to try an find the Hwnd of particular windows so that going forward you CAN access the relevant window directly

>But the number is possibly generated in such a way that looking at the (correct) binary representation, allows you to know something, for example, what type of window, or what level, or whatever
Nope. The hWnd is merely a handle that indirectly (the indirecfion means that Windows can safely do housekeeping that reorganises memory) points the the location in memory where the definition or structure of the window exists. In itself it provides no more information than that.

The windows structure, however, provides a ton of information , mostly through the existence and maintence of the windows Style (e.g whether the window has scroll bars, or has a caption ... or is vbisible ...)

Now a windows in not a com object so it doesn't have named properties we can access by name. The various styles that are currentlyy applied to a window are held in a 32bit area of memory (i.e an unsigned long) and each bit in the long represents a particular style being on or off. We can then mask the 32bits to read specific bit, or styles ...

And that is what is going one with

If lngStyle And mcWSVISIBLE Then

There is an API call that, given an hWnd can retrieve the Long that represents the all the styles - here referred to as lngStyle - retrieved from a window with a particular hWnd. There is also a specific style that the Win32 documentation calls WS_VISIBLE and which has been, for no obvious reason I can see, renamed to mcWSVISIBLE, and is defined as 0x10000000L (which would be 00010000000000000000000000000000 in binary)

So what is happening is that we retrieve a Long with all the Style settings, then mask using And to check if a specific bit representing the WS_VISIBLE property is set or not

So just to be clear here, what is NOT going on is "get all the handles of the main visible windows, which is what that code line is doing"

Here's a list of the documented styles, by the way, if you are interested. Note that none of them are defined in decimal ...

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

Thanks for all that, I really appreciate it, I had gone off thinking I had it all sussed on this one early for once……
( I figured there would be a good reason for the 2's compliment, - it was just my failed attempt at humour again, with my suggestion of the origin, no offence intended… That sort of humour amuses me, (at the time, - ** I often edit it out later myself as I tidy and correct the post) - you know what they say … simple minds… and I figure it might help confuse the AI Bots that are learning from our posts, so that they can achieve their goal of getting rid of us all, (**often they swoop on me shortly after I make a new post, for a day or two). (Maybe the Admins should re direct all those annoying Bots flooding us recently to the Scuttlebutt, that will chuck a spanner in their logical learning algorithms)
[Joke]So now I know it was you, Chris Greaves or some other smart member here who invented 2’s compliment, fair enough, :) - ( I am the only idiot in the world that missed getting told about it, Lol, - it came about while I was living in the forest probably :) ) [/Joke] )
_.___________________________-

I am not sure about this hwnd stuff, I thought I was
Give me another day to think about this, and all you wrote. Amongst other things I will sacrifice another computer doing investigations with that spy software , ( using that software seems to sometimes cause strange problems, sometimes terminal crashes) , and then if I survive, I will be back …

Alan
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

… continued later, from last post

OK something minor perhaps first…
SpeakEasy wrote:
18 Nov 2024, 01:15
].... a specific style that the Win32 documentation calls [/color]WS_VISIBLE and which has been, for no obvious reason I can see, renamed to mcWSVISIBLE, and is defined as 0x10000000L (which would be 00010000000000000000000000000000 in binary)
I think the name is just the Author’s choice isn’t it? He gives it a name similar to the official one.
Are you suggesting that in coding of some sort, in some language, somewhere, somehow, the use of the specific variable name, WS_VISIBLE, ( as shown in the documentation ) , may give you that actual number, ( &H10000000 / 268435456 / 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ), without you having to declare it, - maybe a bit like you sometimes get available named constants when you do Early Binding in VBA? ( In Late binding you must use the actual numbers where needed, or variable constants that you declare yourself, because the specific named variables in the official name will not be available) .
_.________________________________________________________

Handle hWnd & Co
Maybe we are slightly talking about different things. Or I just don’t quite speak the language correctly. ( I speak quite well English, German and a few technical languages such as some areas of Physics and high frequency Microwave Electronics, but I have not quite picked up the spoken / written Computer technical language yet)
I am fairly clear now, on how we get at stuff using API stuff , via the handle, hwnd, and traditionally we might have a few code lines to navigate down the hierarchy, ( just as we discussed for the big coding in that "Office clipboard" thread). You get one at the ….. Top , Main, Visible, or some such word to describe the one you start with. Then you go about getting the next, or Child or the one bearing some relation to the first one you got. And so on. If you are lucky you can get at what you want that way. A direct way is only possibly for the Top , Main, Visible, or however you describe the one you start with. I know it’s usually necessary to navigate along in a sort of sequential explorer tree root or branch route way. (Makes sense as a computer is just a lot of long strings - Chris Greaves told me, - just to blame him again, he wont mind.)


So, I'll go back a bit, with a new coding for today, with new (a)musing explanations/ coding description
It’s the Dev Ashish’s one again, that Hans found for me. I just refresh my explanations. It may help me to get it straight
........
......
Here 'tis, ( and as I know now you kindly take a look at my musings there, which I am very grateful for, I will try to be a bit more polite, :) ( Things are looking up there – A week ago my Sister found me there and started posting me pictures. So now at least three people have been there along with the 20,000 Bots that have been attacking and crawling the place since a few years ) (Please feel free to post anything anywhere there, - I will move it if necessary if it is in the wrong place etc. ) )

Me: it won’t help someone knowing [the hWnd] to find a window in another running software
You say I am wrong.
Using my refreshed today coding:
Exhibit 1: I run the coding, close Excel, re open and run again using same Office version
and
Exhibit 2: I then close Excel, open a different version of office and run again.
Results are consistent – same hwnd for other things . So that suggest I was wrong. I think I did not originally have this coding, and what I did have was getting a lot more stuff as it was not doing either the style selection or the selection of only windows with a caption. My view of all the stuff was blurred. I had not re checked. I may have done later eventually. So I got there perhaps a lot quicker thanks to you, with less work as I would have stubbornly assumed I was right until I had re checked and rechecked and rechecked first. – I trust more what you say then what I say myself with this stuff, so Thanks. I think also that I started getting the style number mixed up with the handle, until it got more clear to me what was going on. … excuses …excuses….
I am seriously annoyed with myself, :(
_.__
As a punishment I bloated an up to date computer with Visual Studio 2022 just to get Microsoft Spy++ to complement all the third party Spys that I installed recently.
So similar experiment again, trying to be more careful this time. An excuse I might try here is that they are all giving me a lot more windows, some with similar names, especially for with something like Excel, so I thought I was seeing different numbers previously for the same window in the Spys, but perhaps I wasn’t,.
So I tried investigating something simpler this time first, - I made a new Notepad file, as I have a feeling they must be simple as I have a vague memory of making something similar in one of my wild VB____ experiments ( crazy story: I followed a tutorial by a University lecturer who got killed by his own son because he didn’t like his Dad’s new girlfriend Jim something I think - I still got his tutorials on a bed side video viewing XP Notebook somewhere )
So my coding gives handle 919324. But all the Spy’s give me 000E071C
https://i.postimg.cc/2yqz2k8f/Microsoft-Spy.jpg
https://i.postimg.cc/7Y2HzZW3/Window-De ... andles.jpg
https://i.postimg.cc/tRfR7x2R/Winspecto ... andles.jpg
https://i.postimg.cc/JzC15Cf9/Win-Spy-N ... andles.jpg

Let me take a guess, … that last number is some Hexogonolary 8’s complimenting representation of 919324 …….
…. BIngo

I am going away to take some well deserved punishment, then maybe comment on some of the rest…
Last edited by DocAElstein on 18 Nov 2024, 22:33, edited 3 times in total.
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
SpeakEasy
5StarLounger
Posts: 753
Joined: 27 Jun 2021, 10:46

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by SpeakEasy »

>I think the name is just the Author’s choice isn’t it?
Sure, but my point is why bother changing the name of a Windows constant that has been defined since the days of Windows 1/2 (although I grant that it wasn't properly documnted until Windows 3). But no, in VBA the only way you can use the name is if you declare it (you'll find it all nicely predeclared for you in the API viewer, by the way), unless someone has produced a win32 API TLB (and they have - for those interested in such things have a look here)

>But all the Spy’s give me 000E071C
> that last number is some Hexogonolary 8’s complimenting representation of 919324

Well yes, as I think you have gathered now, Spy++ reports hWnds in hexadecimal.

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

SpeakEasy wrote:
18 Nov 2024, 22:28
... But no, in VBA the only way you can use the name is if you declare it
Thx
_.______________________________________

Some more feedback to your earlier post….…..
SpeakEasy wrote:
18 Nov 2024, 01:15
.... the various FindWindow family of API functions cannot take you directly to windows lower down in the hierarchy, you have to walk it. But NONE of those functions take an hWnd as a parameter -
This is confusing me.
I had not checked them all out, but I had been thinking that might be the case, that Find stuff might mostly not take a hwnd
But on the other hand I am not too sure about this. Clearly some Find things do take a hwnd as parameter, or at least one - for example FindWindowExA##
I don’t know if the choice of wording in the functions is saying something. I am thinking the Get mostly ( but also sometime perhaps a Find# ) gives you the hwnd allowing you to go somewhere to find something without telling you the exact location.
Typically when we get a hwnd it’s from a hwnd nearby, walking the tree as you say . (Perhaps the , walking the tree is something that can easily be missed when learning as you might , as I did , try to get the handles of a few things close to the top and so mistakenly think you could get at other things directly with its handle. The first lesson should perhaps do a quick bit of delving down the hierarchy

But this is all still a bit unclear to me, the bit you said about Find not taking hwnd parameter(s)

_.___________________-
SpeakEasy wrote:
18 Nov 2024, 01:15
....
>But the number is possibly generated in such a way that looking at the (correct) binary representation, allows you to know something, for example, what type of window, or what level, or whatever
Nope......
Indeed. I now agree with you there. I was getting that style number mixed up with a handle number
_.________________-

I am very glad you corrected me. I had planned to make some comments at some tutorials this morning, and I would have been writing rubbish

_.___________
SpeakEasy wrote:
18 Nov 2024, 01:15
....what is NOT going on is "get all the handles of the main visible windows, which is what that code line is doing"...
Err … OK it’s not getting any handles directly, its allowing / organising that we skip over giving the codes output (which includes the handle**##) for things other than what could be considered perhaps the main windows I see
(**## The author did not include in his output the handles, I did that small mod. The author not including the handles is one thing that helped me go in my wrong thinking that handles given for a window were different for different running things , since I figure for that reason he thought it was not much use to give )


Thanks again
Alan
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
SpeakEasy
5StarLounger
Posts: 753
Joined: 27 Jun 2021, 10:46

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by SpeakEasy »

>Clearly some Find things do take a hwnd as parameter

I probably took a shortcut in my comments. What I meant was that none of the Find family allow you to find a specific, arbitary hWnd (if you already know an hWnd you don't need to find it). The FindWindowEx example you correctly raise as taking an hWnd parameter simply uses them to put you at a known point in the hierarchy to start walking ... Sorry if my statement was misleading

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16919
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by ChrisGreaves »

SpeakEasy wrote:
19 Nov 2024, 09:03
... find a specific, arbitrary hWnd (if you already know an hWnd you don't need to find it).
I've not been following this in depth, but I am starting to think that the hwnd behaviour is the same as many collections(?) in VBA. I can no longer put my hands on specific examples, but I recall many times in Word/VBA looping (For/Next) to match something in order to be able to use a specific item of a set directly. I had to "determine its index number" before I could read or update the single item - or determine that the single item did not exist.
Active VBA programmers are invited to supplement my thoughts with a list of collections iof genuine VBA collections. :thankyou:
Cheers, Chris
Never panic in a room that holds a computer.

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

Edit: Caution. I might be talking rubbish - https://eileenslounge.com/viewtopic.php ... 32#p322332
Edit again - I was talking mostly rubbish. Sorry about that, but was worth it - now I know better :smile:
I am not clear what you mean by behaviour?
If you mean how their values are stored, then the handles behave a bit differently. One of the main points is that you can’t get to them numbers directly in any way. **
If you mean how the windows are stored/ accessed by their handles, then same again, you can’t get to them windows directly in any way, even if you know their identification handle number.
You are forced to go in steps, "walking the tree". ( Perhaps this is necessary, because as you get the next handle in order to move on to get the final handle you want, maybe behind the scenes something else goes on to somehow "put you there", as maybe only then can you do stuff there? )

** I suppose it might look like the Spy software gets you all the handle values directly, but I guess they go along walking all the trees as far as they can. But getting those handles still does not help you to get directly to a window.

If we do get to a window, we can give it a message to change something. You can’t change anything in a collection. ( Message is perhaps not such a good word. That implies like sending a written command in an envelope. If I understand correctly it’s more like the order you give when you are there, or maybe not. I am not sure )
Last edited by DocAElstein on 19 Nov 2024, 20:50, edited 2 times in total.
Regards , Ālan , DocÆlstein :england: , :germany:

User avatar
SpeakEasy
5StarLounger
Posts: 753
Joined: 27 Jun 2021, 10:46

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by SpeakEasy »

>But getting those handles still does not help you to get directly to a window
??? As I said earlier, if you have an hwnd you can absolutely directly access the window that hwnd references, so I am confused as to what you are saying here.

>you can’t get to them windows directly in any way, even if you know their identification handle number
Again, huh? You absolutely can get to them (if by identification handle number you mean hWnd). Or maybe I am misunderstanding what you mean by 'get to them'

>the hwnd behaviour is the same as many collections
No, it's pretty much behaves like a hiearchical dataset (which a collection is not)

User avatar
DocAElstein
5StarLounger
Posts: 804
Joined: 18 Jan 2022, 15:59
Location: An Englishman, illegally re-routing rivers, in Hof, Beautiful Bavaria. Rule, Britannia!

Re: VBA code line If And Then with 2 (big) numbers (If lngStyle And mcWSVISIBLE Then)

Post by DocAElstein »

I figured we always needed to walk the tree. From one window to the next. I missed the point perhaps. Maybe to get the handle you need to walk the tree, but if you have already any handle, from say a Spy, you can use that in one line to get there???
I am confused. I need to practice a few codings perhaps to get it clear
Regards , Ālan , DocÆlstein :england: , :germany: