Wrapping C++ object with ATL
Hello,
I have been trying this on and off for sometime but still couldn;t figured out how to do it. Basically i have a few C++ classes as follow: - (simplified version)
Class GenericDevice{
public:
virtual long Open(LPCTSTR DeviceName);
virtual long Close();
private:
...
};
class DeviceA : public GenericDevice{
public:
virtual long Open(LPCTSTR DeviceName);
virtual long Close();
long Device_A_Specific(....);
}
I wanted to use ATL to wrap around this classes.
And i need to expose the following methods:
Open,Close, Device_A_Specific from ATL.
After I created an ATL project with VC++ 6.0, i added an atl object though the wizard, and i have the following:
class ATL_NO_VTABLE CSimpleObj :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CSimpleObj , &CLSID_SimpleObj>,
public IDispatchImpl<ISimpleObj, &IID_ISimpleObj, &LIBID_MYATLLib>
{
public:
.....
}
Where and how should i proceed now?? assuming i am using the ATL wizard again?
[1161 byte] By [
mce] at [2007-11-18 20:30:46]

# 1 Re: Wrapping C++ object with ATL
Make the GenericDevice class a template:
template <class T>
class GenericDevice
{
long Open(LPCTSTR DeviceName)
{
T* pT = static_cast<T*>(this);
return pT->Open(DeviceName);
}
long Close()
{
T* pT = static_cast<T*>(this);'
return pT->Close();
}
};
Now you can derive DeviceA from GenericDevice and pass the class name as template parameter:
class DeviceA: public GenericDevice<DeviceA>
All you have to do now is to overload the Open and Close functions.
I hope this helps, good luck.
# 3 Re: Wrapping C++ object with ATL
well i guess you mean this: - ?
class ATL_NO_VTABLE CSimpleObj :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CSimpleObj , &CLSID_SimpleObj>,
public IDispatchImpl<ISimpleObj, &IID_ISimpleObj,
public CGenericDevice
&LIBID_MYATLLib>
{
}
could you show me an example (skeleton will do for me) creating the COM wrapper functions?? using my C++ classes as example??
have done many ATL, but when come to putting C++ into ATL, i am totally lost. do you need the wrapper for every C++ classes or just the CGenericDevice class??
mce at 2007-11-11 1:14:41 >

# 4 Re: Wrapping C++ object with ATL
Yep thats what I mean. You just create the function's using the function wizard which will update the .idl..h and .cpp files appropriately.
and then in the .cpp file you just transfer the call to your base class,
CGenericDevice::Open(...) etc.
fransn at 2007-11-11 1:15:40 >
