Stack trouble during DLL call?

Am I missing something here?  The stack needs to be "adjusted" by 8 bytes in Delphi after returning from the call but I have no idea why!

  asm
    pop EAX;
    pop EAX;
  end;

double* are pushed onto the stack as a DWORD pointer
BYTE are pushed onto the stack as values
double is pushed onto the stack as 8 bytes of data

Thank you for your help!
MP

Here's the Visual C++ DLL function export:

int _stdcall GetPoly(
     BYTE p_bTypeOfData,
     BYTE p_bAnode,
     BYTE p_bFilterType,
     double p_fThickness,
     double p_fkVp,
     double* p_dC0,
     double* p_dC1,
     double* p_dC2,
     double* p_dC3,
     double* p_dC4,
     double* p_fkVpLo,
     double* p_fkVpHi,
     double* p_fTotFiltrLo,
     double* p_fTotFiltrHi);

// here's my Delphi 5 declaration for the DLL function
function GetPoly(
     iTypeOfData,
     iAnodeType,
     iFilterType:byte;
     dThickness,dKV:double;
     var dPoly0,dPoly1,dPoly2,dPoly3,dPoly4:double;
     var dKVLow,dKVHigh:double;
     var dTotalFiltrLow, dTotalFiltrHigh:double):integer;
     StdCall;
     external 'MyDLL';

Here's my call for the function:

Result:=GetPoly(
     iType,
     iAnode,
     iFilter,
     dThickness,
     dKV,
     dPoly1,
     dPoly2,
     dPoly3,
     dPoly4,
     dPoly5,
     dKVLow,
     dKVHigh,
     dLow,
     dHigh);

// now I need to remove 8 bytes from the stack if things are going to work properly.

LVL 2
mikepjAsked:
Who is Participating?
 
AvonWyssConnect With a Mentor Commented:
Thats usually becuase of the calling convention used by C programs. CDECL (or STDCALL) calls require tha caller (!) to remove the parameters from the stack, while PASCAL and REGISTER calls require the callee to clean up the stack (Note: in the register convention, only parameters which do not fit into the registers are put on the stack).

However, in your case, you have declared it as STDCALL, which should be correct. Note, however, that the declaration as shown by you is not correct:

    var dPoly0:double,dPoly1,dPoly2,dPoly3,dPoly4:double;
should read
    var dPoly0, dPoly1, dPoly2, dPoly3, dPoly4: Double;

Anyways, are you sure that the VC declaration matches the effecive call? It seems a little odd to me, I would have expected to see the first two double parameters to be VAR (or CONST or OUT) also. This would account for the 8 bytes.
0
 
Lee_NoverConnect With a Mentor Commented:
var dPoly0:double,dPoly1,dPoly2,dPoly3,dPoly4:double;

shouldn't all of them be var ?
this is the real fishy part : > var dPoly0:double,dPoly1 <

I'm guessing it should be like :
var dPoly0, dPoly1, dPoly2, dPoly3, dPoly4: double;

otherwise I don't see anything wrong

and what if you initialize all of the poly vars prior to calling the function ?
like :
dPoly1:=0.0;
dPoly2:=0.0;
...
Result:=GetPoly(
0
 
mikepjAuthor Commented:
Oops!  You're both correct; when creating the EE question I made a mistake.  It is using:

var dPoly0, dPoly1, dPoly2, dPoly3, dPoly4: double;

At one point I had just to make sure (I know better) I had tried this:

var dPoly0:double; var dPoly1:double; var dPoly2:double...

I had tried initializing the polys initially because I wasn't sure at the beginning that I was having stack problems.  It didn't help.

I have done a similar call to another DLL and this was no problem.

I think I'm going to e-mail the authors of the DLL; they sent me new source code but I think it must not match exactly the source they have sent me.  Will advise...

Thanks for your replies!
MP
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mikepjAuthor Commented:
I forgot to mention that I have corrected the question text.

Since StdCall does have the caller responsible for cleaning up the stack, this makes me wonder whether a mismatch in the DLL (the DLL parameter list being different than the source they provided) could cause this or whether that's not possible.  Any opinions?

I've also wondered if this could possibly be a Delphi 5.01 bug...doubtful.

Thanks!
MP
0
 
robert_marquardtCommented:
Check if double does not convert to extended.
With stdcall the called function cleans up the stack.
0
 
robert_marquardtCommented:
Can you send me the C header? robert_marquardt@gmx.de
0
 
mikepjAuthor Commented:
Robert,

The header is in the question.

THanks,
MP
0
 
robert_marquardtCommented:
The complete C header file please.
0
 
mikepjAuthor Commented:
The file only contains exports and this is the only export of interest.  For privacy and the proprietary interests of my client, I can't give the rest.  It has no #includes, #define, pragma, etc.  Only exports similar to the one of interest.

int _stdcall GetPoly(BYTE p_bTypeOfData, BYTE p_bAnode, BYTE p_bFilterType, double p_fThickness, double p_fkVp, double* p_dC0, double* p_dC1, double* p_dC2, double* p_dC3, double* p_dC4, double* p_fkVpLo, double* p_fkVpHi, double* p_fTotFiltrLo, double* p_fTotFiltrHi);

The .def file contains the following (other export removed):

LIBRARY MyLib
DESCRIPTION DLL FOR MyLib CARD

EXPORTS
     GetPoly     @14
0
 
mikepjAuthor Commented:
The DLL didn't match the source.  It works now.

Thanks,
MP
0
 
pnh73Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ and Refund

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
pnh73Commented:
Thankyou for the resolution of this question.

Paul (pnh73)
EE Cleanup Volunteer
0
 
mikepjAuthor Commented:
thank you!
0
 
pnh73Commented:
Request for PAQ and Refund will be removed.

Paul (pnh73)
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.