-4

Possible Duplicate:
C++ plugin for Unity “EntryPointNotFoundExeption”

I understand how to prevent name mangling with extern "C" on individual functions in c++, but is there any way to prevent it when exporting member functions?

WMIWrapper.cpp

namespace WMIWrapper
{

    extern "C" {

        WMIWrapper::WMIWrapper()
        {
            _locator = NULL;
            _service = NULL;
            _monitors = NULL;
        }

        WMIWrapper::~WMIWrapper()
        {
            if(_service != NULL)
                _service->Release();
            if(_locator != NULL)
                _locator->Release();
        }

        void WMIWrapper::CreateCOM(wchar_t* err, int errLength)
        {
            wstringstream ERRStream (wstringstream::in | wstringstream::out);
            HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);  
            if(FAILED(hRes))  
            {  
                ERRStream << "Unable to launch COM: 0x" << std::hex << hRes << endl; 
            } 

            hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
            if(FAILED(hRes))
            {
                ERRStream << "Unable to set security level for COM: " << std::hex << hRes << endl;
            } 

            if(FAILED(hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&_locator))))  
            {  
                ERRStream << "Unable to create a WbemLocator: " << std::hex << hRes << endl;   
            }

            if(ERRStream != NULL)
                wcscpy_s(err, errLength, ERRStream.str().c_str());
        }

        void WMIWrapper::CreateService(wchar_t* err, int errLength)
        {
            wstringstream ERRStream (wstringstream::in | wstringstream::out);
            HRESULT hRes;
            if(_locator == NULL || FAILED(hRes = _locator->ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &_service)))  
            {  
                ERRStream << "Unable to connect to \"CIMV2\": " << std::hex << hRes << endl; 
            }  

            if(ERRStream != NULL)
                wcscpy_s(err, errLength, ERRStream.str().c_str());
        }

        void WMIWrapper::GetMonitors(wchar_t* err, int errLength)
        {
            HRESULT hRes;
            wstringstream ssMonitorDescription;
            if(_locator == NULL 
                || _service == NULL
                || FAILED(hRes = _service->ExecQuery(L"WQL", L"SELECT * FROM Win32_DesktopMonitor", WBEM_FLAG_FORWARD_ONLY, NULL, &_monitors)))
            {
                ssMonitorDescription << "Unable to retrieve desktop monitors: " << std::hex << hRes << endl;
                wcscpy_s(err, errLength, ssMonitorDescription.str().c_str());
                return;
            }

            IWbemClassObject* clsObj = NULL;
            int numElems;
            while((hRes = _monitors->Next(WBEM_INFINITE, 1, &clsObj, (ULONG*)&numElems)) != WBEM_S_FALSE)
            {
                if(FAILED(hRes))
                    break;

                VARIANT vRet;
                VariantInit(&vRet);
                if(SUCCEEDED(clsObj->Get(L"Description", 0, &vRet, NULL, NULL)) && vRet.vt == VT_BSTR)
                {
                    ssMonitorDescription << "Description: " << vRet.bstrVal << endl;
                    VariantClear(&vRet);
                }
            }

            clsObj->Release();

            wcscpy_s(err, errLength, ssMonitorDescription.str().c_str());
        }

        void WMIWrapper::HelloWorld(wchar_t* testString, int length)
        {
            wstring hello = L"Hello World";
            wcscpy_s(testString, length, hello.c_str());
        }
    }
}

WMIWrapper.h

#ifndef _WMIWRAPPER_H_
#define _WMIWRAPPER_H_

#include <Windows.h>  
#include <sstream>  
#include <iostream>
#include <WbemCli.h>  

using std::endl;
using std::wstring;
using std::wstringstream;

#pragma comment(lib, "wbemuuid.lib")  

namespace WMIWrapper
{
    extern "C" {

        class WMIWrapper 
        {  
        public:
            WMIWrapper();
            ~WMIWrapper();


            __declspec(dllexport) void CreateCOM(wchar_t*, int);
            __declspec(dllexport) void CreateService(wchar_t*, int);
            __declspec(dllexport) void GetMonitors(wchar_t*, int);
            __declspec(dllexport) void HelloWorld(wchar_t*, int);


        private:
            IWbemLocator* _locator;
            IWbemServices* _service;
            IEnumWbemClassObject* _monitors;
        };
    }
}

#endif

Now when I want to use those functions in Unity, I need to decompile the dll to find out what my EntryPoints are for the function names. I do not want to have to do this.

I know I got a little overzealous with with the extern "C"...

Community
  • 1
  • 1
Josh Elias
  • 3,250
  • 7
  • 42
  • 73
  • voting to close as it has been asked repeatedly over the last few days [C++ plugin for Unity "EntryPointNotFoundExeption"](http://stackoverflow.com/questions/11619986/c-plugin-for-unity-entrypointnotfoundexeption) – ssube Jul 25 '12 at 18:30
  • It's the same project, not the same question. – Josh Elias Jul 26 '12 at 14:36

1 Answers1

3

UPDATE: As @peechykeen pointed out in the comments, if you are going to use .def then you can directly rename your mangled names. I'm leaving the original answer although it's much more useful for hiding the exported names rather then renaming them.

Original answer:

One way to do it is to "hide" the exported names behind ordinals. For that you will need to define a .def file and then in its EXPORTS section put all the exported names you want to hide. For example, to assign ordinal 1 to a function exported by a Boost serialization you would do this:

EXPORTS
??0?$oserializer@Vportable_binary_oarchive@@U?$pair@$$CBHH@std@@@detail@archive@boost@@QEAA@XZ  @1  NONAME

And so on for all the functions. Now, doing this manually is both tedious and error prone. And you will get link errors every time you change any part of the exported interface. To semi-automate it, I use Dependency Walker and a Perl script. It works like this:

  1. In .def file place markers in EXPORTS section:

    EXPORTS
    ;BEGIN_RENAMING_TAG
    ;END_RENAMING_TAG
    
  2. Load the binary into Dependency Walker, go to exported functions window, select all the exported functions and copy them to clipboard.

  3. Past the copied text between BEGIN/END tags in the .def file.
  4. Run the following Perl script on the .def file:

    #perl -w
    print $ARGC;
    die "ERROR: Provide name of one DEF file to process\n" if @ARGV != 1;
    
    my $renaming = 0;
    my $counter = 1;
    my $fileName = $ARGV[0];
    my @lines;
    open(FILE, $fileName) or die $!;
    while(<FILE>)
    {
        if(/;END_RENAMING_TAG/)
        {
            $renaming = 0;
        }
    
        if($renaming == 1)
        {
            chomp;
            my $line = $_."\t@".$counter."\tNONAME";
            push(@lines, $line);
            ++$counter;
        }
        else
        {
            chomp;
            push(@lines, $_);
        }
    
        if(/;BEGIN_RENAMING_TAG/)
        {
            $renaming = 1;
        }
    }
    
    close FILE;
    open(FILE, ">$fileName") or die $!;
    print FILE join("\n", @lines);
    close FILE;
    
  5. Entries in your .def file are now all in <entry> @<ordinal> NONAME format.

Now, I don't use these ordinals to access the functions, I just rename them as it bugs me to expose hundreds of exported functions that I don't need (Boost serialization that I use in turn uses dllexport to force linking of its functions). So you would need to do more in Perl script and say export an enum with function names and ordinals. If you want to do this right you would have to implement a demangling algorithm in the Perl script that would give you accurate names, take care of overloading (same name, different args) and keep the enum name constants.

In order to access the functions behind ordinals, you use GetProcAddress but cast the ordinal to LPCSTR. See the documentation for the function for more details.

ierceg
  • 418
  • 1
  • 8
  • 11
  • If you're going to use a `def`, you may as well take the extra few seconds to provide a name instead of relying on possibly-changing ordinals. Symbols can be exported using a different name than declared internally (which may be used with ordinals). – ssube Jul 25 '12 at 15:34
  • @peachykeen You're right - I thought that could be done but then I missed it when double-checked EXPORTS documentation. I'll edit the answer to reflect this. – ierceg Jul 25 '12 at 15:51