• Welcome to Powerbasic Museum 2020-B.
 

ProgEx14 - Name Mangling In C++ And Ways To Deal With It

Started by Frederick J. Harris, November 06, 2009, 03:52:54 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/*
   ProgEx14
   
   Below we have the same C++ program as in the last example except that we've
   removed the extern "C" from in front of the exported function prototype.
   Create a new C++ Dll project named ProgEx14 and place this dllMain.cpp file
   in it.  If you copy this text into Notepad or your editor and save it as
   dllMain.cpp, when you create the project you can right-click on the project
   name in most Development Environments and you'll have a context sensitive
   menu selection that will allow you to 'Add' Source files to a project, and
   this is a good and fast way to create projects if you have the various .cpp,
   .h, and .rc files that constitute the project.  Anyway, compile this into
   ProgEx14.dll, then turn to the next version of dllHost1.bas which you should
   place in the ProgEx14 directory.
*/
#include <stdio.h>
__declspec(dllexport) void Print(char*);

void Print(char* msg)
{
printf("%s\n",msg);
}


Below is the PowerBASIC Console Compiler program that attempts to load the above C++ dll.  Compile it in the same directory with the C++ dll so it can be easily found...


'dllHost1.bas
'
'If you examine the libProgEx14.def file produced by the C++ compiler for
'ProgEx14 you'll see something that looks like a dog's breakfast...
'
'EXPORTS
';  Print(char*)
'  _Z5PrintPc @ 1
'
'What you're seeing here is C++ 'name mangling'.  In C++ you can have any
'number of functions (both ordinary functions & class member functions)
'that are named exactly the same, so long as their parameter lists are
'different.  For a trivial example, suppose you wanted a function to increment
'a parameter by one.  You could have an IncrOne(int iNum) that takes an integer
'parameter, and a IncrOne(double dblNum) that takes a double parameter.  The
'compiler would set up the code to call the correct function based on the
'differing parameters.  However, the way it sets up this trick it to work with
'alias names of the functions that it creates in non-standardized ways
'involving the size of the parameters and their count - and usually prefaced
'with an underscore.  One way of finding out what name the compiler exported
'is to look in the module definition file as seen above.  It will list the
'exports.  If you don't have the module definition file its a bit more
'challenging.  There are special tools as part of most compiler systems to
'help with this in terms of dumping the exports from the binaries, but some-
'times good old Notepad will do if you open up the dll and search for symbols.
'Naturaly, don't change anything or the file will be kaputt.  Anyway, you can
'see below what I had to do to get the dll to successfully run.
'
'In our next series of programs, lets look at 'Explicit Loading of Dlls'.


#Compile Exe
#Dim All
Declare Sub Prnt CDecl Lib "ProgEx14.dll" Alias "_Z5PrintPc" (Byref szMsg As Asciiz)

Function PBMain() As Long
  Call Prnt("Hello, World!")
  Waitkey$

  PBMain=0
End Function