Ruby Win32API 参数是什么?如何传递空指针?

发布于 2024-12-14 19:00:10 字数 1673 浏览 0 评论 0 原文

我知道以下内容:

  • 'L' - 长
  • 'P' - 指针
  • 'I' - 整数
  • 'V' > - Void

我的问题是,当我执行 API 调用时,我无法传递空指针。例如:['L', 'P', 'L'] -> api.call(0, nil, 0) :: ArgumentError: 给定空指针。我的问题是:是否还有更多我不知道的参数类型以及如何将空指针作为方法参数传递?

背景

我一直在互联网上搜索基于 WinForms 的应用程序的本机 Ruby 编程示例。为了简化编码,我考虑过将 .NET 添加到 Ruby,称为 IronRuby(试图避免使用 wxRuby,同时也是 .NET 的粉丝),但我首先希望能够首先使用纯 Ruby 进行显式编码。

现在,我已经成功地实现了我在 user32.dll 对象中测试过的大多数地址,例如:

api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
# or
api = Win32API.new('user32', 'MessageBeep', ['L'], 'I')

..但是我无法在没有 null 的情况下执行 CreateWindowCreateWindowEx参数。 如果有任何帮助,我已经找到了如何在Python中执行此操作 此处(在 WinAPI 下)。

使用 Win32API:msdn.microsoft.com/en-us/library/ff381397(v=VS.85).aspx

[编辑]
好吧,我想我可能刚刚通过此链接解决了我自己的问题 (警告:可能包含不当内容)[link]

我更多地使用该论坛作为参考,并在我自己周围做了一些摆弄:
createwindow = Win32API.new("user32","CreateWindowEx",'lpplllllllll','l')
showwindow = Win32API.new('user32','ShowWindow',%w(ll),'l')

hWND = createwindow.call((0x00000100|0x00000200),"静态", “窗户标题",((0x4000000|0x80000000|0)|0x02000000),0,0,600,400,0,0,0,0)
showwindow(hWND, 1)

显示“窗口”后发生的唯一事情是崩溃......这可能是由于一些不正确的处理,但是,我很高兴我得到了它去工作(一点点)!只需要弄清楚剩下的...

I know the following:

  • 'L' - Long
  • 'P' - Pointer
  • 'I' - Integer
  • 'V' - Void

My problem is that I can't pass a null pointer when I perform an API call. E.g.: ['L', 'P', 'L'] -> api.call(0, nil, 0) :: ArgumentError: Null pointer given. My question is: Are there more parameter types that I don't know about and what should I do to pass a null pointer as a method parameter?

Background

I have been searching the internet for native Ruby programming examples of WinForms-based applications. I have considered the .NET addition to Ruby known as IronRuby for simplicity in coding (trying to avoid wxRuby, and also a .NET fan), but I first want to be able to code explicitly in pure Ruby first.

Now, I have successfully been able to implement most addresses I've tested in the user32.dll object such as:

api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
# or
api = Win32API.new('user32', 'MessageBeep', ['L'], 'I')

..but I cannot perform a CreateWindow Or CreateWindowEx without null parameters.
If it would be of any help, I have found how to do this in Python here (under WinAPI).

Using Win32API: msdn.microsoft.com/en-us/library/ff381397(v=VS.85).aspx

[EDIT]
Well, I think I may have just solved my own problem with this link
(warning: may contain inappropriate content): [link]

I more used that forum as reference and did a bit of fiddling around my self:
createwindow = Win32API.new("user32","CreateWindowEx",'lpplllllllll','l')
showwindow = Win32API.new('user32','ShowWindow',%w(l l),'l')

hWND = createwindow.call((0x00000100|0x00000200),"static", "Window Title",((0x4000000|0x80000000|0)|0x02000000),0,0,600,400,0,0,0,0)
showwindow(hWND, 1)

The only thing that happens after the 'window' is displayed is crash... and that may have been because of some incorrect handling, but, I am happy that I got it to work(for a little bit)! Just need to figure out the rest...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-12-21 19:00:10

您可能会发现使用新的和改进的 FFI 可以获得更好的效果,而不是使用 Win32API(我相信它是建立在晦涩且很少使用的 DL 模块之上)模块。

操作方法如下:

  • (1) 获取 ffi:
    gem install ffi

  • (2) 然后试试这个:

需要“ffi”

模块 Win32
   扩展 FFI::Library
   ffi_lib 'user32'
   Attach_function:消息框, 
       :MessageBoxA,[ :指针, :string, :string, :long ], :int
结尾

rc = Win32.messageBox(nil, "Hello Ruby 用户!", "FFI 很简单", 0x40)

放置RC

这似乎比您在编辑中发布的解决方案更容易。

注意:空指针而不是Hwnd使得消息框没有所有者窗口。


Here are some links that may help:

Instead of using Win32API (which I believe is built on top of the obscure and little used DL module), you might find better mileage using the new and improved FFI module.

Here's how:

  • (1) Get ffi:
    gem install ffi

  • (2) Then try this:

require 'ffi'

module Win32
   extend FFI::Library
   ffi_lib 'user32'
   attach_function :messageBox, 
       :MessageBoxA,[ :pointer, :string, :string, :long ], :int
end

rc = Win32.messageBox(nil, "Hello Ruby user!", "FFI is easy", 0x40)

puts rc

This seems easier than the solution you posted in your edit.

Note: The null pointer instead of Hwnd makes the message box have no owner window.


Here are some links that may help:

风吹雪碎 2024-12-21 19:00:10

我没有对此进行测试,因为我不在 Windows 上,但我认为您打算使用常量 DL::NULL。您可以在此处查看它的实际操作(倒数第二行),它看起来与您的用例类似。希望这有帮助!

I haven't tested this since I'm not on Windows but I think you're intended to use the constant DL::NULL. You can see it in action here (second-to-last line) and it looks similar to your use case. Hope that's helpful!

神仙妹妹 2024-12-21 19:00:10
require 'ffi'

module Win32
  extend FFI::Library
  ffi_lib 'user32'
  attach_function( 
                  :messageBox, 
                    :MessageBoxA,
                      [ :pointer, :string, :string, :long ], 
                      :int
                 )
end

rc = Win32.messageBox(nil, "Hello Ruby user!", "FFI is easy", 0x40)

puts rc
require 'ffi'

module Win32
  extend FFI::Library
  ffi_lib 'user32'
  attach_function( 
                  :messageBox, 
                    :MessageBoxA,
                      [ :pointer, :string, :string, :long ], 
                      :int
                 )
end

rc = Win32.messageBox(nil, "Hello Ruby user!", "FFI is easy", 0x40)

puts rc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文