Win32 DLL in C++ cleaning the stack in VB

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
LVL 3
Genetic_WolfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
>> 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
Genetic_WolfAuthor Commented:
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
nietodCommented:
>>  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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

tinchosCommented:
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
Genetic_WolfAuthor Commented:
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
Genetic_WolfAuthor Commented:
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
tinchosCommented:
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
Genetic_WolfAuthor Commented:
I still don't understand why the Accept button doesn't work...

make it a grade A would you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.