开发者

passing interfaces as parameters in ATL

I have a native C++ dll and two classes:

classA
{
void setNumber(int number);
int getNumber();
}
classB
{
void setclassA(classA*);
classA* getclassA();
}

I have an ATL dll that wraps the native C++ dll and two interfaces: InterfaceForClassA , InterfaceForClassB .

InterfaceForClassA :

STDMETHODIMP CInterfaceForClassA::createClassAObject(void)
{
         ClassA* Aobject = new ClassA(); 
         return S_OK;
}
STDMETHODIMP CInterfaceForClassA::setNumberInClassA(SHORT pVal)
{     
     if(Aobject != NULL)
         Aobject->setNumber(pVal);
     return S_OK;
}
STDMETHODIMP CInterfaceForClassA::getNumberInClassA(SHORT* pVal)
{     
      if(Aobject != NULL)  
          *pVal = Aobject->getNumber();
      return S_OK;
}

InterfaceForClassB :

IInterfaceForClassA* clsAInterface;
STDMETHODIMP CInterfaceForClassB::setClassA(IInterfaceForClassA * val)
{
   val->QueryInterface(IID_IInterfaceForClassA,(void**)clsAInterface;
}
STDMETHODIMP CInterfaceForClassB::getClassA(IInterfaceForClassA ** returnVal)
{
   clsAInterface->QueryInterface(IID_IInterfaceForClassA,(void**)returnVal);
}

and finally i have a c# project that includes the ATL dll, in the c# project i call ATL dll 开发者_Go百科classes:

        InterfaceForClassA clsa1 = new InterfaceForClassA();
        clsa1.createClassAObject();
        clsa1.setNumberInClassA(12);

        InterfaceForClassB clsb = new InterfaceForClassB();
        clsb.setClassA(clsa1);

        InterfaceForClassA clsa2 = new InterfaceForClassA();
        clsa2 = clsb.getClassA();

        Console.WriteLine("number: " + clsa2.getNumberInClassA());

and when i run the c# project, it gives NullReferenceException error in the last line above. Can you help me?


Your CInterfaceForClassB::setClassA implementation is incorrect. It should be (note the &):

STDMETHODIMP CInterfaceForClassB::setClassA(IInterfaceForClassA * val)
{
   val->QueryInterface(IID_IInterfaceForClassA,(void**)&clsAInterface);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜