Delphi Win32 Specific API Call - Why Fly Exceptions Without "asm pop ..."?

I am using Delphi to create an XLL add-in for Excel that includes many calls to the Excel4v function xlcall32.dll function. However, since I assume that very few Delphi experts work with this particular API, I hope this problem can be observed in other APIs as well.

In C, in particular, in the xlcall.h file that ships with the Microsoft Excel 2007 XLL SDK , Excel4v is defined as:

int pascal Excel4v(int xlfn, LPXLOPER operRes, int count, LPXLOPER opers[]); 

In Delphi, I use:

 function Excel4v(xlfn: Integer; operRes: LPXLOPER; count: Integer; opers: array of LPXLOPER): Integer; stdcall; external 'xlcall32.dll'; 

LPXLOPER is a pointer to a structure (in C) or a record (in Delphi).

I did my homework on declaring C functions in Delphi ( this excellent article was a big help), and I think m declaring Excel4v properly. However, calls from Delphi code to this function raise exceptions ("access violation ..." is what I see), except when they are followed by the following line:

 asm pop sink; end; 

Where the "shell" is defined somewhere as an integer.

I have no information about the assembly ... So I could not try to fix the exceptions with "asm pop sink; end;" in any way. But "asm pop wasink; end;" really captures exceptions. I first saw what he used in this useful article about creating XLL using Delphi . Here's the most relevant quote:

"From Delphi, a big stumbling block with add-ons is an additional parameter after the return address on the stack. It is freed up every time you call Excel. Ive never found out what it is, but as long as you drop it your add-in will work fine. Add asm pop variable line, end; after each call, where the variable can be any global, local, or object variable that has a length of at least 4 bytes well. Repeat - IT SHOULD BE ON after every call to Excel4v. Otherwise, you build time bombs. "

Basically I want to understand what is really happening and why . What can cause a Win32 function to return an "extra parameter after the return address on the stack", and what does it mean?

Perhaps there is another way to fix this, for example. with another compiler option or another way to declare a function?

And is there anything risky in calling "asm pop sink; end;" after every call to Excel4v ...? It seems to be working fine, but since I do not understand what is happening, it feels a little dangerous ...

+6
c assembly winapi delphi xll
source share
3 answers

I do not believe that this is pascal vs stdcall - they are very similar to conditional conventions and should not lead to an inconsistent stack when the function exits.

From the reference article ,

This would indeed be a very nice syntax, but it is not the same as above the definition of an array. Array-of-Parameters are the parameters of an open array. They can look like any array, and they accept any array, but they get an additional (hidden) parameter that executes the highest index in the array (High cost). Since this only happens in Delphi and not in C or C ++, you would have a real problem. (See also my article on open arrays), since the actual number of parameters will not match.

You get the optional parameter "highest array index" passed to the function. This value is int and should be cleared when the function exits so that you do not end with a damaged stack and crash. This article shows how to pass arrays to C functions.

Something like:

 type PLPXLOPER = ^LPXLOPER; 

And pass PLPXLOPER as the last parameter.

+8
source share

Most Windows functions use __ stdcall for their calling conventions .

0
source share

Your calling convention is wrong, in particular, "stdcall". Declaration C is listed as "pascal"

Stdcall passes parameters in order from right to left, expects the program to clear and not use registers. Pascal, OTOH passes parameters in order from left to right. Therefore, everything does not happen as the other half of the code expects anyway.

Modify your Delphi declaration in the same way as pascal instead of stdcall.

0
source share

All Articles