且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-14 04:14:11

与其使用Win32API(我相信它是基于晦涩且很少使用的DL模块构建的),不如使用Win32API新的和改进的FFI模块.

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.

方法如下:

  • (1)获取ffi:
    gem install ffi

  • (1) Get ffi:
    gem install ffi

(2)然后尝试以下方法:

(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.

注意:用空指针代替Hwnd会使消息框没有所有者窗口.

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


以下一些链接可能会有所帮助:


Here are some links that may help:
  • Constants to customise your message boxes (dialog box buttons and icon)
  • Another example using FFI instead of Win32API