在Delphi 2010中使用C / C ++ DLL [英] Using C/C++ DLL in Delphi 2010
问题描述
我想使用ssdeep中的dll( http://ssdeep.sourceforge.net/ )。 API是:
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);
int fuzzy_hash_buf(unsigned char *buf, uint32_t buf_len, char *result);
那么在Delphi中,我写这样:
then in Delp i write it like this:
函数fuzzy_hash_buf(buf:Pbyte; buf_len:Cardinal; result:PAnsiChar):integer; STDCALL;外部'fuzzy.dll'名称'fuzzy_hash_buf';
function fuzzy_hash_buf(buf : Pbyte; buf_len : Cardinal; result : PAnsiChar): integer; stdcall; external 'fuzzy.dll' name 'fuzzy_hash_buf';
如何在Delphi中使用该功能?
How to use that function in Delphi?
谢谢
推荐答案
如果 fuzzy.dll
导出函数 fuzzy_hash_buf
与C声明
int fuzzy_hash_buf(unsigned char *buf, uint32_t buf_len, char *result);
那么你是对的Delphi声明将是
then you are right that the Delphi declaration would be
function fuzzy_hash_buf(buf: PAnsiChar; buf_len: cardinal; result: PAnsiChar):
integer;
要在Delphi中使用,请在界面
单位的部分,写入
To use this in Delp in the interface
section of a unit, write
function fuzzy_hash_buf(buf: PAnsiChar; buf_len: cardinal; result: PAnsiChar):
integer; stdcall;
然后,在实现
非常相同的单位,你不能自己实现这个功能,而是指向外部的DLL:
Then, in the implementation
section of the very same unit, you do not implement the function yourself, but rather point to the external DLL:
function fuzzy_hash_buf; external 'fuzzy.dll' name 'fuzzy_hash_buf`
请注意,您不必重新声明参数,结果类型和调用约定( stdcall
)。
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
val := fuzzy_hash_buf(buf, len, output);
你声明了 fuzzy_hash_buf
。
恐怕我对 CreateFileMapping 功能。但是,在阅读MSDN文档之后,我相信你可以执行
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
var
buf: PAnsiChar;
buf := MapViewOfFile(FFileMappingHandle, FILE_MAP_READ, 0, 0, 0);
// Now, if I have understood MapViewOfFile correctly, buf points to the first byte of the file.
var
StatusCode: integer;
TheResult: PAnsiChar;
GetMem(TheResult, FUZZY_MAX_RESULT);
StatusCode := fuzzy_has_buf(buf, FFileSize, TheResult);
// Now TheResult points to the first byte (character) of the output of the function.
这篇关于在Delphi 2010中使用C / C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!