1

I created a DLL inside of MV C++ 2012 and when I used

Dumpbin /Exports filename

The name of the function inside of the DLL file has an equal sign inside of it. I had to use Common Language Runtime Support (/crl) because I used a DLL from C#. Is this why the name of the function would show up with an equals sign? My header file:

#ifdef ColorDLL_EXPORTS
#define ColorDLL_API __declspec(dllexport)
#else
#define ColorDLL_API __declspec(dllexport)
#endif

extern "C"{
ColorDLL_API int ColorSelect(int i);
}

ColorDLL.cpp

#include "stdafx.h"
#include "ColorDLL.h"
#using <ColorDiologeClass.dll>

extern "C"{
ColorDLL_API int ColorSelect(){
ColorDiologeClass::Class1::ColorReturn(1);
return 1;
}

} When I used Dumpbin the name showed up as this:

Name
ColorSelect = _ColorSelect

Why is this? I am expecting it to show up as ColorSelect, not ColorSelect = _ColorSelect. And if I were to leave it this way, how would I call this function from a program like JMP where it needs the exact function name? Would it be ColorSelect? Or would it be ColorSelect = _ColorSelect?

user1334858
  • 1,885
  • 5
  • 30
  • 39
  • Don't fix it, this is a Good Thing. It prevents the C# program from dying a horrible death when you change your C++ code. Just use the EntryPoint property in the [DllImport] attribute. Then again, when you compiler with /clr then this isn't needed at all. Just create a public ref class. – Hans Passant May 31 '13 at 22:31
  • Create a public class for the function and it will fix the decorating? – user1334858 May 31 '13 at 23:52
  • You say don't fix it. But how can I use this function outside of MV if it is decorated? – user1334858 May 31 '13 at 23:53

3 Answers3

0

The name is "mangled" - the return type and the parameters are enchoded into the name of the function. Should you wish to NOT have that, you would use extern "C" before the function name (or around a block of functions).

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I Tried to use extern "C" in the header and the .cpp and it got rid of the decoration. it now looks like this Name: ColorSelect = _ColorSelect But I want it to look like Name: ColorSelect Do you know how to get rid of the = _ColorSelect – user1334858 Jun 03 '13 at 14:22
  • @user1334858 : You can't - it's part of the C language's naming convention (for this compiler). – Mats Petersson Jun 03 '13 at 23:28
0

That would be name mangling, which is the under-the-covers feature of c++ that allows it to support function overloading (since it incorporates the argument types of the function into its name).

Here's another question that goes into greater detail.

Community
  • 1
  • 1
Wug
  • 12,956
  • 4
  • 34
  • 54
0

Microsoft calls this "decorating" instead of mangling. They include a command line tool named "undname" that will produce the original name from the decorated name:

C:\>undname ?ColorSelect@@YAHXZ
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Undecoration of :- "?ColorSelect@@YAHXZ"
is :- "int __cdecl ColorSelect(void)"

If you want to do the same in your own code, you can do that too, using UnDecorateSymbolName.

For what it's worth, decorating/mangling supports not only overloading, but typesafe linking. Typesafe linking stems from function overloading though it isn't really function overloading in itself.

Specifically, typesafe linking deals with (for example) how to deal with C++ code that has overloads of, say, sqrt for float, double, long double, and probably complex as well, but links to a C library that provides a double sqrt(double), but not the other overloads. In this case, we typically want that to be used when the right arguments were/are used, but not otherwise.

This can (or could) arise even without function overloading being involved. For example, in pure C you could do something like this:

#include <stdio.h>
extern int sqrt(int);

// ...
printf("%d", sqrt(100));

Now, we've told the compiler we're using a version of sqrt that takes (and returns) an int. Unfortunately, the linker doesn't realize that, so it still links with the sqrt in the standard library that takes and returns double. As a result, the code above will print some thoroughly useless result (typically 0, not that it matters a lot).

Typesafe linkage prevents that -- even though it isn't exactly function overloading, we still have two functions with the same name, but different types by the time we're linking. By encoding the parameter type(s) into the name, the linker can keep this sorted out just as well as the compiler can.

The same can (and frequently does) arise in C when we have name collisions between different libraries. With a traditional C compiler, straightening out this sort of mess can be extremely difficult (at best). With a C++ compiler, unless the two libraries use not only the same names, but identical number and types of parameters, it's never a problem at all.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So the ultimate goal is to call it in JMP and it needs the exact function name. Would I be able to use the original function name? Or is it stored in the DLL in as the decorated name? – user1334858 May 31 '13 at 23:34
  • @user1334858: It's stored in the DLL as the decorated name. – Jerry Coffin Jun 01 '13 at 00:00