如何在Delphi中结合重载和stdcall? [英] How to combine overload and stdcall in Delphi?

查看:194
本文介绍了如何在Delphi中结合重载和stdcall?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导出该函数的SOAP数据模块

I have a SOAP Data Module that exports this function

function MyFunction(MyParam1, MyParam2): boolean; stdcall;

我可以从另一个exe使用此功能。一切正常。

I can use this function from another exe. Everything works.

现在,我想在其所在的同一项目中使用相同的函数。我将其单元添加到了uses子句中,但它无法识别它(我有未声明的标识符)。然后,我添加了一个重载,但无法正常工作。

Now I want to use the same function from inside the same project it's in. I added its unit to the uses clause but it didn't recognise it (I got Undeclared Identifier). Then I added an overload but I can't get it to work.

function MyFunction(MyParam1, MyParam2): boolean; stdcall; overload;
function MyFunction(MyParam1, MyParam2): boolean; overload;

我收到不允许使用字段定义...

I get "field definitions not allowed..."

我希望能够使用stdcall从外部访问该函数,但也希望在内部像普通库函数调用一样访问该函数。有人知道我怎么能做到吗?

I want to be able to access the function from outside using stdcall, but also internally like common library function calls. Does anyone know how I can achieve that?

推荐答案

您的问题与调用约定无关。

Your problem has nothing to do with the calling convention.

一些注意事项:

第一,

function MyFunction(MyParam1, MyParam2): boolean; stdcall;

是语法错误。您忘记了指定 MyParam1 MyParam2 的类型。

is a syntax error. You have forgotten to specify the types of MyParam1 and MyParam2.

考虑单位

  unit Unit1;

  interface

  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs;

  function Func1(MyParam1, MyParam2: integer): boolean;

  implementation

  function Func1(MyParam1, MyParam2: integer): boolean;
  begin
    ShowMessage('Func1');
  end;

  function Func2(MyParam1, MyParam2: integer): boolean;
  begin
    ShowMessage('Func2');
  end;

  end.

只有 Func1 对其他单位可见,因为在接口部分中仅声明了 。界面是其他单位看到的。

Only Func1 will be visible to other units, because only Func1 is declared in the interface section. And the interface is what other units see.

您可以使用 stdcall 在您自己的项目中。那根本不是问题。您甚至可能不会注意到该函数具有异常的调用约定。

You can use stdcall inside your own project. That isn't a problem at all. You will probably not even notice that the function has an 'unusual' calling convention.

A一对重载函数(过程)是一对具有相同名称但具有不同参数列表的函数(过程),例如

A pair of overloaded functions (procedures) is a pair of functions (procedures) with the same name but with different parameter lists, as in

function Add(A, B: integer): integer; overload;
function Add(A, B: real): real; overload;

两个函数即使重载也不能具有相同的名称和参数列表。确实,如果允许这样做,那么编译器将如何知道您要调用的函数?!

Two functions cannot have the same name and parameter lists, even if they are overloaded. Indeed, if that was allowed, then how in the world would the compiler know what function you want to call?!

这篇关于如何在Delphi中结合重载和stdcall?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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