【发布时间】:2010-09-21 02:38:36
【问题描述】:
我正在尝试编写 RSPEC(ruby 风格的 BDD)和 Windows 应用程序之间的接口。应用程序本身是用一种晦涩的语言编写的,但它有一个 C API 来提供访问权限。我已经使用 Ruby/DL,但即使是对 DLL 方法的最基本调用也难以正常工作。这是我目前所拥有的,在一个名为 gt4r.rb 的文件中:
require 'dl/import'
module Gt4r
extend DL::Importable
dlload 'c:\\gtdev\\r321\\bin\\gtvapi'
# GTD initialization/termination functions
extern 'int GTD_init(char *[], char *, char *)'
extern 'int GTD_initialize(char *, char *, char *)'
extern 'int GTD_done(void)'
extern 'int GTD_get_error_message(int, char **)'
end
到目前为止,我的阅读表明这就是我所需要的,所以我写了一个 RSPEC 示例:
require 'gt4r'
@@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
@@normal_user = "BMCHARGUE"
describe Gt4r do
it 'initializes' do
rv = Gt4r.gTD_initialize @@normal_user, @@normal_user, @@test_environment
rv.should == 0
end
end
当运行时...
C:\code\GraphTalk>spec -fs -rgt4r gt4r_spec.rb
Gt4r
- initializes (FAILED - 1)
1)
'Gt4r initializes' FAILED
expected: 0,
got: 13 (using ==)
./gt4r_spec.rb:9:
Finished in 0.031 seconds
1 example, 1 failure
返回值 (13) 是实际返回码,表示错误,但是当我尝试将 gTD_get_error_message 调用添加到我的 RSPEC 时,我无法使参数正常工作。
我是否朝着正确的方向前进,谁能指出我可以尝试的下一件事?
谢谢, 布雷特
对这个问题的跟进,显示当我尝试从目标库中获取错误消息时失败的部分:
require 'gt4r'
@@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
@@normal_user = "BMCHARGUE"
describe Gt4r do
it 'initializes' do
rv = Gt4r.gTD_initialize @@normal_user, @@normal_user, @@test_environment
Gt4r.gTD_get_error_message rv, @msg
@msg.should == ""
rv.should == 0
end
end
我希望在@msg 中返回错误消息,但运行时我得到以下信息:
Gt4r
(eval):5: [BUG] Segmentation fault
ruby 1.8.6 (2008-08-11) [i386-mswin32]
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
如果我使用符号 (:msg) 来代替:
C:\code\GraphTalk\gt4r_dl>spec -fs -rgt4r gt4r_spec.rb
Gt4r
- initializes (ERROR - 1)
1)
NoMethodError in 'Gt4r initializes'
undefined method `to_ptr' for :msg:Symbol
(eval):5:in `call'
(eval):5:in `gTD_get_error_message'
./gt4r_spec.rb:9:
Finished in 0.046 seconds
1 example, 1 failure
显然我在 ruby 和 C 之间传递参数时遗漏了一些东西,但是什么?
【问题讨论】:
标签: c ruby interface rspec rubydl