none
Trouble exporting function with a leading underscore.

    Question

  • Hello All,

    I'm hoping someone can help me with a problem I'm having exporting a function from a DLL.  I need to hook some APIs in a DLL.  I'm trying to do this by writing a shim DLL that forwards most APIs to the original DLL.  The original DLL has two functions with similar names.  The name of one of the functions is the same name as the other function with an underscore prepeneded.  For instance, FooBar and _FooBar.  However, the DLL I generate doesn't export the function with the underscore, _FooBar.

    Here's a short example that illustrates the problem.  I wrote a .def file for my shim DLL and the original DLL.

    # File: shim_dll.def
    LIBRARY shim_dll
    EXPORTS
        Foo     = original_dll.Foo
        _Bar    = original_dll._Bar
        FooBar  = original_dll.FooBar
        _FooBar = original_dll._FooBar

    #File: original_dll.def
    LIBRARY original_dll
    EXPORTS
        Foo
        _Bar
        FooBar
        _FooBar

    Then, I created an import library for the original DLL:

    C:\tmp>lib /def:original_dll.def /machine:x86
    Microsoft (R) Library Manager Version 8.00.50727.762
    Copyright (C) Microsoft Corporation.  All rights reserved.

       Creating library original_dll.lib and object original_dll.exp

    Then, I used the linker to create the shim DLL:

    C:\tmp>link shim_dll.obj original_dll.lib /def:shim_dll.def
     (R) Incremental Linker Version 8.00.50727.762
    Copyright (C) Microsoft Corporation.  All rights reserved.

       Creating library shim_dll.lib and object shim_dll.exp

    When, I use dumpbin to check the exports from shim_dll.dll, I see that _FooBar is missing.

    C:\tmp>dumpbin /exports shim_dll.dll
    Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
    Copyright (C) Microsoft Corporation.  All rights reserved.


    Dump of file shim_dll.dll

    File Type: DLL

      Section contains the following exports for shim_dll.dll

        00000000 characteristics
        4909E345 time date stamp Thu Oct 30 12:39:33 2008
            0.00 version
               1 ordinal base
               3 number of functions
               3 number of names

        ordinal hint RVA      name

              1    0          Foo (forwarded to original_dll.Foo)
              2    1          FooBar (forwarded to original_dll.FooBar)
              3    2          _Bar (forwarded to original_dll._Bar)

      Summary

            2000 .data
            2000 .rdata
            1000 .reloc
            7000 .text

    I'll be most grateful if someone can tell me how to export both of these functions. 

    Thanks,

    Jeff
    Thursday, October 30, 2008 4:45 PM

Answers

  • Hi Fantastic,

    Thanks for the reply.  Unfortunately, someone else broke the leading underscore rule and I'm stuck with it.  I only have source code for parts of the original DLL and no source code for it's clients.  If I wanted to avoid the leading underscores, I would have to modify and recompiling the original DLL and all it's clients.

    Happily, I found a work around.  Instead of exporting FooBar and _FooBar in a .def file, I put the following pragmas in a C module.  When I linked this, both symbols were exported.

    pragma comment(linker, "/export:_FooBar=original_dll.FooBar")
    #pragma comment(linker, "/export:__FooBar=original_dll._FooBar")
    Monday, November 03, 2008 3:47 PM