What are the Ruby Win32API Parameters | How do I pass a null pointer?

喜欢而已 提交于 2019-11-29 12:26:31
Assad Ebrahim

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:

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!

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!