Search code examples
c++windowsvisual-c++comimporterror

How to handle multiple imports that have the same namespace?


I'm working on a C++ command line application that is a Component Object Model (COM) client. There are two COM server implementations and I'd like to support both of them in my program (choosable via a application parameter).

Both server are using the same namespace OmniRig, so everything clashes. I tried to split them into separate classes to avoid the conflict but that didn't work. Is is possible to support both COM servers and how would I go about doing so?

OmniRigV1.h:

#include "OmniRigBase.h"

#import "C:\Program Files (x86)\Afreet\OmniRig\OmniRig.exe"
using namespace OmniRig;

class OmniRigV1 : public OmniRigBase {...}

OmniRigV2.h:

#include "OmniRigBase.h"

#import "C:\Program Files (x86)\Omni-Rig V2\omnirig2.exe"
using namespace OmniRig;

class OmniRigV2 : public OmniRigBase {...}

main.cpp:

int main(int argc, char* argv[])
{
    ProgramOptions options(argc, argv);
    OmniRigBase* omnirig;
    switch (options.getOmnirigVersion()) {
        case OmniRigVersion::OmniRigVersion1:
            omnirig = new OmniRigV1(options);
            break;
        case OmniRigVersion::OmniRigVersion2:
            omnirig = new OmniRigV2(options);
            break;
        default:
            exit(E_OPTION_OMNIRIG_VERSION);
    }
...

Complete code at: https://github.com/cniesen/IcomClockOmniRig/tree/adf7ce1b0ef716ec2f72d50bffbada4e811a52cf/src


Solution

  • As Ingor mentioned in the comments, I needed to use the rename_namespace option with the #import directive:

    #import "C:\Program Files (x86)\Afreet\OmniRig\OmniRig.exe" rename_namespace("OmniRig1")
    

    and

    #import "C:\Program Files (x86)\Omni-Rig V2\omnirig2.exe" rename_namespace("OmniRig2")
    

    Now each implementation can be accessed with the appropriate name like OmniRig1::OmniRigX and OmniRig2::OmniRigX

    Credit and thanks goes to Ingor.