gpt4 book ai didi

c++ - Ruby win32 API 接口(interface)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:17 26 4
gpt4 key购买 nike

我需要访问 ruby​​ 中 win32 库的一些函数。我在网上发现关于 Win32API 类的信息非常稀少,所以我在这里问一下。

我知道你可以这样做:

function = Win32API.new('user32','MessageBox',['L', 'P', 'P', 'L'],'I')

但我似乎无法使用当前的 win32 绑定(bind)调用此函数:

http://msdn.microsoft.com/en-us/library/bb762108%28VS.85%29.aspx

问题出在它的原型(prototype)上:

UINT_PTR SHAppBarMessage(      
DWORD dwMessage,
PAPPBARDATA pData
);

我将能够使用 win32 ruby​​ 绑定(bind)来获取返回类型和第一个参数,但是,第二个参数需要一个结构。结构体定义如下:

typedef struct _AppBarData {
DWORD cbSize;
HWND hWnd;
UINT uCallbackMessage;
UINT uEdge;
RECT rc;
LPARAM lParam;
} APPBARDATA, *PAPPBARDATA;

我尝试使用这两种方法定义此 api 方法:

api = Win32API.new('shell32','SHAppBarMessage',['L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L'],'I') 

api = Win32API.new('shell32','SHAppBarMessage',['L', 'LLLLLLLL'],'I')

但第一个在“call”方法期间出现段错误,第二个由于在“call”方法调用中指定的参数数量错误而无法运行。有什么方法可以公开此 api 函数而无需求助于在 C++ 中创建外部模块?

谢谢。

最佳答案

诀窍是使用“P”作为所有指针参数的格式说明符。您必须提供一个字符串作为指向区域。

当然,您必须确保这些字符串具有正确的预期大小,否则会发生不好的事情。

你可以直接创建这些字符串

# Mostly useful when the area will be totally overwritten
pointed_to_area = "\0" * n

或者使用更文明的Array#pack

# Allows you to control how ruby values get encoded in the buffer
pointed_to_area = [1, 2, 3, 4].pack('SsLI')

希望这对您有所帮助。


以下在我的 XP 盒子上使用旧的 ruby​​ 1.8.2 工作:

require 'Win32API'


module Win32
# This method is only here for test purposes
# Be careful to use the ascii version
FindWindow = Win32API.new('user32', 'FindWindowA', ['P', 'P'], 'L')
def self.findWindow(lpClassName, lpWindowName)
h = FindWindow.call(lpClassName, lpWindowName)
raise "FindWindow failed" if h == 0
h
end

# From winddef.h
RECT = Struct.new(:left, :top, :right, :bottom)
RECT.class_eval do
def pack
[left, top, right, bottom].pack('l4')
end
def self.unpack(s)
new(*s.unpack('l4'))
end
end

# From shellapi.h
APPBARDATA = Struct.new(:cbSize, :hWnd, :uCallbackMessage, :uEdge, :rc, :lParam)
APPBARDATA.class_eval do
def pack
unless rc.is_a? RECT
raise ArgumentError, ":rc must be an instance of Win32::RECT, got #{rc.inspect}"
end
# DWORD + HWND + UINT + UINT + RECT + LPARAM
cbSize = 4 + 4 + 4 + 4 + 16 + 4
[cbSize, hWnd, uCallbackMessage, uEdge, rc.pack, lParam].pack('L2I2a16L')
end
def self.unpack(s)
tmp = self.new(*s.unpack('L2I2a16L'))
tmp.rc = RECT.unpack(tmp.rc)
tmp
end
end
SHAppBarMessage = Win32API.new('shell32', 'SHAppBarMessage', ['L', 'P'], 'L')

# Calls SHAppBarMessage and returns the altered APPBARDATA
def self.shAppBarMessage(dwMessage, appBarData)
s = appBarData.pack
ok = (SHAppBarMessage.call(dwMessage, s) != 0)
raise "SHAppBarMessage failed" unless ok
APPBARDATA.unpack(s)
end

ABM_NEW = 0x00000000
ABM_REMOVE = 0x00000001
ABM_QUERYPOS = 0x00000002
ABM_SETPOS = 0x00000003
ABM_GETSTATE = 0x00000004
ABM_GETTASKBARPOS = 0x00000005
ABM_ACTIVATE = 0x00000006
ABM_GETAUTOHIDEBAR = 0x00000007
ABM_SETAUTOHIDEBAR = 0x00000008
ABM_WINDOWPOSCHANGED = 0x00000009
ABM_SETSTATE = 0x0000000a

ABE_LEFT = 0
ABE_TOP = 1
ABE_RIGHT = 2
ABE_BOTTOM = 3
end




if __FILE__ == $0
require 'test/unit'
class SHAppBarMessageTest < Test::Unit::TestCase
include Win32

def test_pack_unpack
a = APPBARDATA.new(-1, 0, 0, ABE_TOP, RECT.new(1, 2, 3, 4), 0)
b = APPBARDATA.unpack(a.pack)
a.cbSize = b.cbSize
assert_equal(a.values, b.values)
end
def test_simple_pos_query
h = Win32.findWindow("Shell_TrayWnd", nil)
a = APPBARDATA.new(-1, 0, 0, ABE_TOP, RECT.new(0, 0, 0, 0), 0)
result = Win32.shAppBarMessage(ABM_GETTASKBARPOS, a)
assert(result.rc.left < result.rc.right)
assert(result.rc.top < result.rc.bottom)
puts result.rc.inspect
end
end
end

关于c++ - Ruby win32 API 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1202262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com