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

北城余情 提交于 2019-11-28 06:05:23

问题


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


回答1:


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:
  • Constants to customise your message boxes (dialog box buttons and icon)
  • Another example using FFI instead of Win32API



回答2:


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!




回答3:


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


来源:https://stackoverflow.com/questions/8099694/what-are-the-ruby-win32api-parameters-how-do-i-pass-a-null-pointer

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