2 decorated names with same entry point

CG85 1 Reputation point
2022-04-28T12:51:31.08+00:00

Hello,

Is there any way to have 2 decorated names for the same entry point of a C function for a DLL created with MSVC?

For example:

void __declspec(dllexport) foo(void);

So the DLL exports the function foo for entry point 0x00001000 and _foo on this same entry point ?

I ask because I saw a dll with this kind of double naming of decorated names for backwards compatibility issues

Greetings

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,787 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. RLWA32 46,031 Reputation points
    2022-04-28T12:56:39.657+00:00

    One way is to use a .def file to create an alias.

    1 person found this answer helpful.

  2. RLWA32 46,031 Reputation points
    2022-04-29T23:46:36.37+00:00

    I found a workaround by compiling as C++ instead of C.

    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>cl /c /MD /Tp FOOBar.c  
    Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30143 for x86  
    Copyright (C) Microsoft Corporation.  All rights reserved.  
      
    FOOBar.c  
      
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>lib /out:mylib.lib FOOBar.obj  
    Microsoft (R) Library Manager Version 14.29.30143.0  
    Copyright (C) Microsoft Corporation.  All rights reserved.  
      
      
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>link /map /mapinfo:exports /def:FooLib.def /machine:x86 /DLL /out:FooLib.dll mylib.lib msvcrt.lib  
    Microsoft (R) Incremental Linker Version 14.29.30143.0  
    Copyright (C) Microsoft Corporation.  All rights reserved.  
      
    LINK : warning LNK4001: no object files specified; libraries used  
       Creating library FooLib.lib and object FooLib.exp  
      
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>dumpbin /exports FooLib.dll  
    Microsoft (R) COFF/PE Dumper Version 14.29.30143.0  
    Copyright (C) Microsoft Corporation.  All rights reserved.  
      
      
    Dump of file FooLib.dll  
      
    File Type: DLL  
      
      Section contains the following exports for FooLib.dll  
      
        00000000 characteristics  
        FFFFFFFF time date stamp  
            0.00 version  
               1 ordinal base  
               2 number of functions  
               2 number of names  
      
        ordinal hint RVA      name  
      
              1    0 00001C90 FOOBar  
              2    1 00001C90 _FOOBar  
      
      Summary  
      
            1000 .data  
            1000 .rdata  
            1000 .reloc  
            1000 .text  
      
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>  
    

    FOOBar.c

    #include "framework.h"  
      
    void FOOBar(void)  
    {  
        OutputDebugStringW(L"Function body for FOOBar\n");  
    }  
    

    Header -

    #pragma once  
      
    #define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers  
    // Windows Header Files  
    #include <windows.h>  
      
      
    __declspec(dllexport) void FOOBar(void);  
    

    FooLib.def -

    LIBRARY  
      EXPORTS  
        _FOOBar=?FOOBar@@YAXXZ  
        FOOBar=?FOOBar@@YAXXZ  
    

    Linker map -

     Exports  
      
      ordinal    name  
      
            1    ?FOOBar@@YAXXZ (void __cdecl FOOBar(void))  
                   exported name: FOOBar  
            2    ?FOOBar@@YAXXZ (void __cdecl FOOBar(void))  
                   exported name: _FOOBar  
    
    
    
    
      
    
    1 person found this answer helpful.

  3. CG85 1 Reputation point
    2022-04-29T07:05:49.86+00:00

    Thank you very much. I have created a POC to verify this and yes, it does work.
    The problem is that my project uses additional options in our (complex) build process that disrupts this part.

    0 comments No comments

  4. RLWA32 46,031 Reputation points
    2022-04-29T12:52:24.877+00:00

    I tried to replicate your environment from the command line. Both function names were exported -

    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>cl /c foo.c
    Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30143 for x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    foo.c
    
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>lib /out:mylib.lib foo.obj
    Microsoft (R) Library Manager Version 14.29.30143.0
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>link /map /mapinfo:EXPORTS /def:FooLib.def /machine:x86 /DLL /out:FooLib.dll mylib.lib
    Microsoft (R) Incremental Linker Version 14.29.30143.0
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    LINK : warning LNK4001: no object files specified; libraries used
       Creating library FooLib.lib and object FooLib.exp
    
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>dumpbin /exports FooLib.dll
    Microsoft (R) COFF/PE Dumper Version 14.29.30143.0
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    
    Dump of file FooLib.dll
    
    File Type: DLL
    
      Section contains the following exports for FooLib.dll
    
        00000000 characteristics
        FFFFFFFF time date stamp
            0.00 version
               1 ordinal base
               2 number of functions
               2 number of names
    
        ordinal hint RVA      name
    
              1    0 00001000 _foo
              2    1 00001000 foo
    
      Summary
    
            2000 .data
            6000 .rdata
            1000 .reloc
            A000 .text
    
    C:\Users\RLWA32\source\repos\Rlwa32\FooLib\FooLib>
    

    And from the linker map -

     Exports
    
      ordinal    name
    
            1    _foo
            2    _foo
                   exported name: foo
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.