我了解如何防止在 c++ 中的单个函数上使用 extern "C" 进行名称修改,但是在导出成员函数时有什么方法可以防止它?
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
现在,当我想在 Unity 中使用这些函数时,我需要反编译 dll 以找出函数名称的 EntryPoints 是什么。我不想这样做。
我知道我对外部“C”有点过分热情......