Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Win32 DLL in C++  cleaning the stack in VB

Posted on 2003-03-17
8
Medium Priority
?
986 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
  • 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
Technology Partners: 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 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

581 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