consuming C dlls with Delphi

走远了吗. 提交于 2019-12-07 09:18:14

问题


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';

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

There is a way to do this right?

UPDATE: thanks guys! you're the best!


回答1:


There are some problems with your code.

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

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.

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';

I would stick with the first declaration, as it matches the original C code. Even if the function is expecting a null-terminated string as input, you can still pass it in using PByte via a type-cast:

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;



回答2:


I would add nothing to the great Remy's answer, but I would like to give a list of the tools that can help in conversion of C DLL headers to Pascal (in no special order):

  • http://sourceforge.net/projects/c2pas
  • http://www.astonshell.com/freeware/c2pas32
  • http://cc.embarcadero.com/item/26951
  • http://rvelthuis.de/programs/convertpack.html
  • http://www.drbob42.com/delphi/headconv.htm
  • http://wiki.lazarus.freepascal.org/H2Pas
  • http://wiki.freepascal.org/Chelper

Beware that these converters can only convert 60-80% of the code, so manual work follows.

The biggest time saver tip I can give is to try to find Visual Basic header translation if it exists for your DLL, and use Marco Cantu's VB converter at http://www.marcocantu.com/tools/vb2delphi.htm. This will probably give you almost 100% automatic conversion (if you are converting just DLL headers, of course). There is also a commercial VBTO converter but trial demo is quite enough for conversion of VB DLL headers to Pascal. Download it here: http://www.vbto.net

Other tips for conversion from C--/C++:

  • http://rvelthuis.de/articles/articles-convert.html
  • http://rvelthuis.de/articles/articles-pchars.html
  • http://rvelthuis.de/articles/articles-cobjs.html
  • http://rvelthuis.de/articles/articles-cppobjs.html
  • http://www.awitness.org/delphi_pascal_tutorial/c++_delphi

Slightly off topic, but I thought this might be useful to someone...




回答3:


For starters, change "string" to "pchar".

There might be other problems besides that.



来源:https://stackoverflow.com/questions/11787669/consuming-c-dlls-with-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!