Solved

Stack trouble during DLL call?

Posted on 2002-05-10
14
143 Views
Last Modified: 2010-04-04
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.

0
Comment
Question by:mikepj
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 100 total points
Comment Utility
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
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 100 total points
Comment Utility
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
 
LVL 2

Author Comment

by:mikepj
Comment Utility
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
 
LVL 2

Author Comment

by:mikepj
Comment Utility
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
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
Check if double does not convert to extended.
With stdcall the called function cleans up the stack.
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
Can you send me the C header? robert_marquardt@gmx.de
0
 
LVL 2

Author Comment

by:mikepj
Comment Utility
Robert,

The header is in the question.

THanks,
MP
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
The complete C header file please.
0
 
LVL 2

Author Comment

by:mikepj
Comment Utility
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
 
LVL 2

Author Comment

by:mikepj
Comment Utility
The DLL didn't match the source.  It works now.

Thanks,
MP
0
 
LVL 1

Expert Comment

by:pnh73
Comment Utility
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
 
LVL 1

Expert Comment

by:pnh73
Comment Utility
Thankyou for the resolution of this question.

Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 2

Author Comment

by:mikepj
Comment Utility
thank you!
0
 
LVL 1

Expert Comment

by:pnh73
Comment Utility
Request for PAQ and Refund will be removed.

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now