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