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”…
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:
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:
In .def file place markers in EXPORTS section:
Load the binary into Dependency Walker, go to exported functions window, select all the exported functions and copy them to clipboard.
Run the following Perl script on the .def file:
Entries in your .def file are now all in
<entry> @<ordinal> NONAMEformat.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
dllexportto 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
GetProcAddressbut cast the ordinal to LPCSTR. See the documentation for the function for more details.