Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...