• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

[Code] Win32 - Get SketchUp Window Handle (WIP)

Scheduled Pinned Locked Moved Developers' Forum
40 Posts 5 Posters 10.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    thomthom
    last edited by 29 Dec 2010, 13:56

    GetCurrentThreadId I think this one is what we can use to get associated window handles. Just need to get callbacks working so we can use EnumThreadWindows to get the windows for the calling thread.

    Thomas Thomassen — SketchUp Monkey & Coding addict
    List of my plugins and link to the CookieWare fund

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 29 Dec 2010, 14:45

      @jim said:

      @thomthom said:

      Where is the win32-api found - the one that supports callbacks?

      http://win32utils.rubyforge.org/

      I'm not sure if this can co-exist with Win32API or not.

      require 'win32/api'
      [attachment=0:26ojrmbj]<!-- ia0 -->win32.zip<!-- ia0 -->[/attachment:26ojrmbj]

      I just tried it, works fine with Win32API.

      Here's a bastardised proof of concept snippet where I found the SketchUp window regardless if it had focus or not.

      
      # http://stackoverflow.com/questions/3327666/win32s-findwindow-can-find-a-particular-window-with-the-exact-title-but-what
      EnumWindows       = Win32;;API.new('EnumWindows', 'KP', 'L', 'user32')
      EnumThreadWindows = Win32;;API.new('EnumThreadWindows', 'LKP', 'I', 'user32')
      GetCurrentThreadId = Win32API.new("kernel32.dll", "GetCurrentThreadId", '', 'L')
      
      # Detect the toolwindows even if Hide Dialogs is active.
      def enum_sketchup_windows
        threadId = GetCurrentThreadId.call
        enumWindowsProc = Win32;;API;;Callback.new('LP', 'I'){ |handle, param|
          #puts "EnumWindows - Callback"
          #puts "> handle; #{handle}"
          #puts "> param; #{param.inspect}"
          window_text = get_window_text(handle)
          window_text.strip! # Remove trailing NULL character
          p window_text unless window_text.empty?
          if !window_text.index(param).nil?
            puts "window was found; handle #{handle}"
            0 # FALSE - stop looking after we find it
            1 # TRUE
          else
            1 # TRUE
          end
        }
        EnumThreadWindows.call(threadId, enumWindowsProc, 'SketchUp')
      end
      
      # Takes the first enumerated window for the calling SketchUp thread and fetches
      # the root owner which should be the SketchUp window. (Not tested against wxSU)
      #
      # Is the enum required to get just one window? Any other function to get an
      # arbitrary window from the SketchUp thread?
      def find_sketchup_window
        threadId = GetCurrentThreadId.call
        hwnd = 0
        enumWindowsProc = Win32;;API;;Callback.new('LP', 'I'){ |handle, param|
          hwnd = GetAncestor.call(handle, GA_ROOTOWNER)
          0
        }
        EnumThreadWindows.call(threadId, enumWindowsProc, 'SketchUp')
        hwnd
      end
      
      

      Remaining issues:

      • Migrate Win32API to Win32::API for all function calls.
      • While testing I eventually got an exception that said there was too many callbacks initiated. Seems there might be a limit. I have not looked into this further, but I think once might have to make one callback proc, and then delegate to the appropriate handling method based on param.

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 29 Dec 2010, 14:56

        @unknownuser said:

        = Documentation
        The source file contains inline RDoc documentation. If you installed
        this file as a gem, then you have the docs.

        Where do you get the source code?

        I tried to install the gem, but got this error:
        gemInstallError.png

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by 29 Dec 2010, 15:18

          It is a c-language based extension, and apparently isn't available already compiled for your platform. So you would need to compile it.

          This is the advantage of using the mingw32 Ruby Installer with devkit - it automatically builds native libraries.

          Link Preview Image
          Downloads

          The easy way to install Ruby on Windows This is a self-contained Windows-based installer that includes the Ruby language, an execution environment, important...

          favicon

          (rubyinstaller.org)

          Hi

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 29 Dec 2010, 15:33

            But where is the source code - since it's suppose to contain the documentation?

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by 29 Dec 2010, 15:36

              For me, it is here:

              C:\Ruby186\lib\ruby\gems\1.8\gems\win32-api-1.4.7-x86-mingw32\ext\win32

              Hi

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 29 Dec 2010, 16:05

                hm... guess I have to look into that ming-thing...

                Anyway - I have produced a code which appear to return the handle for the SketchUp window of the calling thread. I created a EnumWindowsProc to delegate enumeration messages in order to avoid Error: #<Win32::API::Error: too many callbacks are defined.>. I really want to know what this limit is. This is the first draft, I expect there is a better way to deal with this.

                But, we do get a reliable window for the SketchUp window we want - as far as I have been able to test.


                Draft 1

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 29 Dec 2010, 16:08

                  Maybe we can handle drag and drop by callbacks. Or intercept window messages so we can simulate the roll-up/down of toolwindows.

                  Still, OSX users are out of luck here...

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by 30 Dec 2010, 02:41

                    Here's some interesting info:

                    #
                    #  some Sketchup window properties;
                    #
                    #  AutomationId  =  ""
                    #
                    #  CLASS   =  "Afx;00400000;b;00010011;00000006;00790557"
                    #  *** the last octet changes each time Sketchup is run,
                    #  examples;  "Afx;00400000;b;00010011;00000006;0012077F"
                    #             "Afx;00400000;b;00010011;00000006;000E0699"
                    #
                    #  ControlType           =  "ControlType.Window"
                    #  LocalizedControlType  =  "window"
                    #
                    

                    The main application window is the only one with a classname like this, and also the only one with a LocalizedControlType that equals "window".

                    ALL dialogs including WebDialogs, are:

                    #
                    #  ClassName             =  "#32770"
                    #  ControlType           =  "ControlType.Window"
                    #  LocalizedControlType  =  "Dialog"
                    #
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by 30 Dec 2010, 03:14

                      @thomthom said:

                      I have produced a code which appear to return the handle for the SketchUp window of the calling thread.

                      Are you planning on distibuting a pre-compiled win-api as part of TT_Lib2 ??

                      I have a few issues with this.
                      (1) It's not in the correct folder. What if a person already has it (and possibly a newer version,) installed ?

                      require will not recognize the path string you have in that example, and would load the older version down under your TT_Lib2 folder. That would overwrite the newer classes that might be loaded, if they were loaded first.

                      OR.. since "T" comes before "W" your version would get loaded first, and when a normal version gets loaded after... your code might get broken. (I say "might" as newer versions are usually better.)

                      So? I like the idea of someone precompiling Berger's WinUtils, and zipping them for manual install by the community (and/or creating a one-click-installer for dummies.) But they should be in the proper folder ... and likely a path to them appended to the $LOAD_PATH array.

                      How much of Berger's suite would the distro have? the bare bones min or the whole thing?

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        thomthom
                        last edited by 30 Dec 2010, 10:18

                        @dan rathbun said:

                        Here's some interesting info:

                        I get Afx:00A50000:b:00010005:00000006:0A7C0E3B during one session,
                        and the next I get Afx:00ED0000:b:00010005:00000006:5CCC0D4F - seems to be more than just the last bit that changes...

                        SU8 - Win7...

                        I don't understand where that name is from - other apps, like Notepad++ has a fixed classname.

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          thomthom
                          last edited by 30 Dec 2010, 10:28

                          @dan rathbun said:

                          Are you planning on distibuting a pre-compiled win-api as part of TT_Lib2 ??

                          I'm looking into it. If license allows it.

                          @dan rathbun said:

                          (1) It's not in the correct folder. What if a person already has it (and possibly a newer version,) installed ?

                          That is a thing that was on my to-look-into list. What if I placed it into a folder, say: TT_Lib2/win32api/win32/api.so - and then added the TT_Lib2/win32api/ path to $LOAD_PATH so one load it as the examples says require 'win32/api'?

                          @dan rathbun said:

                          OR.. since "T" comes before "W" your version would get loaded first, and when a normal version gets loaded after... your code might get broken. (I say "might" as newer versions are usually better.)

                          We have that potential issue with Win32API already, though it often lies in the root of the plugins folder, you can't be sure what version was copied into there last.

                          @dan rathbun said:

                          How much of Berger's suite would the distro have? the bare bones min or the whole thing?

                          Atm I only have use of api.so - but there was some interesting stuff he had.

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • Dan RathbunD Offline
                            Dan Rathbun
                            last edited by 30 Dec 2010, 12:33

                            @thomthom said:

                            @dan rathbun said:

                            Here's some interesting info:

                            I get Afx:00A50000:b:00010005:00000006:0A7C0E3B during one session,
                            and the next I get Afx:00ED0000:b:00010005:00000006:5CCC0D4F - seems to be more than just the last bit that changes...

                            SU8 - Win7...

                            I don't understand where that name is from- other apps, like Notepad++ has a fixed classname.

                            @unknownuser said:

                            (http://en.wikipedia.org/wiki/Application_Framework_eXtensions)":35xuu8cn]One interesting quirk of MFC is the use of "Afx" as the prefix for many functions, macros and the standard precompiled header name "stdafx.h". During early development what became MFC was called "Application Framework Extensions" and abbreviated "Afx". The name Microsoft Foundation Classes (MFC) was adopted too late in the release cycle to change these references.

                            @unknownuser said:

                            Just few words about AFX:XXXXX:XXX like windows..... Dont confuse with this type of class names, these are just class names and actually MFC generates class names for its windows this way. Some of those numbers are an HINSTANCE and a process ID (to make sure the class name is unique). I'm not sure what the others are - if you really want to know, you can probably find it in 'MFC source code. -MAHESH

                            @unknownuser said:

                            (http://www.autohotkey.com/forum/topic633.html), Chris, in his reply, ":35xuu8cn]Below are quotes from the only relevant link discovered on Google at:

                            @unknownuser said:

                            Those Afx window classes were custom classes distributed with early versions of MFC, before the "Common Controls" concept was introduced.

                            Many people have tried to "read" text from them but you CAN'T, not with messages anyway!

                            They're almost certainly owner-drawn - the text is not delivered by messages, it's "hand-drawn" into the window's DC

                            ...

                            Apparently legacy from the early days ... there are many things I see (about Sketchup UI objects,) poking about with MS Tools that don't look quite right.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              thomthom
                              last edited by 30 Dec 2010, 12:45

                              So, many of what looks like system UI elemens is not drawn by the system - but emulated by the AFX/MFC framework?

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by 30 Dec 2010, 12:51

                                There are several GUI frameworks a coder can choose to use ... WTL sounds very interesting.

                                Link Preview Image
                                Windows Template Library - Wikipedia

                                favicon

                                (en.wikipedia.org)

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by 4 Jan 2011, 09:41

                                  @dan rathbun said:

                                  I've also read that the DL library is 'on the outs', and they are planning to deprecate it and replace it with something else.

                                  OK.. now I remember what the next generation (after DL,) was supposed to move to, (the Foriegn Function Interface):
                                  Wikipedia: libffi
                                  and the github site for Ruby FFI is:
                                  https://github.com/ffi/ffi

                                  It is not yet included with Ruby as of v1.9.1-p429

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • A Offline
                                    Anton_S
                                    last edited by 22 Jun 2012, 19:13

                                    Here is my way to find sketchup window.
                                    I used it in SU Window Settings plugin.

                                    Parts of ClassName change After restarting SU
                                    ~sample: "Afx:00400000:b:00010011:00000006:0574025D"
                                    From my testing:

                                    • On windows XP, the last set of values, separated by ":" change.
                                      Afx:00400000:b:00010011:00000006:0574025D
                                    • On windows 7, all even parts, separated by ":" change. The odd parts are always the same, including on both 32 and 64 -bit processors.
                                      Afx:00400000:b:00010011:00000006:0574025D

                                    So, the only parts of class that are reliable to locate main SU window are, Afx, b and 00000006. Plus to get the current sketchup window, you could add the Process.pid. The code below does all of that work 😄

                                    @main_window = Hash.new
                                    @find_main_window = Win32;;API;;Callback.new('IP', 'I'){|hwnd, lParam|
                                      p = 0.chr*4
                                      threadID = GetWindowThreadProcessId.call(hwnd, p)
                                      pid = p.unpack('l')[0]
                                      next true if (pid != Process.pid)
                                      cname = 0.chr*50
                                      GetClassName.call(hwnd, cname, cname.size)
                                      cname = cname.strip
                                      if (cname[0,3] == "Afx") and (cname[13,1] == "b") and (cname[24,8] == "00000006")
                                        @threadID = threadID
                                        @main_window["hwnd"] = hwnd
                                        next false
                                      end
                                      true
                                    }
                                    EnumWindows.call(@find_main_window, nil)
                                    

                                    Note, this example was only tested on Windows XP and On Win7.

                                    What vous think?

                                    1 Reply Last reply Reply Quote 0
                                    • Dan RathbunD Offline
                                      Dan Rathbun
                                      last edited by 22 Jun 2012, 21:49

                                      You neglect to show the require() staement that loads Dan Berger's win32-api extensions. (This example does not use the Win32API.so file, that comes "out-of-the-box" with Ruby.)

                                      I'm not here much anymore.

                                      1 Reply Last reply Reply Quote 0
                                      • Dan RathbunD Offline
                                        Dan Rathbun
                                        last edited by 22 Jun 2012, 21:54

                                        I find it much simplier.. to use a KNOWN child window (sometimes I use the Console window, sometimes I create a hidden child window with a caption that is unique, like perhaps I insert a time-stamp and the current Sketchup pid, returned with $$, into my child window caption.)

                                        Then find the handle to that unique window, and get it's owner.

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • A Offline
                                          Anton_S
                                          last edited by 23 Jun 2012, 00:54

                                          @dan rathbun said:

                                          You neglect to show the require() staement that loads Dan Berger's win32-api extensions. (This example does not use the Win32API.so file, that comes "out-of-the-box" with Ruby.)

                                          Thanks for pointing that out. Yep, the code above requires win32-api

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 2 / 2
                                          • First post
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement