How to combine overload and stdcall in Delphi?

陌路散爱 提交于 2019-12-13 16:47:57

问题


I have a SOAP Data Module that exports this function

function MyFunction(MyParam1, MyParam2): 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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6257013/how-to-combine-overload-and-stdcall-in-delphi

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