C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-14-2009, 12:33 PM   #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.
Kall is offline   Reply With Quote
Old 06-14-2009, 01:45 PM   #2
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,895
__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;
}
Sebastiani is offline   Reply With Quote
Old 06-14-2009, 04:27 PM   #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.
Kall is offline   Reply With Quote
Old 06-14-2009, 04:51 PM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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
__________________
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.

Last edited by matsp; 06-14-2009 at 04:57 PM.
matsp is offline   Reply With Quote
Old 06-14-2009, 05:41 PM   #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.
Kall is offline   Reply With Quote
Old 06-15-2009, 05:54 AM   #6
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,433
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
CornedBee is offline   Reply With Quote
Old 06-16-2009, 05:37 AM   #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.
Kall is offline   Reply With Quote
Reply

Tags
dll, export, symbols, visual studio 2008

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:00 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22