COM in C++

So I am trying to write a program that will instantiate and control a COM object, in this case a telescope dome simulator (DomeSim.exe). So for example, if I tell the program to park the dome, the simulator will park etc. Using COM in C++ is quite annoying and difficult, and at the moment I am just trying to implement one method, and even still I can't tell if I'm doing it right as I'm just going off online tutorials and examples. Here is the Code below.

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <windows.h>
#include <C:\Users\Usman-Khan\Desktop\dome_script\DomeSimulator.h>
#import <C:\Users\Usman-Khan\Desktop\dome_script\DomeSim.exe>

//-----------------------------------------------------------------------------
// Declarations
//-----------------------------------------------------------------------------

const IID DomeSimulator::IID_Dome =
{ 0xdb93d7ba, 0xb7f6, 0x4734, { 0x92, 0x34, 0x3e, 0x39, 0xa1, 0xa5, 0xa5, 0xa3 }} ;
;

/******************************************************************************
******************************************************************************/
DomeSimulator::DomeSimulator()
{
initializeCOM();
}

/******************************************************************************
******************************************************************************/
DomeSimulator::~DomeSimulator()
{
uninitializeCOM();
}

/******************************************************************************
******************************************************************************/
void DomeSimulator::initializeCOM()
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
pIUnknown = NULL;
//m_IDome = NULL;
}
/******************************************************************************
******************************************************************************/
bool DomeSimulator::loadCOM()
{
if( pIUnknown != NULL )
{
pIUnknown->Release();
pIUnknown = NULL;
}
CLSID ASCOMDomeSimulator;
CLSIDFromProgID( L"DomeSim.Dome", &ASCOMDomeSimulator );
HRESULT hr = CoCreateInstance(ASCOMDomeSimulator, NULL, CLSCTX_ALL, IID_IUnknown, reinterpret_cast<void**>(&pIUnknown));
if (SUCCEEDED(hr))
{
//hr = pIUnknown->QueryInterface(IID_Dome,(void**)&m_IDome );
return true;
}
else
{
return false;
}
}
/******************************************************************************
******************************************************************************/
void DomeSimulator::uninitializeCOM()
{
//if( m_IDome != NULL )
//{
// m_IDome->Release();
// m_IDome = NULL;
//}
if( pIUnknown != NULL )
{
pIUnknown->Release();
pIUnknown = NULL;
}
CoUninitialize();
}
/******************************************************************************
******************************************************************************/
bool DomeSimulator::get_AtHome() const
{
//if( m_IDome != NULL )
//{
// return m_IDome->get_AtHome();
//}
return 0;
}
/******************************************************************************
******************************************************************************/
//-----------------------------------------------------------------------------
// Execution
//-----------------------------------------------------------------------------
int main(void)
{
DomeSimulator instance;
bool home = &DomeSimulator::get_AtHome;
return home;
};
)

That was the main code, while the code below is the wrapper file DomeSimulator.h

#include <windows.h>
#include <string>
#include <vector>
//int *pIUnknown;

//namespace DomeSim {
// struct _Domes{};
//};

//Wrapper Class
class DomeSimulator
{
public:
DomeSimulator();
~DomeSimulator();
void initializeCOM();
bool loadCOM();
void uninitializeCOM();
bool get_AtHome() const;
private:
static const IID IID_Dome;
//DomeSim::_Domes *m_IDome;
IUnknown *pIUnknown;
};

So the main method I'm trying to get to use atm is the get_AtHome method, which should be simple enough. I just want to know if I am going about this the right way, and what I need to do to fix it as COM is quite confusing.
Last edited on
Using the #import directive is the accepted way to go about it in C++.

I wish I could help, as I'm kind of a COM guy, but I don't do it that way. I use my own rather low level direct techniques, which wouldn't work for you unless you've devoted your life to COM, as I have :).

The person here who's knowledgable about using the #import directive with COM in C++ is Andy Weskin. Possibly others too, but I know Andy is real familiar with it. He might be busy and not have the time to help though. About the only help I can offer is the suggestion to learn how to use code tags. Nobody wants to look at unformatted code.
Last edited on
that that you'd put here doesnt looks like ole
real ole looks like that http://fullproject.googlecode.com/svn/trunk/code/SDK/WinAPI%20Programming/CHAP20/


Code from Petzold book



bool DomeSimulator::get_AtHome() const
.......
bool home = &DomeSimulator::get_AtHome

not bad


You just can try to invoke something like "microsoft rich textbox" to begin. For example

Last edited on
Topic archived. No new replies allowed.