Jump to content

IsPtr() IsHwnd() question about differences


Go to solution Solved by Nine,

Recommended Posts

Posted

I have some inaccuracies in my understanding of several issues.

I had a problem that made me wonder what the difference is between IsPtr() and IsHwnd()

I modified the example from the documentation for the IsPtr() function as follows:

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)

    ConsoleWrite("#" & @ScriptLineNumber & " - " & IsHWnd($hWnd) & @CRLF)
    ConsoleWrite("#" & @ScriptLineNumber & " - " & IsPtr($hWnd) & @CRLF)
    ConsoleWrite("#" & @ScriptLineNumber & " - " & VarGetType($hWnd) & @CRLF)

    ; Close the Notepad window using the handle returned by WinWait.
    WinClose($hWnd)
EndFunc   ;==>Example

I would like to ask why both functions IsPtr() IsHwnd() return a positive result ?

I assumed these were two different things (Handle and Pointer).

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I suppose you want to know the difference between handles and pointers not strictly between IsPtr() and IsHWnd() which are functions used to identify if a variable is a pointer or a handle, right?

In short terms a handle it's a reference to an object in memory. This reference might be a number of different things but usually it's an integer index specific to a resource while a pointer it's variable that stores the address of another variable. But we must keep in mind that pointers are not just ordinary variables because they have data types and allows operations specific to pointers.

Posted

From the online help --

  Quote

Pointer types store a memory address which is 32bits or 64bits depending on if the 32bit or 64-bit version of AutoIt is used. They are converted to hexadecimal representation when stored in a string variable. Window handles (HWnd) as returned from WinGetHandle() are a pointer type.

Expand  

I was surprised to read that Window handles are a pointer type. I've always thought of handles as "a pointer to a pointer".

  • Solution
Posted

IsHwnd requires that the window exist.  While IsPtr not.  That's the main difference.  See :

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)
    WinKill($hWnd)

    ConsoleWrite("#" & @ScriptLineNumber & " - " & IsHWnd($hWnd) & @CRLF)
    ConsoleWrite("#" & @ScriptLineNumber & " - " & IsPtr($hWnd) & @CRLF)
    ConsoleWrite("#" & @ScriptLineNumber & " - " & VarGetType($hWnd) & @CRLF)
EndFunc   ;==>Example

 

Posted (edited)

Source ChatGPT:

  Quote

Pointers and handles are both concepts used in computer programming, but they are typically associated with different programming paradigms and have some differences in their usage.

1. **Pointers:**
   - **Definition:** A pointer is a variable that stores the memory address of another variable. In other words, it "points" to the location in memory where the actual data is stored.
   - **Usage:** Pointers are commonly used in low-level programming languages like C and C++. They provide direct access to memory addresses, allowing for more fine-grained control over memory management.
   - **Dereferencing:** To access the value pointed to by a pointer, you need to dereference it using the `*` operator in languages like C or C++.

   ```c
   int x = 10;
   int *ptr = &x; // ptr points to the memory address of x
   int value = *ptr; // Dereferencing ptr to get the value at the memory address it points to
   ```

   - **Memory Management:** Pointers require manual memory management, meaning the programmer is responsible for allocating and freeing memory.

2. **Handles:**
   - **Definition:** A handle is an abstract reference or identifier used to access an object or resource. Unlike pointers, handles don't directly represent memory addresses; instead, they serve as a way to indirectly reference an object.
   - **Usage:** Handles are often used in high-level programming languages, especially those with automatic memory management (garbage collection), such as Java or C#.
   - **Abstraction:** Handles provide a level of abstraction, allowing the underlying system to manage memory without direct intervention from the programmer.

   ```java
   // Java example using handles (references)
   String str = "Hello, World!";
   ```

   - **Automatic Memory Management:** Handles are associated with automatic memory management systems, where the runtime environment takes care of memory allocation and deallocation.

In summary, while both pointers and handles are used to reference objects or data, pointers are more direct and low-level, involving memory addresses and manual memory management. Handles, on the other hand, are often higher-level abstractions used in languages with automatic memory management, providing a layer of indirection between the programmer and the actual memory addresses.

Expand  

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted (edited)

btw.

here is some more examples:
 

#include <Array.au3>

Example()

Func Example()
    ; Retrieve a list of window handles using a regular expression. The regular expression looks for titles that contain the word SciTE or Internet Explorer.
    Local $aWinList = WinList("[REGEXPTITLE:(?i)(.*SciTE.*|.*Internet Explorer.*)]")
    For $i=1 To $aWinList[0][0]
        ConsoleWrite("- " & $aWinList[$i][1] & ' TYPE=' & VarGetType($aWinList[$i][1]) & @CRLF)
    Next
    Local $hWND = WinGetHandle("[REGEXPTITLE:(?i)(.*SciTE.*|.*Internet Explorer.*)]")
;~  Local $hWND = WinWait("[REGEXPTITLE:(?i)(.*SciTE.*|.*Internet Explorer.*)]")
    ConsoleWrite("> " & $hWND & ' TYPE=' & VarGetType($hWND) & @CRLF)
    ConsoleWrite("> " & ' IsHWnd=' & IsHWnd($hWND) & @CRLF)
    ConsoleWrite("> " & ' IsPtr=' & IsPtr($hWND) & @CRLF)

    _ArrayDisplay($aWinList)

EndFunc   ;==>Example

and of course from HelpFile "VarGetType.au3":

#include <MsgBoxConstants.au3>

Local $aArray[2] = [1, "Example"]
Local $mMap[]
Local $dBinary = Binary("0x00204060")
Local $bBoolean = False
Local $pPtr = Ptr(-1)
Local $hWnd = WinGetHandle(AutoItWinGetTitle())
Local $iInt = 1
Local $fFloat = 2.0
Local $oObject = ObjCreate("Scripting.Dictionary")
Local $sString = "Some text"
Local $tStruct = DllStructCreate("wchar[256]")
Local $vKeyword = Default
Local $fuFunc = ConsoleWrite
Local $fuUserFunc = Test

MsgBox($MB_SYSTEMMODAL, "", _
        "Variable Types" & @CRLF & @CRLF & _
        "$aArray : " & @TAB & @TAB & VarGetType($aArray) & " variable type." & @CRLF & _
        "$mMap : " & @TAB & @TAB & VarGetType($mMap) & " variable type." & @CRLF & _
        "$dBinary : " & @TAB & @TAB & VarGetType($dBinary) & " variable type." & @CRLF & _
        "$bBoolean : " & @TAB & VarGetType($bBoolean) & " variable type." & @CRLF & _
        "$pPtr : " & @TAB & @TAB & VarGetType($pPtr) & " variable type." & @CRLF & _
        "$hWnd : " & @TAB & @TAB & VarGetType($hWnd) & " variable type." & @CRLF & _
        "$iInt : " & @TAB & @TAB & VarGetType($iInt) & " variable type." & @CRLF & _
        "$fFloat : " & @TAB & @TAB & VarGetType($fFloat) & " variable type." & @CRLF & _
        "$oObject : " & @TAB & VarGetType($oObject) & " variable type." & @CRLF & _
        "$sString : " & @TAB & @TAB & VarGetType($sString) & " variable type." & @CRLF & _
        "$tStruct : " & @TAB & @TAB & VarGetType($tStruct) & " variable type." & @CRLF & _
        "$vKeyword : " & @TAB & VarGetType($vKeyword) & " variable type." & @CRLF & _
        "MsgBox : " & @TAB & @TAB & VarGetType(MsgBox) & " variable type." & @CRLF & _
        "$fuFunc : " & @TAB & @TAB & VarGetType($fuFunc) & " variable type." & @CRLF & _
        "Func 'Test' : " & @TAB & VarGetType(Test) & " variable type." & @CRLF & _
        "$fuUserFunc : " & @TAB & VarGetType($fuUserFunc) & " variable type.")

Func Test()
EndFunc   ;==>Test

 

Both VarGetType($hWnd)  shows PTR as a result.
Is there any case when VarGetType($hWnd) will return HWND ?

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

seems like not but it can differentiated: 

#include <MsgBoxConstants.au3>

Local $pPtr = Ptr(-1)
Local $hWnd = WinGetHandle(AutoItWinGetTitle())

MsgBox($MB_SYSTEMMODAL, "", _
        "$pPtr : " & @TAB & @TAB & VarGetType($pPtr) & ' - ' & IsHWnd($pPtr) & ' - ' & IsPtr($pPtr) & " variable type." & @CRLF & _
        "$hWnd : " & @TAB & @TAB & VarGetType($hWnd) & ' - ' & IsHWnd($hWnd) & ' - ' & IsPtr($hWnd) & " variable type." & @CRLF )

maybe open a trac and ask to be looked at ?

Edited by argumentum
spelling

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
  On 11/19/2023 at 1:31 AM, mLipok said:

Is there any case when VarGetType($hWnd) will return HWND ?

Expand  

Probably not. It's easy to call FindWindowA and you will see that the result of such a call it's in fact a pointer. Anyway IsHWnd() can be used to test the small range of window handles but as I said in a previous comment handles can be a number of different things and it would be more handy to have a function named IsHandle() and maybe set @extended according with the handle type (window, file, etc).

Posted
  On 11/19/2023 at 12:09 PM, Andreik said:

it would be more handy to have a function named IsHandle() and maybe set @extended according with the handle type (window, file, etc).

Expand  

Nice idea.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...