D
D
D0ct0r_Murder2018-02-25 17:27:44
C++ / C#
D0ct0r_Murder, 2018-02-25 17:27:44

What are the errors - I implement the com interface in dll.?

13 mistakes

#include "stdafx.h"
#include <iostream>
#include <ole.h>
#include <Unknwn.h>

extern "C" __declspec(dllexport) const IID IID_IX = { 0xb3d5176e, 0x592a, 0x4fb5, 0xa866, 0x802ca839f6b3 };
extern "C" __declspec(dllexport) const IID IID_IY = { 0xf4ee8427, 0x53d3, 0x4595, 0xac0d, 0x15d5856e9ba0 };

extern "C" __declspec(dllexport) __interface IX :  IUnknown
{
  virtual std::string _stdcall FxMessage() = 0;
};

extern "C" __declspec(dllexport) __interface IY : IUnknown
{
  virtual std::string _stdcall FyMessage() = 0;
};

class CA : public IX, public IY
{
private:
  int m_cRef;

public:
  CA();

  HRESULT _stdcall QueryInterface(const IID &iid, void** ppv);
  ULONG _stdcall AddRef();
  ULONG _stdcall Release();

  std::string _stdcall FxMessage();
  std::string _stdcall FyMessage();
};

CA::CA()
{
  m_cRef = 0;
}

HRESULT _stdcall CA::QueryInterface(const IID &iid, void** ppv)
{
  if (iid == IID_IX)
    *ppv = static_cast<IX*>(this);
  else if (iid == IID_IY)
    *ppv = static_cast<IY*>(this);
  else if (iid == IID_IUnknown)
    *ppv = static_cast<IUnknown*>(static_cast<IX*>(this));
  else
  {
    *ppv = NULL;

    return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 512);
  }

  static_cast<IUnknown*>(static_cast<IX*>(this))->AddRef();
  return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_ITF, 1);
}

ULONG _stdcall CA::AddRef()
{
  return ++m_cRef;
}

ULONG _stdcall CA::Release()
{
  if ((--m_cRef) == 0)
    delete this;
  return m_cRef;
}

std::string _stdcall CA::FxMessage()
{
  return "FxMessage";
}

std::string _stdcall CA::FyMessage()
{
  return "FyMessage";
}

extern "C" __declspec(dllexport) IUnknown* CreateInstance()
{
  IUnknown* pI = static_cast<IUnknown*>(static_cast<void*>(new CA));
  pI->AddRef();
  return pI;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-02-25
@jcmvbkbc

D0ct0r_Murder inheritance from IUnknown must be public and virtual. At the same time, double type casts can be replaced by single ones. And in a good way - use ATL and do not write this code by hand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question