Thread: How to export functions of a DLL in Visual Studio 2008 with the actual name?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Madrid, Spain.
    Posts
    4

    How to export functions of a DLL in Visual Studio 2008 with the actual name?

    First I tried exporting like this:

    Code:
    __declspec (dllexport) int WINAPI foo(int foo2) {
       return 0;
    }
    But the actual name exported was not foo but something like _foo@43253.

    Then I tried this:

    Code:
    extern "C" __declspec (dllexport) int WINAPI foo(int foo2) {
       return 0;
    }
    And the exported name became similar to _foo@8.

    Is there any way that the exported name of the function is just foo?

    Thanks for your time.
    Last edited by Kall; 06-14-2009 at 12:49 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    __declspec (dllexport) should only be defined when compiling the dll. You could do something like this:

    Code:
    #ifndef DLL_HPP
    #define DLL_HPP
    
    #include <windows.h>
    
    #ifdef __cplusplus
    #define EXTERN_C extern "C"
    #else 
    #define EXTERN_C
    #endif
    #ifdef BUILDING_DLL
    #ifdef USE_DEFAULT_DLL_MAIN
    BOOL APIENTRY DllMain( HINSTANCE, DWORD, LPVOID )
    {
    	return TRUE;
    }
    #endif
    #define DYNAMIC EXTERN_C __declspec ( dllexport )
    #else
    #define DYNAMIC EXTERN_C
    #endif
    
    #endif /* DLL_HPP */
    And then:

    Code:
    // foo.hpp
    #ifndef FOO_HPP
    #define FOO_HPP
    #include "dll.hpp"
    
    DYNAMIC int WINAPI foo(int);
    
    #endif /* FOO_HPP */
    Code:
    // foo.cpp
    #define BUILDING_DLL
    #define USE_DEFAULT_DLLMAIN
    #include "dll.hpp"
    #include "foo.hpp"
    DYNAMIC int WINAPI foo(int foo2) {
       return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Madrid, Spain.
    Posts
    4

    Smile Thanks!

    Thank you very much for the info!

    Doing it that way makes it more maintainable, but the results are the same. I keep getting a dll with that function exported but with the name of "_foo@4" instead of "foo".

    I generated that dll with the actual foo function, just as you wrote it.

    Thanks for everything and your time,
    Kall.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the naming convention of the function is based on the calling convention. Since DLL's are (normally) called by STDCALL calling convention, it appends a @X where X is the number of bytes cleaned up by the calling function. This avoids problems where the caller passes N parameters, and the function cleans up the space of M parameters and M != N.

    In the CDECL calling convention, it's the caller cleaning up the stack, so the compiler in itself is responsible for both putting the arguments ON the stack and taking them OFF the stack when the function has returned. This means that there is no need to keep track of the number of bytes passed to the function in itself in the same way (e.g. passing an extra argument won't cause the stack to go out of whack).

    Why do you care what the function name is? I'm sure you have good reasons, I'm asking to understand what the right solution may be, not because I don't think you have a good reason.

    Edit: I should point out that WINAPI will become stdcall calling convention once the compiler sees the code.

    --
    Mats
    Last edited by matsp; 06-14-2009 at 04:57 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Madrid, Spain.
    Posts
    4

    Smile Thank you!

    Quote Originally Posted by matsp View Post
    Why do you care what the function name is? I'm sure you have good reasons, I'm asking to understand what the right solution may be, not because I don't think you have a good reason.
    Don't worry for asking . I'm trying to write a plugin for a closed source program (so I can't access its sources). The program looks for a certain function in plugins DLLs in runtime which represents an interface with it, that's why the exported name needs to be exactly the same what they say. That function returns a pointer to a structure composed by some flags and several defined functions so that it can interact with the plugin.

    In the SDK there is an example of how that function is (or may be) and they use WINAPI as calling convention.

    Thanks for everything and your time,
    Kall.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can use linker definition scripts (.def) to export functions with unmangled names.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    Madrid, Spain.
    Posts
    4

    Thanks!

    Quote Originally Posted by CornedBee View Post
    You can use linker definition scripts (.def) to export functions with unmangled names.
    Thanks! I'll try that and see if it works.

    If it is because of the calling conventions, can I remove WINAPI safely as it becomes stdcall in the end? I've noticed that removing it the name of the functions is the correct name, but I wonder if the program could load it correctly.

    Thanks for everything,
    Kall.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to Visual Studio 2008
    By bargomer in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2008, 02:16 PM
  2. Visual Studio crashed with using java.exe to load DLL
    By George2 in forum C Programming
    Replies: 1
    Last Post: 08-14-2007, 02:38 AM
  3. Visual Studio Express 2008 problems.
    By Normac in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 10:41 AM
  4. using classes
    By krazykrisi in forum C++ Programming
    Replies: 9
    Last Post: 11-22-2006, 10:41 AM
  5. Visual Studio Express for free
    By Frobozz in forum C# Programming
    Replies: 2
    Last Post: 04-29-2006, 09:59 PM

Tags for this Thread