c++ - 在 Delphi 2010 中使用 C/C++ DLL

标签 c++ c delphi api dll

我想使用来自 ssdeep ( http://ssdeep.sourceforge.net/ ) 的 dll。 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中使用该函数?

谢谢!

最佳答案

如果 fuzzy.dll 使用 C 声明导出函数 fuzzy_hash_buf

int fuzzy_hash_buf(unsigned char *buf, uint32_t buf_len, char *result);

那么你是对的,Delphi 声明是

function fuzzy_hash_buf(buf: PAnsiChar; buf_len: cardinal; result: PAnsiChar):
  integer;

要在 Delphi 中使用它,请在单元的 interface 部分中编写

function fuzzy_hash_buf(buf: PAnsiChar; buf_len: cardinal; result: PAnsiChar):
  integer; stdcall;

然后,在同一单元的implementation部分,您不自己实现函数,而是指向外部DLL:

function fuzzy_hash_buf; external 'fuzzy.dll' name 'fuzzy_hash_buf`

请注意,您不必重新声明参数、结果类型和调用约定 (stdcall)。

现在您可以像使用 native 的实际功能一样使用此功能。例如,你可以写

val := fuzzy_hash_buf(buf, len, output);

来自使用您在其中声明fuzzy_hash_buf 的单元的任何单元。

更新

恐怕我对CreateFileMapping还不够熟悉功能。不过,看完MSDN文档后,我相信你可以做到

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.

关于c++ - 在 Delphi 2010 中使用 C/C++ DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3557398/

相关文章:

c++ - Unicode 和文本控件未正确转换为 UTF8

c++ - 是否可以使用 Cygwin 或 MinGW 将 Linux 守护程序移植到 Windows?

C 按位逻辑运算难题

C:打开();正在产生未处理的异常错误

delphi - 为 SOAP 客户端发布数据并获取响应

delphi - Delphi 中服务应用程序中给定文件的相对路径

c++ - 从导出的 dll 函数访问 dll 中的全局数据

c++ - 对于循环长度问题

c - 实现线程数组

delphi - 无法调用类中声明的方法实现通用接口(interface)方法