difference between __cdecl and __stdcall

-------- __cdecl sample code ----------------------------

; Function compile flags: /Ogtp
; File c:\work_space\testdll\serverdll\serverdll.cpp
; COMDAT _ServerDllFunc1
_TEXT SEGMENT
_i$ = 8 ; size = 4  //param 1
_j$ = 12 ; size = 4 //parm 2
_ServerDllFunc1 PROC ; COMDAT

; 27 : {

push ebp
mov ebp, esp   //set up the frame

; 28 : printf("ServerDllFunc1 called!:%d, %d\n", i, j);

push DWORD PTR _j$[ebp] //notice order: first j, then i
push DWORD PTR _i$[ebp]
push OFFSET ??_C@_0BP@KKBDKNHI@ServerDllFunc1?5called?$CB?3?$CFd?0?5?$CFd?6?$AA@
call DWORD PTR __imp__printf
add esp, 12 ; 0000000cH //clean stack space for string, i, j

; 29 : }

pop ebp //clean the frame
ret 0   //don't free the stack space the parameters that caller passed  on the callstack
_ServerDllFunc1 ENDP
_TEXT ENDS

 

-------- __stdcall sample code ----------------------------

; Function compile flags: /Ogtp
; File c:\work_space\testdll\serverdll\serverdll.cpp
; COMDAT _ServerDllFunc2@8
_TEXT SEGMENT
_i$ = 8 ; size = 4
_j$ = 12 ; size = 4
_ServerDllFunc2@8 PROC ; COMDAT

; 33 : {

push ebp
mov ebp, esp

; 34 : printf("ServerDllFunc2 called!:%d, %d\n", i, j);

push DWORD PTR _j$[ebp]
push DWORD PTR _i$[ebp]
push OFFSET ??_C@_0BP@EAJFHABK@ServerDllFunc2?5called?$CB?3?$CFd?0?5?$CFd?6?$AA@
call DWORD PTR __imp__printf
add esp, 12 ; 0000000cH

; 35 : }

pop ebp
ret 8 // clean the stack space which called passed
_ServerDllFunc2@8 ENDP
_TEXT ENDS

 

dumpbin result:

-------------------------------------

 

Dump of file ServerDLL.dll

File Type: DLL

Section contains the following exports for ServerDLL.dll

00000000 characteristics
5905DE12 time date stamp Sun Apr 30 20:52:34 2017
0.00 version
1 ordinal base
9 number of functions
9 number of names

ordinal hint RVA name

1 0 000011D0 ?cppFunc@@YAHHH@Z = ?cppFunc@@YAHHH@Z (int __cdecl cppFunc(int,int))
3 1 00001100 ServerDllFunc1 = _ServerDllFunc1
2 2 00001190 _GetData@4 = _GetData@4
4 3 00001120 _ServerDllFunc2@8 = _ServerDllFunc2@8
5 4 00001140 _StoreData@4 = _StoreData@4
6 5 0000334C g_i = _g_i
7 6 00005004 g_shared = _g_shared
8 7 00004000 g_shared_arr = _g_shared_arr
9 8 000011C0 notCalled = _notCalled

Summary

1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
1000 .text
2000 Shared

so __stdcall will mangle name to _functionName@N pattern (N is the bytes sum of parameters ), while __cdecl only modify the name to _functionName

 

posted on 2017-04-30 18:50  ltzhou  阅读(100)  评论(0)    收藏  举报