dan_l
dan_l

Reputation: 1682

how to use C++ load a COM server written in C#?

I am trying to use a COM server generated by Visual Studio 2010 . When I run CreateInstance(..) , I get error code 0x80040154 . I enabled fusion log and ran fuslogvw . Fuslogvw reported that loading of assembly (by unknown caller assembly)

mscorlib.resources, Version=4.0.0.0, Culture=zh-HK, PublicKeyToken=b77a5c561934e089

I have already installed .net framework 4 language pack for traditional chinese and also simplified chinese .

Please help . Thanks in advance.

My c++ code is as follow:

#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;
}

Upvotes: 2

Views: 180

Answers (1)

cynic
cynic

Reputation: 5415

0x80040154 means "class not registered". To register COM components inside .NET assemblies, you use regasm.exe tool from the .NET framework directory. It can be done manually, or as a custom post-build step.

Upvotes: 3

Related Questions