ruby dll_如何从Ruby中的Windows DLL处理返回的指针结构数据

ruby dll

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below:

在Ruby中,可以通过Win32API类,win32-api gem或其他gem轻松地调用或调用API DLL库。 对于一般的DLL API调用,有很多参考,下面列出了一些很好的技巧:

http://www.rubytips.org/2008/05/13/accessing-windows-api-from-ruby-using-win32api-library/ http://www.rubytips.org/2008/05/13/accessing-windows-api-from-ruby-using-win32api-library/ http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_windows.html http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_windows.html

But all of these articles, tips don't mention how to process the returned pointer of a struct or memory block. As we known, standard DLL routine should return a code and return pointer/struct/memory block by function arguments, memory should free by claimer, so we can allocate memory in our codes and pass this pointer into DLL, the DLL routine save returned data into that pointer, after processed we need to free the allocated pointer ourselves. Anyway, the DLL author might not follow the General Programming Rules, or have some special reason, the DLL maybe return a pointer structure data directly instead of function arguments.

但是所有这些文章,技巧都没有提到如何处理结构或内存块的返回指针。 众所周知,标准DLL例程应返回代码并按函数参数返回指针/结构/内存块,内存应由声明者释放,因此我们可以在代码中分配内存并将此指针传递给DLL,DLL例程保存返回的数据在该指针中,经过处理后,我们需要自己释放分配的指针。 无论如何,DLL作者可能不遵循通用编程规则,或者有某些特殊原因,DLL可能直接返回指针结构数据而不是函数参数。

For example, if a definition like below:

例如,如果定义如下:

typedef  struct Data {
    int len;
    int flag;
    char values[1000];
  } *Data;
  PData = *Data;
Data* GetData(int index, int flag); cdcel;

Win32API only provider a few data types like "0", "V", "I", "L", "P", other data types of other languages will mapping to these types, then we using "P" to receive the pointer data. unfortunately, we can't get the correct data by Win32API in Ruby via normally way:

Win32API仅提供一些数据类型,例如“ 0”,“ V”,“ I”,“ L”,“ P”,其他语言的其他数据类型将映射到这些类型,然后我们使用“ P”接收指针数据。 不幸的是,我们无法通过Win32API在Ruby中以正常方式获取正确的数据:

require "Win32API"
getData = Win32API.new('filename.dll', 'GetData', ['I', 'I'], 'P');
pdata = getData.call(100, 10)

In fact, Ruby doesn't know the size of the return pointer's object, thus Ruby just process pointer's data like "null-terminated" string. so the returned data might be truncated if the returned pointer's object is a complex struct. A similar question appears here demonstrating this problem: http://www.iteye.com/topic/47936

实际上,Ruby不知道返回指针对象的大小,因此Ruby只是处理指针数据(例如“以空终止”的字符串)。 因此,如果返回的指针的对象是复杂的结构,则返回的数据可能会被截断。 一个类似的问题出现在这里,说明了此问题: http : //www.iteye.com/topic/47936

We hope Ruby will improve this later, but how do we process the PData pointer after the call now?

我们希望Ruby会在以后对此进行改进,但是现在调用后如何处理PData指针?

If we want to get returned data correctly, first, we need to define another API first:

如果我们要正确获取返回的数据,首先,我们需要首先定义另一个API:

@CopyMemory = Win32API.new('Kernel32.dll', 'RtlMoveMemory', ['P', 'I', 'I'], '0')
getData = Win32API.new('filename.dll', 'GetData', ['I', 'I'], 'L')
pd = getData.call(100, 10)
buf = Array.new(buf_size, 0).pack('L*')
@CopyMemory.call(buf, pd, buf_size)
len, flag = buf.unpack('I2')
values = buf[9, len]

翻译自: https://www.experts-exchange.com/articles/8454/How-to-process-returned-pointer-structure-data-from-Windows-DLL-in-Ruby.html

ruby dll

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值