Casting
-
I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney
-
I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney
Yes, but it is not safe. Since you are in your CUser class when you create the new object, it would be safer to call new, then cast the object to the interface if the interface needed to be exported from the function.
HRESULT CUsers::SomeFunc()
{
CUser pUser = new CUser;
pUser->m_SomeData;
...
CComPtr piUser
piUser = static_cast(pUser);
...
}Good Luck
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life! -
I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney
Justin Turney wrote: That was my question, is the cast possible? Why do you want to do that? You should never do anything like that. If a proxy or stub gets involved in between then the code will not be safe.
-
I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney
-
I have an out-proc server i'm currently writing. In this server I have a number of interfaces. I was wondering if I could do the following. All the interfaces and implementations of the interfaces are in the same project. interface IUser {....}; // IUser definition interface IUsers {....}; // IUsers definitions class CUser : public IUser {....}; // The implementation of IUser class CUsers : public IUsers {...}; // The implementation of IUsers HRESULT CUsers::SomeFunc() { CComPtr pUser; pUser.CreateInstance(__uuidof(IUser)); ((CUser*)(pUser.p))->m_SomeData; // Can I do this cast from IUser to CUser? ... } That was my question, is the cast possible? Justin Turney
pUser.CreateInstance(__uuidof(IUser));
__uuidof(IUser) is the UUID of IUser interface, not the his coclass CLSID. So this call will be unsuccessful. You can use the local creation of COM object:
CComObject < CUser >* pUser;
HRESULT hr = pUser->CreateInstance( & pUser );
if( SUCCEEDED(hr) )
{
pUser->m_SomeData;
...
delete pUser;
}With best wishes, Vita