I've managed to include an object file created by masm in a Delphi 6 project. The problem is that I have to use pascal calling convention (.model flat, pascal) and then all API-calls will be screwed up because the parameters are being pushed in the wrong order.

How can I keep the pascal calling convention and still be able to invoke apis normally (I don't want to push the parameters manually)?
Posted on 2003-12-05 14:48:49 by Delight
Delight,

Shouldn't you be able to tell delphi to use the pascal calling convention?. Some procedure prototype option or something? The masm procedure uses the pascal calling convention right?
Posted on 2003-12-05 14:56:02 by Odyssey
You are defining the default calling convention, so if you don't specify a calling convention with the "PROC" or "EXTERN" statements that is the one it'll use.

You can of course mix and match, I believe that wsprinft is a C calling convention function.

Mirno
Posted on 2003-12-05 16:07:33 by Mirno
Delight, this is the exact reason why I made my 'protoize' (.lib -> .inc) generate explicit PROC statements instead of relying on ".model". Unfortunately protoize is in a broken state right now (old source rots etc ;)), but a sample full-style declaration would be:


BeginUpdateResourceA PROTO STDCALL :DWORD, :DWORD

So, you should be able to just add "PASCAL" for the exported funcs.
And really, as far as I remember, delphi does support other calling conventions as well - but it's been quite a while since I used delphi, around v2 or v3 I guess. I did link in some assembly back then, though :)
Posted on 2003-12-05 16:14:58 by f0dder
Why not just specify the calling convention in the PROTOs for the Delphi functions, leave the default as stdcall so that internal and api procs use the more sane calling convention.

DelphiProc PROTO PASCAL :DWORD,:DWORD
Posted on 2003-12-05 16:20:13 by donkey
delphi uses the register calling convention
eax, edx, ecx and stack

taken from delphi help:
Directive Parameter order Clean-up Passes parameters in registers?
register Left-to-right Routine Yes
pascal Left-to-right Routine No


but you can specify to use the stdcall calling convention to be safe
something like:
procedure Demo(s: string); stdcall; external;
Posted on 2003-12-05 22:31:05 by Jnrz
The problem is that Borland is using a slightly different object format and Delphi will complain about various things when changing the .model directive to stdcall.

Would it be possible to create some sort of macro replacement for invoke that pushes the parameters in reversed order?
Posted on 2003-12-06 03:32:44 by Delight
I solved the problem using the _call macro created by Four-F:
http://www.asmcommunity.net/board/index.php?topic=14450

Thanks for your help.
Posted on 2003-12-06 04:02:52 by Delight
Delight, iirc masm supports generation of the old borland-style format by adding the /omf switch to the commandline, and if not, there should be coff->omf conversion tools around. But oh well, if you got it working the other way, I guess that's fine enough :)
Posted on 2003-12-06 07:46:05 by f0dder