问题描述
我知道以下内容:
-
'L'
-长 -
'P'
-指针 -
'I'
-整数 -
'V'
-虚空
'L'
- Long'P'
- Pointer'I'
- Integer'V'
- Void
我的问题是执行API调用时无法传递空指针.例如:['L', 'P', 'L'] -> api.call(0, nil, 0) :: ArgumentError: Null pointer given
.我的问题是:还有更多我不知道的参数类型,我应该怎么做才能将空指针作为方法参数传递?
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?
我一直在Internet上搜索基于WinForms的应用程序的本机Ruby编程示例.为了简化编码,我考虑了将NET添加到称为IronRuby的Ruby中(试图避免使用wxRuby,也是一个.NET迷),但是我首先希望能够首先使用纯Ruby进行显式编码.
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.
现在,我已经能够成功实现我在user32.dll对象中测试过的大多数地址,例如:
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')
..但是没有空参数我无法执行CreateWindow
或CreateWindowEx
.如果有什么帮助,我已经在Python 此处(在WinAPI下).
..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).
使用Win32API:msdn.microsoft.com/zh-cn/library/ff381397(v = VS.85).aspx
Using Win32API: msdn.microsoft.com/en-us/library/ff381397(v=VS.85).aspx
好吧,我想我可能已经通过此链接解决了自己的问题(警告:可能包含不适当的内容): [链接]
Well, I think I may have just solved my own problem with this link(warning: may contain inappropriate content): [link]
我更多地使用该论坛作为参考,并且对自己进行了一些摆弄:createwindow = Win32API.new("user32","CreateWindowEx",'lpplllllllll','l')
showwindow = Win32API.new('user32','ShowWindow',%w(l l),'l')
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...
推荐答案
与其使用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:
- 要自定义的常量您的消息框(对话框框的按钮和图标)
- 另一个使用
FFI
代替Win32API 的示例
- Constants to customise your message boxes (dialog box buttons and icon)
- Another example using
FFI
instead of Win32API
这篇关于什么是Ruby Win32API参数|如何传递空指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!