耗时ç的DLL德尔福 [英] consuming C dlls with Delphi

查看:124
本文介绍了耗时ç的DLL德尔福的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个签名的DLL函数:

I have a Dll function with this signature:

UInt32 Authenticate(uint8 *Key);

我对德尔福这样做的:

I'm doing this on Delphi:

function Authenticate(Key:string) : UInt32; external 'mylib.dll' name 'Authenticate';

但始终,函数返回10(错误code)和应用刹车:\\

But always, the function return 10 (error code) and the application brakes :\

有一种方法可以做到这一点吧?

There is a way to do this right?

更新: 谢谢你们!你是最棒的!

推荐答案

有一些问题,你的code。

There are some problems with your code.

1) UINT8 字节在Delphi中,没有字符串

1) uint8 is the equivilent of Byte in Delp not String.

2)C code为使用编译器的默认调用约定,这通常是 __ CDECL 。德尔福的默认调用约定,在另一方面,是注册来代替。它们并不互相兼容。如果不匹配的调用约定,堆栈和CPU寄存器将不能正确运行时函数调用期间管理的。

2) the C code is using the compiler's default calling convention, which is usually __cdecl. Delphi's default calling convention, on the other hand, is register instead. They are not compatible with each other. If you mismatch the calling convention, the stack and CPU registers will not be managed correctly during the function call at runtime.

在C code的字面翻译就是这个:

A literal translation of the C code would be this instead:

function Authenticate(Key: PByte) : UInt32; cdecl; external 'mylib.dll';

然而,假设该函数实际上是期待一个空终止字符串,然后做这个:

However, assuming the function is actually expecting a null-terminated string then do this instead:

// the function is expecting a pointer to 8-bit data,
// so DO NOT use `PChar`, which is 16-bit in Delphi 2009+...
function Authenticate(Key: PAnsiChar) : UInt32; cdecl; external 'mylib.dll';

我要坚持第一个声明,因为它原来的C code相匹配。即使函数需要一个空值终止字符串作为输入,您还可以通过它使用 PBYTE 通过类型转换:

var
  S: AnsiString;
begin
  Authenticate(PByte(PAnsiChar(S)));
end;

或者,如果功能允许空值输入空字符串:

Or, if the function allows NULL input for empty strings:

var
  S: AnsiString;
begin
  Authenticate(PByte(Pointer(S)));
end;

这篇关于耗时ç的DLL德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆