从 Delphi 程序调用 C DLL [英] Calling C DLL from Delphi program

查看:32
本文介绍了从 Delphi 程序调用 C DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Delphi 代码中,我必须使用以下原型调用 DLL 的函数(用 Visual C 编写):

In my Delphi code I have to call a DLL's function (written in Visual C) with the following prototype:

int PWFunc(LPCSTR szName, int nWidth, int nHeight, LPCSTR szFileName)

如何将 Delphi AnsiString 变量(用于 Name 和 FileName)转换为函数调用的正确类型参数(LPCSTR szName 和 szFileName)?我知道 VC LPCSTR 类型对应于 Delphi PAnsiChar 类型,但是将 AnsiString 转换为 PAnsiChar 的正确程序是什么?

How can I convert Delphi AnsiString variables (for Name and FileName) into right type parameters (LPCSTR szName and szFileName) of function call ? I know that VC LPCSTR type corresponds to Delphi PAnsiChar type, but what is the right procedure to convert AnsiString to PAnsiChar ?

推荐答案

LPCSTRLPSTR 对应于 PAnsiChar,所以这就是你使用:

LPCSTR and LPSTR correspond to PAnsiChar, so that is what you use:

function PWFunc(szName: PAnsiChar; nWidth, nHeight: Longint;
  szFileName: PAnsiChar): Longint; cdecl { or stdcall, see documentation };
  external 'somedll.dll' name 'PWFunc';

你这样称呼它:

X := PWFunc(PAnsiChar(AnsiString(SomeName)), 17, 33, 
       PAnsiChar(AnsiString(SomeFileName)));

你的函数是 stdcall 还是 dcecl 取决于编译器设置.阅读文档.如果有疑问,请尝试两者.对我来说它看起来像 cdecl,所以从它开始.

Whether your function is stdcall or dcecl depends on compiler settings. Read the documentation. If in doubt, try both. It looks like cdecl to me, so start with that.

这篇关于从 Delphi 程序调用 C DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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