?
Solved

Win32 DLL in C++  cleaning the stack in VB

Posted on 2003-03-17
8
Medium Priority
?
985 Views
Last Modified: 2012-05-04
if you search (all the colleciton) in MSDN under 'DLLs for Beginners'
you found explanation about how to buil DLL.

under the .DEF section, they explain that you should use
__stdcall declaration instead of __cdecl  but that imply that you must
cleanup the stack after each call.

"it requires each function call to include stack cleanup code"

they specify that you do that by:

"decorate the function name with an underscore followed by an @#nn, where #nn is the total number of bytes (in multiples of 4) in the argument list in decimal, or the number of bytes needed by the arguments in the stack"


therefore, if you have something like:

int  __stdcall Myfunc (int a, double  b)

the .DEF file will look like:

; DEF file for the  DLL
EXPORTS
   _MyFunc@12                  @1;  To call as MyFunc in C in __stdcall


where @12 is 12 byte for the stack to be cleaned up.
@1 is the ordinal number of the API call for Myfunc.

That part, I understand.  Here, what cause me problem:

(int a, double  b)
int a = 4 byte,
double b = 8 byte
total 12 bytes

right ?

what if I have

(short a, double  b)
short a = 2 bytes
double b = 8 byte
total 10 bytes

Question 1:
Do I specify 10 byte or 12 in EXPORTS of .DEF file ?  

"decorate the function name with an underscore followed
by an @#nn, where #nn is the total number of bytes
(in multiples of 4) in the argument list in
  ^^^^^^^^^^^^^^^
decimal, or the number of bytes needed by the arguments in the stack"

Question 2: Even If I'm a little bit new in DLL, I created a C DLL a few years ago and use it in VB in all my program.  I NEVER used this decoration. and never had any problem.

Can I ommit the stack cleanup declaration if I use VB ?
the .DEF will then look like:

; DEF file for the  DLL
EXPORTS
   _MyFunc                  @1;  To call as MyFunc in C in __stdcall


Thanks for all
0
Comment
Question by:Genetic_Wolf
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 8151871
>> Do I specify 10 byte or 12 in EXPORTS of .DEF file ?  
You would specify 10 bytes, because that 2nd function use only 10 bytes of parameters.

HOWEVER, you don't have to do this. I've never seen this documented anywhwere, but if you just specify the name without the standard call decoration, like

EXPORTS
   MyFunc

the function will be exported without any of the stadnard call decoration.   Its always worked for me, but it doesn't seem to be documented anywhere.

>> I created a C DLL a few years ago and use it in VB in all my program.  
>> I NEVER used this decoration. and never had any problem.
Yes, that's right.  Its not documented though.

>>; DEF file for the  DLL
>>           EXPORTS
>>             _MyFunc                  @1;  To call as MyFunc in C in __stdcall
I beleive you can drop that "_" too.  I might be wrong about that though.


ALSO you need to remove the C++ decoration, if you haven't done so.  that is done with extern "C"  like

extern "C"  int  __stdcall Myfunc (int a, double  b)

NOTE also that none of this stuff needs to be done if this DLL will be used only from C++.  and you don't have to worry about the standard call stuff if it will be used from only C or C++.    This only matters if you need to make this used from a wide variety of languages.
0
 
LVL 3

Author Comment

by:Genetic_Wolf
ID: 8152099
nietod:
The :
extern "C"  int  __stdcall

is declared in a .h module.

and yes, it is documented, look in VB or VC, go to help, '?'  look for 'SEARCH...', choose 'all the collection' and type in search field:
'dll for beginner'

the first title is 'DLLs for Beginners'

go down further in this article until:
'//Header file  pascal.h'

you will see:

//Header file  pascal.h
extern "C" double __stdcall MyFunc(int a, double b);
extern "C" int  CdeclFunc(int a);   //By default this is a __cdecl convetion


; DEF file for the  DLL
EXPORTS
   MYFUNC =_MyFunc@12 ; Alias for function call as MYFUNC for pascal calls
   _MyFunc@12                  ;  To call as MyFunc in C in __stdcall
  CdeclFunc                    ;   Called from C program in __cdecl
  CMyFunc = CdeclFunc          ;  Calling CdeclFunc with different name

just a little before that, you can see:

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler automatically converts a variable argument (vararg) function to __cdecl. The C name decoration scheme is to decorate the function name with an underscore followed by an @#nn, where #nn is the total number of bytes (in multiples of 4) in the argument list in decimal, or the number of bytes needed by the arguments in the stack. For the following function declaration, the decorated name is Myfunc@12.

My problem is that they specify:
(in multiples of 4)

So, 10 would NOT be a right value isn't ?
So, do I need to work really in multiple of 4 and in that way, count 1 short for 4 byte ?  what if I have 2 short ?  Do I count 4 or 8 ?

and you're right when you said 'never had problem' without this decoration.  never had too... just whant to make thing the way it should be.  What if on some system, they need that decoration ?  Don't want to take any chance on a  commercial product.

Have you any 'link' to support your claim ?
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 8152332
>>  yes, it is documented, look in VB or VC
What's not documented is the fact you can remove the standard call decoration without using an alias in the module definition file.  They say you have to do

  _MyFunc@12                  @1;  To call as MyFunc in C in __stdcall

or

   MYFUNC =_MyFunc@12 ; Alias for function call as MYFUNC for pascal calls

but it seems that doing

   MyFunc

is enough.  the linker will generate an undecorated export given this alone.

>> where #nn is the total number of bytes (in multiples of 4
Right.  I forgot about that!   So yes it shoudl be 12, not 10.

by the way if you ever have any doubts about a decorated name, compile and link the DLL and then use the DUMPBIN program (in the VC BIN directory) to list all the exported functions from the DLL (/EXPORTS)  then you can fidn the exported function name and specify that in your module definition file.


>> Have you any 'link' to support your claim ?
???   I thought I was trying to make clear that I have never found any documentation to support this claim.  Its from experience alone.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:tinchos
ID: 9502223
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:

Answered by: nietod

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 3

Author Comment

by:Genetic_Wolf
ID: 9502916
nietod ,

I'm sorry, I forgot that question and I supposed I waited until someone else made a comment...  but you've been very helpfull, So, the best is for you to have the point.

Thanks.
0
 
LVL 3

Author Comment

by:Genetic_Wolf
ID: 9502926
nietod,

I'm sorry, I have to ask the Cleanup Volunteer to give you the point, the system refuse to accept your answer.  When I press the button, I receive:

• beta.experts-exchange.com: beta.experts-exchange.com  
Unable to load /shared/post.jsp because beta.experts-exchange.com: beta.experts-exchange.com

best luck!
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9502937
Dont worry Generic Wolf if you cannot accept nietod's answer

This question is marked to be cleaned and my recommendation was to give nietod the points

As there is no objection, when the moderator comes here to clean it up, he will give him the answer as valid and award him his points.

0
 
LVL 3

Author Comment

by:Genetic_Wolf
ID: 9502946
I still don't understand why the Accept button doesn't work...

make it a grade A would you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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