#Require win32api
if RUBY_VERSION < "2.0.0"
require File.join(PLUGIN_ROOT, "ene_viewport_resizer", "Win32API.so")#Bundled with plugin for SU 2013.
else
require "Win32API"#Standard libraries included in SU 2014+.
end
GetActiveWindow = Win32API.new("user32.dll", "GetActiveWindow", "", "L")
GetWindowRect = Win32API.new("user32.dll", "GetWindowRect", "LP", "I")
ShowWindow = Win32API.new("user32.dll", "ShowWindow", "LI", "I")
MoveWindow = Win32API.new("user32.dll", "MoveWindow", "LIIIII", "I")
This code works fine in sketchup low version, but error in sketchup2024, how do I need to adjust it to call win32api?
In fact, it doesn’t. It’s just that trimble has used the qt framework since version 23. There are some problems with the window call. You can still call the win local dll file, but it is recommended to use the fiddle library to implement it.
Firstly, since the SketchUp 2023.0 release, the SketchUp API comes with an method to resize the viewport, so making Win32 system calls to do this is no longer needed. See:
Using the Win32API
class has been deprecated since Ruby 1.9.1.
Ruby version 3.x does not have the "Win32API.rb"
file, which is just a wrapper using Fiddle
library calls. (This was a Ruby Core decision, and not a decision made by the Trimble SketchUp Extensibility team.)
Make sure that you have the latest version of ene_viewport_resizer if you still need to use it.
ping @eneroth3
1 Like
Ruby 2.7.8 was the last version to include Win32API
(contained in Win32API.rb
).
SU 2024 is using Ruby 3.2. The last file version is here on GitHub.
It might work with SU 2024, or you could use it to help write Fiddle code…
Extension authors can wrap the Win32API
class in a mixin module, then include
it into their extension submodules or classes:
Example mixin module for Win32API ... (click to expand) ...
# encoding: UTF-8
require 'fiddle/import'
module MyNamespace
module MixinWin32API
class Win32API
DLL = {}
TYPEMAP = {"0" => Fiddle::TYPE_VOID, "S" => Fiddle::TYPE_VOIDP, "I" => Fiddle::TYPE_LONG}
POINTER_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q*' : 'l!*'
WIN32_TYPES = "VPpNnLlIi"
DL_TYPES = "0SSI"
def initialize(dllname, func, import, export = "0", calltype = :stdcall)
@proto = [import].join.tr(WIN32_TYPES, DL_TYPES).sub(/^(.)0*$/, '\1')
import = @proto.chars.map {|win_type| TYPEMAP[win_type.tr(WIN32_TYPES, DL_TYPES)]}
export = TYPEMAP[export.tr(WIN32_TYPES, DL_TYPES)]
calltype = Fiddle::Importer.const_get(:CALL_TYPE_TO_ABI)[calltype]
handle = DLL[dllname] ||= begin
Fiddle.dlopen(dllname)
rescue Fiddle::DLError
raise unless File.extname(dllname).empty?
Fiddle.dlopen(dllname + ".dll")
end
@func = Fiddle::Function.new(handle[func], import, export, calltype)
rescue Fiddle::DLError => e
raise LoadError, e.message, e.backtrace
end
def call(*args)
import = @proto.split("")
args.each_with_index do |x, i|
args[i], = [x == 0 ? nil : x].pack("p").unpack(POINTER_TYPE) if import[i] == "S"
args[i], = [x].pack("I").unpack("i") if import[i] == "I"
end
ret, = @func.call(*args)
return ret || 0
end
alias Call call
end
end # mixin module
end # top-level namespace module
… and then in an extension submodule …
module MyNamespace
module MyExtension
include MixinWin32API
# ... code using the mixed in Win32API class
end # extension submodule
end # top-level namespace module
Thanks for the advice given by pt_quick、DanRathbun、MSP_Greg
Win32APIWin32API.rb In lower versions of sketchup is kept in the Tools\RubyStdLib directory. But it was discarded in sketchup2024.
Again, SketchUp did not discard it. The Ruby Core project removed it.
SketchUp extension developers have had 10 years to convert to using Fiddle
instead of Win32API
.