Menu

Using Microsoft style mangled name

2008-03-09
2012-09-26
  • Tim Browning

    Tim Browning - 2008-03-09

    I am currently writing a plugin for an app that calls a couple of functions (if present in the DLL) by their mangled names. Unfortunately it calls them by Microsoft mangled names. The functions are:

    ?Constructor@@YAXXZ
    ?Destructor@@YAXXZ

    I need to be able to set up a Dev-C++ DLL to be called by these names. The functions are as such:

    __declspec(dllexport) void Constructor ( void )
    {

    }

    __declspec(dllexport) void Destructor ( void )
    {

    }

    I may be mistaken, but I got the impression that this may be possible by using a custome .def file. Am I wrong in thinking this? If this is true, how would I go about setting this up? If I am wrong, but it is possible, how would I go about doing this?

    Any help with this is appreciated. I just started with C++ a few weeks ago (previously worked with VB6, VB.NET, and C#), so this is all quite new to me.

     
    • cpns

      cpns - 2008-03-10

      If you have the DLL source you have two choices:

      1) Rebuild the DLL using Dev-C++
      2) Declare the functions with "C" linkage and rebuild with either tool.

      For (2) in the header for the DLL:

      extern "C"
      {
      declspec(dllexport) void Constructor ( void ) ;
      declspec(dllexport) void Destructor ( void ) ;
      }

      Both the DLL and teh code using teh DLL must then be rebuilt with this new declaration. Note the the code defining these functions must include this header to enforce the declaration.

      The functions will then loose the mangling. Note that this also precludes the ability to overload these functions, for them to be member functions, for them to take parameters or return values of class type, or to take or return reference types; i.e. the interface cannot have anything that is not valid in C.

      If you cannot rebuild the DLL, then you could load the DLL and access the functions at run-time using Loadbrary() and GetProcAddress(). But using knowledge of mangled names is entirely non-portable. The names could change even from simply being re-compiled by a different version of the same vendor's compiler.

      Clifford

       
      • cpns

        cpns - 2008-03-10

        Well, I suppose that if you are really desparate to make this work, you could write it with C linkage but use the mangled names. Unfortunately names containing a ? much less beginning with one are not legal.

        If the question is simply whether MinGW has a setting to use Microsoft's name mangling, then the answer is no.

        In your situation, the obvious solution is to use a Microsoft compiler. VC++ 2008 Express Edition is both free and to be quite honest far superior to Dev-C++. If you have a particular like fo rteh Dev-C++ environment, you can use the derivitive wxDev-C++ with Microsoft toolchains.

        Clifford

         
    • Tim Browning

      Tim Browning - 2008-03-10

      Clifford,

      Thanks for the reply. My problem is a little different than the one you describe a solution to. I already know how to get rid of name mangling, and I am writing the DLL... it is the application that I don't have the source for.

      The problem is that this application calls the functions from plugin DLL's by the Microsoft mangled name. I need to be able to provide this style mangled name from Dev-C++. If I tried using extern "C" to get rid of name mangling then the application would still be calling the function by the wrong name since it would be calling for "?Constructor@@YAXXZ" (Microsoft mangled) instead of "Contructor" (extern "C" unmangled).

      Since I already know the Microsoft mangled name for this function signature, is there a way I can provide the mangled name I want to use, and force the Dev-C++ to use the Microsoft mangled name I provide? Possibly in a custom .def file?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.