使用 C/C++ Delphi 2010 中的 DLL
我想使用 ssdeep 的 dll (http://ssdeep.sourceforge.net/)。 API 为:
int fuzzy_hash_buf(unsigned char *buf, uint32_t buf_len, char *result);
然后在Delphi中,我这样写:
function fuzzy_hash_buf(buf : Pbyte; buf_len : Cardinal; result : PAnsiChar): integer;标准调用;外部“fuzzy.dll”名称“fuzzy_hash_buf”;
在Delphi中如何使用该函数?
谢谢!
I want to use dll from ssdeep (http://ssdeep.sourceforge.net/). The API is:
int fuzzy_hash_buf(unsigned char *buf, uint32_t buf_len, char *result);
then in Delphi, i write it like this:
function fuzzy_hash_buf(buf : Pbyte; buf_len : Cardinal; result : PAnsiChar): integer; stdcall; external 'fuzzy.dll' name 'fuzzy_hash_buf';
How to use that function in Delphi?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
fuzzy.dll
使用C声明导出一个函数fuzzy_hash_buf
,那么您是对的,Delphi声明将是
To use this in Delphi, in the
interface
implementation
部分,您不必自己实现该函数,而是指向外部 DLL:请注意,您不必重新声明参数、结果类型和调用约定 (
stdcall
)。现在您可以像使用本机的实际功能一样使用该功能。例如,您可以
从
使用
在其中声明fuzzy_hash_buf
的单元的任何单元进行写入。更新
我担心我对 CreateFileMapping 不够熟悉 函数。不过,读完MSDN文档后,我相信你可以做到
If
fuzzy.dll
exports a functionfuzzy_hash_buf
with the C declarationthen you are right that the Delphi declaration would be
To use this in Delphi, in the
interface
section of a unit, writeThen, in the
implementation
section of the very same unit, you do not implement the function yourself, but rather point to the external DLL:Notice that you do not have to redeclare the parameters, the result type, and the calling convention (
stdcall
).Now you can use this function as if it were an actual function of this unit. For instance, you might write
from any unit that
uses
the unit in which you declaredfuzzy_hash_buf
.Update
I am afraid that I am not familiar enough with the CreateFileMapping function. However, after reading the MSDN documentation, I believe that you can do
除了调用约定可能错误(
stdcall
或cdecl
)之外,看起来您已经正确声明了该函数。根据参数名称和类型,我的猜测是您应该在第一个参数中传递一个指向字节数组的指针,并在第二个参数中告诉函数您有多少字节已经给了。您还可以传递一个指向该函数将为您填充的字符数组的指针。该数组的大小假设足以容纳函数将放在那里的任何内容。函数结果可能是指示成功或失败的状态代码。
查阅文档表明我的猜测是正确的。结果缓冲区的长度应至少为
FUZZY_MAX_RESULT
字节。您可以通过声明一个字符数组来实现这一点:将其传递给函数:
文档没有说明如何确保结果缓冲区以 null 终止,因此我们在末尾保留一个额外的字节,然后放置一个 null那里的人物。这使得可以安全地将结果缓冲区传递给像
ShowMessage
这样需要string
参数的函数。Aside from possibly having the calling convention wrong (
stdcall
orcdecl
), it looks like you have declared that function correctly.Based on the parameter names and types, my guess is that you're supposed to pass a pointer to an array of bytes in the first parameter, and in the second parameter you tell the function how many bytes you've given it. You also pass a pointer to an array of characters that the function will fill for you. The size of that array is assumed to be large enough to hold whatever the function will put there. The function result is probably a status code indicating success or failure.
Consulting the documentation shows that my guesses are correct. The result buffer should be at least
FUZZY_MAX_RESULT
bytes long. You could get that by declaring an array of characters:Pass that to the function:
The documentation doesn't say anything about ensuring that the result buffer is null-terminated, so we reserve an extra byte on the end, and then put a null character there. That makes it safe to pass the result buffer to functions like
ShowMessage
that expectstring
parameters.