Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stack trouble during DLL call?

Posted on 2002-05-10
14
Medium Priority
?
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 300 total points
ID: 7002500
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 300 total points
ID: 7002504
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
ID: 7002769
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:mikepj
ID: 7002780
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
ID: 7002821
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
ID: 7003010
Can you send me the C header? robert_marquardt@gmx.de
0
 
LVL 2

Author Comment

by:mikepj
ID: 7003229
Robert,

The header is in the question.

THanks,
MP
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7003375
The complete C header file please.
0
 
LVL 2

Author Comment

by:mikepj
ID: 7003433
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
ID: 7048301
The DLL didn't match the source.  It works now.

Thanks,
MP
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9005615
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
ID: 9006153
Thankyou for the resolution of this question.

Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 2

Author Comment

by:mikepj
ID: 9006212
thank you!
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9006476
Request for PAQ and Refund will be removed.

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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