Click to See Complete Forum and Search --> : I have a question on extern "C"
EnthusiasticNewbie
November 2nd, 2009, 06:08 AM
--------------------------------------------------------------------------------
hello
I have a question..
I realized that when I compiled and made export functions in dll with 'extern "C" __declspec(dllexport)', function names didn't include underscores in front of their name.
but I heard that symbols of c functions should include underscores..
is it a just a feature of Visual C++ compiler?
Even in import and export sections(in EXE , DLL), the function name doesn't contain underscores in front of its name. but when I compiled function with __stdcall, function name was like _functionName@parametersize and that's exactly like what I learned before..
naming rule of __cdecl functions in C has been changed ? or is it a just matter of compiler ?
and one more question..
when we use standard API in kernel32.dll, user32.dll, gdi32.dll and so on
even if most functions are standard call functions, the function name is not like
_anyName@parametersize .. why ? were they not made by __declspec(dllexport) ?
I need help.. I've found no answers that meets my questions..
thanks
MrViggy
November 3rd, 2009, 03:36 PM
Post your code. That should work (if I remember my syntax correctly).
Viggy
VijayDandur
November 4th, 2009, 03:18 AM
Dear friend,
By adding this " __declspec(dllexport) " statement indicates that the function is to be exported , and __declspec is a standard calling convention. Thats all , it wont add and "Underscores" during function decoration.
What Extern "C" means , follow the "C" name decoration standards, Which doesn't add any extra characters in function decoration.
I Think you got it.
Thakns.
Igor Vartanov
November 4th, 2009, 09:10 AM
I heard that symbols of c functions should include underscores..
. . .
Even in import and export sections(in EXE , DLL), the function name doesn't contain underscores in front of its name.
You're missing the diffeence between function symbol and function export name. Symbol is a name used internally by linker. Export is a name used by loader for external dynamical linkage. Two absolutely different entities.
Let's see some details by example.
// 23.cpp
extern "C" __declspec(dllexport) void foo() {}
cl 23.cpp /link /dll /map:23.map /out:23.dll
; 23.map
23
Timestamp is 4af1883c (Wed Nov 04 16:57:16 2009)
Preferred load address is 10000000
Start Length Name Class
0001:00000000 000063f4H .text CODE
0002:00000000 000000f4H .idata$5 DATA
. . .
0003:00000000 00000cd0H .data DATA
0003:00000ce0 00000bbcH .bss DATA
Address Publics by Value Rva+Base Lib:Object
0000:00000000 __except_list 00000000 <absolute>
0000:00000003 ___safe_se_handler_count 00000003 <absolute>
0000:00000000 ___ImageBase 10000000 <linker-defined>
0001:00000000 _foo 10001000 f 23.obj <-- symbol
0001:00000005 __CRT_INIT@12 10001005 f LIBCMT:dllcrt0.obj
0001:000002d4 __DllMainCRTStartup@12 100012d4 f LIBCMT:dllcrt0.obj
D:\Temp\23>exports 23.dll
ModuleType:
PE executable (DLL)
Subsystem:
Win32 GUI
ModuleName:
23.dll
Exported Info: BaseOfOrdinals = 1
NumberOfNames = 1
NumberOfFunctions = 1
RelVirtAddr Ord Hint ExportedFuncName
0x00001000 1 0 foo <-- export name
Igor Vartanov
November 4th, 2009, 09:23 AM
Let's see another case, a bit different comparing to the previous one.
// 23.cpp
extern "C" void foo() {}
; 23.def
LIBRARY 23.dll
EXPORTS
oops = foo ; here's the trick
cl 23.cpp 23.def /link /dll /map:23.map /out:23.dll
23
Timestamp is 4af18d73 (Wed Nov 04 17:19:31 2009)
Preferred load address is 10000000
Start Length Name Class
0001:00000000 000063f4H .text CODE
0002:00000000 000000f4H .idata$5 DATA
0002:000000f4 00000004H .CRT$XCA DATA
. . .
0002:0000152c 000000f4H .idata$4 DATA
0002:00001620 00000452H .idata$6 DATA
0002:00001a80 0000003eH .edata DATA
0003:00000000 00000cd0H .data DATA
0003:00000ce0 00000bbcH .bss DATA
Address Publics by Value Rva+Base Lib:Object
0000:00000000 __except_list 00000000 <absolute>
0000:00000003 ___safe_se_handler_count 00000003 <absolute>
0000:00000000 ___ImageBase 10000000 <linker-defined>
0001:00000000 _foo 10001000 f 23.obj
0001:00000005 __CRT_INIT@12 10001005 f LIBCMT:dllcrt0.obj
0001:000002d4 __DllMainCRTStartup@12 100012d4 f LIBCMT:dllcrt0.obj
0001:000002f5 __amsg_exit 100012f5 f LIBCMT:crt0dat.obj
0001:00000319 ___crtCorExitProcess 10001319 f LIBCMT:crt0dat.obj
. . .
D:\Temp\23>exports 23.dll
ModuleType:
PE executable (DLL)
Subsystem:
Win32 GUI
ModuleName:
23.dll
Exported Info: BaseOfOrdinals = 1
NumberOfNames = 1
NumberOfFunctions = 1
RelVirtAddr Ord Hint ExportedFuncName
0x00001000 1 0 oopsYou see? Export name generally may have nothing common with the symbol it relates to.
EnthusiasticNewbie
November 7th, 2009, 09:02 AM
Thanks for your kind reply :)
U seem to have understood what I wondered
But I have another question
Where's the mechanism that links symbol name (like _anyFunc) with exports name(like anyFunc) ?
That is, What can make loader find proper exports name when there's symbol like _anyfunc ?
Exports name can be various when there's only one symbol name .
The secret is in lib file ?
Igor Vartanov
November 8th, 2009, 05:38 AM
Sure. This is exactly what import library is for. Just take a look inside some, and you find the symbol bound to export name. Though the second case will look more tricky.
EnthusiasticNewbie
November 8th, 2009, 12:28 PM
really thanks for your great explaination
but I couldn't find any linkage in import library file..
there are only symbol names.. no exports name in it!
I wonder why.. but I understood that lib file is the only thing
that can link symbol and exports name even if I couldn't see what it is..
anyway thanks again :)
Igor Vartanov
November 11th, 2009, 01:32 PM
0000000600: 20 20 20 20 20 20 31 32 │ 35 37 39 36 33 38 34 34 1257963844
0000000610: 20 20 20 20 20 20 20 20 │ 20 20 20 20 20 20 30 20 0
0000000620: 20 20 20 20 20 20 33 32 │ 20 20 20 20 20 20 20 20 32
0000000630: 60 0A 00 00 FF FF 00 00 │ 4C 01 44 01 FB 4A 0C 00 `◙ яя L☺D☺ыJ♀
0000000640: 00 00 00 00 08 00 5F 66 │ 6F 6F 00 32 33 2E 64 6C ◘ _foo 23.dl
0000000650: 6C 00 │ l
Well, this should be it. And seems like loader does some additional work and strips the underscore off... I'm embarrassed a bit...
EnthusiasticNewbie
November 12th, 2009, 08:21 AM
Thanks :)
and thanks for my another question on ReadProcessMemory too !
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.