CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 2nd, 2009, 06:08 AM
    EnthusiasticNewbie EnthusiasticNewbie is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 11
    EnthusiasticNewbie is an unknown quantity at this point (<10)
    Question I have a question on extern "C"

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

    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
    Reply With Quote
      #2    
    Old November 3rd, 2009, 03:36 PM
    MrViggy's Avatar
    MrViggy MrViggy is offline
    Elite Member
     
    Join Date: Feb 2002
    Posts: 3,775
    MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)MrViggy has much to be proud of (1500+)
    Re: I have a question on extern "C"

    Post your code. That should work (if I remember my syntax correctly).

    Viggy
    Reply With Quote
      #3    
    Old November 4th, 2009, 03:18 AM
    VijayDandur VijayDandur is offline
    Junior Member
     
    Join Date: Feb 2009
    Posts: 1
    VijayDandur is an unknown quantity at this point (<10)
    Re: I have a question on extern "C"

    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.
    Reply With Quote
      #4    
    Old November 4th, 2009, 09:10 AM
    Igor Vartanov's Avatar
    Igor Vartanov Igor Vartanov is offline
    Elite Member
     
    Join Date: Nov 2000
    Location: Voronezh, Russia
    Posts: 3,103
    Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)
    Re: I have a question on extern "C"

    Quote:
    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.

    Code:
    // 23.cpp
    extern "C" __declspec(dllexport) void foo() {}
    Code:
    cl 23.cpp /link /dll /map:23.map /out:23.dll
    Code:
    ; 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
    Code:
    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
    __________________
    Respectfully,
    Igor

    Last edited by Igor Vartanov; November 4th, 2009 at 09:28 AM.
    Reply With Quote
      #5    
    Old November 4th, 2009, 09:23 AM
    Igor Vartanov's Avatar
    Igor Vartanov Igor Vartanov is offline
    Elite Member
     
    Join Date: Nov 2000
    Location: Voronezh, Russia
    Posts: 3,103
    Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)
    Re: I have a question on extern "C"

    Let's see another case, a bit different comparing to the previous one.

    Code:
    // 23.cpp
    extern "C" void foo() {}
    Code:
    ; 23.def
    LIBRARY 23.dll
    EXPORTS
    	oops = foo    ; here's the trick
    Code:
    cl 23.cpp 23.def /link /dll /map:23.map /out:23.dll
    Code:
     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
    . . .
    Code:
    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   oops
    You see? Export name generally may have nothing common with the symbol it relates to.
    __________________
    Respectfully,
    Igor

    Last edited by Igor Vartanov; November 4th, 2009 at 09:32 AM.
    Reply With Quote
      #6    
    Old November 7th, 2009, 09:02 AM
    EnthusiasticNewbie EnthusiasticNewbie is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 11
    EnthusiasticNewbie is an unknown quantity at this point (<10)
    Re: I have a question on extern "C"

    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 ?

    Last edited by EnthusiasticNewbie; November 8th, 2009 at 12:28 PM.
    Reply With Quote
      #7    
    Old November 8th, 2009, 05:38 AM
    Igor Vartanov's Avatar
    Igor Vartanov Igor Vartanov is offline
    Elite Member
     
    Join Date: Nov 2000
    Location: Voronezh, Russia
    Posts: 3,103
    Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)
    Re: I have a question on extern "C"

    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.
    __________________
    Respectfully,
    Igor
    Reply With Quote
      #8    
    Old November 8th, 2009, 12:28 PM
    EnthusiasticNewbie EnthusiasticNewbie is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 11
    EnthusiasticNewbie is an unknown quantity at this point (<10)
    Re: I have a question on extern "C"

    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

    Last edited by EnthusiasticNewbie; November 8th, 2009 at 12:33 PM.
    Reply With Quote
      #9    
    Old November 11th, 2009, 01:32 PM
    Igor Vartanov's Avatar
    Igor Vartanov Igor Vartanov is offline
    Elite Member
     
    Join Date: Nov 2000
    Location: Voronezh, Russia
    Posts: 3,103
    Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)Igor Vartanov has a brilliant future (2000+)
    Re: I have a question on extern "C"

    Code:
    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...
    __________________
    Respectfully,
    Igor
    Reply With Quote
      #10    
    Old November 12th, 2009, 08:21 AM
    EnthusiasticNewbie EnthusiasticNewbie is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 11
    EnthusiasticNewbie is an unknown quantity at this point (<10)
    Re: I have a question on extern "C"

    Thanks
    and thanks for my another question on ReadProcessMemory too !
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:22 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009