我正在尝试使用 Visual Studio 2010 生成的 COM 服务器。 当我运行 CreateInstance(..) 时,收到错误代码 0x80040154 。 我启用了融合日志并运行了fuslogvw。 Fuslogvw 报告程序集加载(由未知调用程序程序集)
mscorlib.resources, Version=4.0.0.0, Culture=zh-HK, PublicKeyToken=b77a5c561934e089
我已经安装了.net Framework 4繁体中文和简体中文语言包。
请帮忙。提前致谢。
我的C++代码如下:
#include "stdafx.h"
#import "/Project/TestCOM/MyProject.tlb"
#include <comutil.h>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
MyProject::_PHPStarterPtr pCalc;
HRESULT hRes = pCalc.CreateInstance(__uuidof(MyProject::PHPStarter));
if(FAILED(hRes))
{
printf("MyProject::_PHPStarterPtr failed w/err 0x%08lx\n", hRes); // it printed MyProject::_PHPStarterPtr failed w/err 0x80040154
}
else
{
pCalc->AddRef();
bstr_t s("hello");
bstr_t outStr = pCalc->Echo(s);
printf("outStr = %s\n", (const char *)outStr);
pCalc->Release();
printf("Dispose() done\n");
}
CoUninitialize();
return 0;
}
最佳答案
0x80040154 表示“类未注册”。要在 .NET 程序集中注册 COM 组件,请使用 regasm.exe tool来自 .NET Framework 目录。它可以手动完成,也可以作为自定义构建后步骤完成。
关于c++ - 如何使用C++加载C#编写的COM服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598665/