0

我需要一些认真的帮助......我正在尝试导出我的成员函数,以便我可以在 C# 中调用它们

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")  

static class WMIWrapper 
{  
public:
    __declspec(dllexport) WMIWrapper();
    __declspec(dllexport) ~WMIWrapper();

    __declspec(dllexport) wstring CreateCOM();
    __declspec(dllexport) wstring CreateService();
__declspec(dllexport) wstring GetMonitors();

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

#endif

WMIWrapper.cpp

#include "WMIWrapper.h"


extern "C" {

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

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

    wstring WMIWrapper::CreateCOM()
    {
        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;
            return L"";  
        }  

        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;
            return L"";
        }

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

        const std::wstring& myWString = ERRStream.str();
        const LPCWSTR p = myWString.c_str();
        return p;

    }

    wstring WMIWrapper::CreateService()
    {
        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;  
            return L"";  
        }  

        const std::wstring& myWString = ERRStream.str();
        const LPCWSTR p = myWString.c_str();
        return p;
    }

    wstring WMIWrapper::GetMonitors()
    {
        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)))
        {
            //ERRStream << "Unable to retrieve desktop monitors: " << std::hex << hRes << endl;
            return L"";
        }

        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)
            {
                //std::wcout <<  L"Description: " << vRet.bstrVal << endl;
                ssMonitorDescription << "Description: " << vRet.bstrVal << endl;
                VariantClear(&vRet);
            }
        }

        clsObj->Release();

        return ssMonitorDescription.str();
    }
}

接口.cpp

#include "WMIWrapper.h"

extern "C" 
{
    __declspec( dllexport ) wstring GetMonitor()
    {
        WMIWrapper* wmiWrapper = new WMIWrapper();
        wmiWrapper->CreateCOM();
        wmiWrapper->CreateServiceW();
        return wmiWrapper->GetMonitors();
    }
}

统一脚本

using UnityEngine;
using System.Runtime.InteropServices;
using System;


public class HardwareDiagnostics : MonoBehaviour {

    //[DllImport("WMIWrapper", EntryPoint="CreateCOM", CharSet = CharSet.Unicode)]
    //static extern String CreateCOM();
    //
    //[DllImport("WMIWrapper", EntryPoint="CreateService", CharSet = CharSet.Unicode)]
    //static extern String CreateService();
    //
    //[DllImport("WMIWrapper", EntryPoint="GetMonitors", CharSet = CharSet.Unicode)]
    //static extern String GetMonitors();
    [DllImport("WMIWrapper", EntryPoint = "GetMonitor", CharSet = CharSet.Unicode)]
    static extern string GetMonitor();

    // Use this for initialization
    void Start () {
        Debug.Log(GetMonitor());
        Debug.Log ("Cock");

    }

    // Update is called once per frame
    void Update () {

    }


}

所以我试图从 Unity 脚本中调用这些成员函数,我得到了EntryPointNotFoundExeption错误。我想可能是因为您无法导出成员函数,所以我尝试编写“Interface.cpp”来执行这些函数并返回结果,但返回相同的错误。

更新

根据建议,我已将 C++ 函数更改为这种格式

void WMIWrapper::CreateCOM(wchar_t* err, int errLength)
    {
        .../Determine wstringstream ERRStream


        wcscpy_s(err, errLength, ERRStream.str().c_str());

    }

我的 C# 是这样的:

public class HardwareDiagnostics : MonoBehaviour {

    [DllImport( "WMIWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    private static extern void CreateCOM(StringBuilder str, int length);

    [DllImport( "WMIWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    private static extern void CreateService(StringBuilder str, int length);

    [DllImport( "WMIWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    private static extern void GetMonitors(StringBuilder str, int length);

    // Use this for initialization
    void Start () {
        StringBuilder buffer = new StringBuilder(255);

        CreateCOM(buffer, buffer.Capacity);
        Debug.Log(buffer.ToString());

        CreateService(buffer, buffer.Capacity);
        Debug.Log(buffer.ToString());

        GetMonitors(buffer, buffer.Capacity);
        Debug.Log(buffer.ToString());

    }

    // Update is called once per frame
    void Update () {

    }


}

但是,在调用第一个函数 CreateCOM() 时,我仍然收到“EntryPointNotFoundExeption”;

4

3 回答 3

2

这是窗户吗?因为你错过了你的

Bool WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID);

如果是 windows 的定义。

请参阅 Charles Petzold 的“Programming Windows: Fifth Edition”第 21 章。

如果你没有它。得到它。所有 Windows 程序员都应该阅读它。

DllMain 函数在库首次启动和终止时调用。DllMain 的第一个参数是库的实例句柄。如果您的库使用需要实例句柄的资源(例如 DialogBox),则应将 hInstance 保存为全局变量。DllMain 的最后一个参数由系统保留...

DLL_PROCESS_ATTACH 的 fdwReason 值表示动态链接库已映射到进程的地址空间。这是库执行任何初始化任务所需的提示,以便为来自进程的后续请求提供服务......

如果初始化成功,DllMain 应该返回一个非零值。返回 0 将导致 Windows 不运行该程序。

当 fdwReason 的值为 DLL_PROCESS_DETACH 时,表示进程不再需要该 DLL..清理...

Visual Studio 的更高版本可能会为您执行此操作——我不确定那部分。

此外,如果您的函数名称被破坏,您将收到一个 EntryPointNotFound 异常。即使使用 extern "C" 声明,您的函数名称也被破坏的原因是因为您在类方法周围放置了 extern "C"。为了让它在没有损坏名称的情况下工作,您必须进行 C 风格的函数声明。

IE

#ifdef _cplusplus
extern "C" {
#endif

__declspec(dllexport) wstring CreateCOM();

...

在类的上下文之外。通过将这些声明为类成员,即使使用 extern "C" 声明,名称仍然会被破坏。

于 2012-07-24T14:29:13.193 回答
-1

错误是因为函数名被 C++ 编译器弄乱了。我认为用 extern "C" {} 将函数声明包装在 .cpp 中解决了这个问题,但我想不是。

[DllImport( "WMIWrapper", EntryPoint = "?CreateCOM@WMIWrapper@1@SAXPA_WH@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
private static extern void CreateCOM(StringBuilder str, int length);

[DllImport( "WMIWrapper", EntryPoint = "?CreateServiceW@WMIWrapper@1@SAXPA_WH@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
private static extern void CreateService(StringBuilder str, int length);

[DllImport( "WMIWrapper", EntryPoint = "?GetMonitors@WMIWrapper@1@SAXPA_WH@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
private static extern void GetMonitors(StringBuilder str, int length);

如果有人知道如何在这种情况下正确使用 extern "C",那将非常有帮助。

于 2012-07-24T18:14:07.380 回答