开发者

How to combine overload and stdcall in Delphi?

I have a SOAP Data Module that exports this function

function MyFunction(MyParam1, MyParam开发者_运维技巧2): boolean; stdcall;

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

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..."

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.

A few things to notice:

A silly bug

First,

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

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

Visibility

Consider the unit

  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.

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.

Calling conventions

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.

Overloaded functions

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?!


The code as you present it does not compile because you have not specified parameter types. If you do so then it will work fine, so long as the parameter lists differ.

For example this compiles fine:

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

But this does not:

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

Overloading is where you have multiple methods with the same name, but different parameter lists. Whenever you call an overloaded method, the compiler selects the method whose parameter lists matches the parameters being passed.


I suspect you trying to overload the method using the same parameter lists for both versions of the method, but a different calling convention. This will not work. Overloaded method resolution can not be performed on the basis of calling convention (or function return value type for that matter). Simply use the stdcall version internally as well as externally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜