当前位置: 首页 > 编程语言 > C#

Unity的“C++插件”“EntryPointNotFoundExeption”分享

时间:2023-04-10 13:08:55 C#

Unity的“C++插件”“EntryPointNotFoundExeption”.h#ifndef_WMIWRAPPER_H_#define_WMIWRAPPER_H_#include#include#include#include使用std::endl;使用std::wstring;使用std::wstringstream;#pragmacomment(lib,"wbemuuid.lib")staticclassWMIWrapper{public:__declspec(dllexport)WMIWrapper();__declspec(dllexport)~WMIWrapper();__declspec(dllexport)wstringCreateCOM();__declspec(dllexport)wstringCreateService();__declspec(dllexport)wstringGetMonitors();私有:IWbemLocator*_locator;IWbemServices*_服务;IEnumWbemClassObject*_monitors;};#endifWMIWrapper.cpp#include"WMIWrapper.h"extern"C"{WMIWrapper::WMIWrapper(){_locator=NULL;_service=NULL;}WMIWrapper::~WMIWrapper(){if(_service!=NULL)_service->Release();如果(_定位器!=NULL)_定位器->发布();}wstringWMIWrapper::CreateCOM(){wstringstreamERRStream(wstringstream::in|wstringstream::out);HRESULThRes=CoInitializeEx(NULL,COINIT_MULTITHREADED);如果(失败(hRes)){ERRStream<<“无法启动COM:0x”<Get(L"Description",0,&vRet,NULL,NULL))&&vRet.vt==VT_BSTR){//std::wcout<CreateCOM();wmiWrapper->CreateServiceW();返回wmiWrapper->GetMonitors();}}Unity脚本我们ingUnityEngine;使用System.Runtime.InteropServices;使用系统;公共类HardwareDiagnostics:MonoBehaviour{//[DllImport("WMIWrapper",EntryPoint="CreateCOM",CharSet=CharSet.Unicode)]//staticexternStringCreateCOM();////[DllImport("WMIWrapper",EntryPoint="CreateService",CharSet=CharSet.Unicode)]//staticexternStringCreateService();////[DllImport("WMIWrapper",EntryPoint="GetMonitors",CharSet=CharSet.Unicode)]//staticexternStringGetMonitors();[DllImport("WMIWrapper",EntryPoint="GetMonitor",CharSet=CharSet.Unicode)]staticexternstringGetMonitor();//用它来初始化voidStart(){Debug.Log(GetMonitor());Debug.Log("公鸡");}//Update每帧调用一次voidUpdate(){}}所以我试图从Unity脚本中调用这些成员函数,我得到了EntryPointNotFoundExeption错误我想这可能是因为你不能导出成员函数,所以我尝试编写“Interface.cpp”来执行这些函数并返回结果但返回相同的错误。UPDATE根据建议我已经将C++函数更改为这种格式voidWMIWrapper::CreateCOM(wchar_t*err,interrLength){.../DeterminewstringstreamERRStreamwcscpy_s(err,errLength,ERRStream.str().c_str());}而我的C#就像这样:publicclassHardwareDiagnostics:MonoBehaviour{[DllImport("WMIWrapper",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]privatestaticexternvoidCreateCOM(StringBuilderstr,intlength);[DllImport("WMIWrapper",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]privatestaticexternvoidCreateService(StringBuilderstr,intlength);[DllImport("WMIWrapper",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]privatestaticexternvoidGetMonitors(StringBuilderstr,intlength);//用它来初始化voidStart(){StringBuilderbuffer=newStringBuilder(255);CreateCOM(缓冲区,缓冲区。容量);Debug.Log(buffer.ToString());创建服务(缓冲区,缓冲区。容量);调试.Log(buffer.ToString());GetMonitors(缓冲区,缓冲区。容量);Debug.Log(buffer.ToString());}//Update每帧调用一次voidUpdate(){}}但是,当第一个函数CreateCOM();这是窗户吗?因为你错过了BoolWINAPIDllMain(HINSTANCEhinstDLL,DWORDfdwReason,LPVOID);defineifwindows请参阅CharlesPetzold的“Windows编程:第五版”第21章。如果你没有。得到它。所有Windows程序员都应该阅读它。DllMain函数在库首次启动和终止时被调用。DllMain的第一个参数是库的实例句柄。如果您的库使用需要实例句柄的资源(例如DialogBox),您应该将hInstance保存为全局变量。DllMain的最后一个参数是系统保留的...DLL_PROCESS_ATTACH的fdwReason值表示动态链接库已经映射到进程的地址空间。这是对库的提示,它可以执行任何初始化任务,以便为来自进程的后续请求提供服务……如果初始化成功,DllMain应该返回非零值。返回0将导致Windows无法运行该程序。当fdwReason的值为DLL_PROCESS_DETACH时,这意味着该进程不再需要DLL..清理...更高版本的VisualStudio可能会为您执行此操作-我不确定那部分。此外,如果您的函数名称被破坏,您将获得EntryPointNotFoundexception。即使使用extern"C"声明,您的函数名称被破坏的原因是因为您将extern"C"放在类方法周围。为了使其在没有损坏的名称的情况下工作,您必须进行C风格的函数声明。即#ifdef_cplusplusextern"C"{#endif__declspec(dllexport)wstringCreateCOM();...在课堂环境之外。通过将它们声明为类成员,即使使用extern"C"声明,名称仍然会被破坏。这会有帮助吗?将std::wstring的内容从C++返回到C#如何:使用C++Interop编组Unicode字符串默认字符串编组该错误源于函数名称被C++编译器破坏的事实。我认为在.cpp中用extern"C"{}包装函数声明解决了这个问题,但我想不是。[DllImport("WMIWrapper",EntryPoint="?CreateCOM@WMIWrapper@1@SAXPA_WH@Z",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]privatestaticexternvoidCreateCOM(StringBuilderstr,intlength);[DllImport("WMIWrapper",EntryPoint="?CreateServiceW@WMIWrapper@1@SAXPA_WH@Z",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]privatestaticexternvoidCreateService(StringBuilderstr,intlength);[DllImport("WMIWrapper",EntryPoint="?GetMonitors@WMIWrapper@1@SAXPA_WH@Z",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]privatestaticexternvoidGetMonitors(StringBuilderstr,intlength);在这种情况下正确使用extern"C"会很有帮助。以上就是C#学习教程:Unity的《C++插件》《EntryPointNotFoundExeption》分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文来自网络收集,不代表作品如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: