我有这个签名的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德尔福
发布评论