Error from Com and CoCreateInstance  
Author Message
Jademobile





PostPosted: Visual C++ Language, Error from Com and CoCreateInstance Top

I would like to generate a DHTML control using ATL and VC2005.

I follow the instruction on "ATL Support for DHTML Controls " section in "ATL Library Reference " of MSDN, but I could not work it out.

I add a new class "mypage1" using wizard. In mypage1.h, m_spBrowser is declared. But the CoCreateInstance process is error.
I don't know the reason. There altogether report 5 errors:
1,d:\work\dhtml1\dhtml1\mypage1.h(203) : error C2327: 'Cmypage1::m_spBrowser' : is not a type name, static, or enumerator
2,d:\work\dhtml1\dhtml1\mypage1.h(203) : error C2065: 'm_spBrowser' : undeclared identifier
3,d:\work\dhtml1\dhtml1\mypage1.h(203) : error C4430: missing type specifier - int assumed. Note: C++ does not support
4,d:\work\dhtml1\dhtml1\mypage1.h(203) : error C2864: 'Cmypage1::hr' : only static const integral data members can be
Initialized within a class
5,d:\work\dhtml1\dhtml1\mypage1.cpp(32) : error C2227: left of '->Navigate' must point to class/struct/union/generic type
        type is ''unknown-type''

B.T.W:
1, Where can I find reference on CoCreateInstance from msdn of VS2005 I can't find it out
2, The first parameter of CoCreateInstance is class ID, where can I get it I tried to construct it by "CLS_"+name of class("mypage1")
3, How to file other parameters

mypage1.h:

// mypage1.h : Cmypage1
#pragma once
#include "resource.h"       //
#include <atlctl.h>
#include "dhtml1.h"
#include "_Imypage1Events_CP.h"


#ifdef _WIN32_WCE
#error "ATL Windows CE HTML "
#endif
class ATL_NO_VTABLE Cmypage1UI :
 public IDispatchImpl<Imypage1UI, &IID_Imypage1UI, &LIBID_dhtml1Lib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
 public CComObjectRootEx<CComSingleThreadModel>
{
BEGIN_COM_MAP(Cmypage1UI)
 COM_INTERFACE_ENTRY(Imypage1UI)
 COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
// Imypage1
public:
 DECLARE_PROTECT_FINAL_CONSTRUCT()

 HRESULT FinalConstruct()
 {
  return S_OK;
 }

 void FinalRelease()
 {
 }

 // HTML <BODY>
 STDMETHOD(OnClick)(IDispatch* pdispBody, VARIANT varColor)
 {
  CComQIPtr<IHTMLBodyElement> spBody(pdispBody);
  if (spBody != NULL)
   spBody->put_bgColor(varColor);
  return S_OK;
 }
public:
 STDMETHOD(HelloHTML)(void);
public:
 STDMETHOD(GoToURL)(void);
};

 

// Cmypage1
class ATL_NO_VTABLE Cmypage1 :
 public CComObjectRootEx<CComSingleThreadModel>,
 public CStockPropImpl<Cmypage1, Imypage1>,
 public IPersistStreamInitImpl<Cmypage1>,
 public IOleControlImpl<Cmypage1>,
 public IOleObjectImpl<Cmypage1>,
 public IOleInPlaceActiveObjectImpl<Cmypage1>,
 public IViewObjectExImpl<Cmypage1>,
 public IOleInPlaceObjectWindowlessImpl<Cmypage1>,
 public ISupportErrorInfo,
 public IConnectionPointContainerImpl<Cmypage1>,
 public CProxy_Imypage1Events<Cmypage1>,
 public IPersistStorageImpl<Cmypage1>,
 public ISpecifyPropertyPagesImpl<Cmypage1>,
 public IQuickActivateImpl<Cmypage1>,
#ifndef _WIN32_WCE
 public IDataObjectImpl<Cmypage1>,
#endif
 public IProvideClassInfo2Impl<&CLSID_mypage1, &__uuidof(_Imypage1Events), &LIBID_dhtml1Lib>,
#ifdef _WIN32_WCE // Windows CE , IObjectSafety
 public IObjectSafetyImpl<Cmypage1, INTERFACESAFE_FOR_UNTRUSTED_CALLER>,
#endif
 public CComCoClass<Cmypage1, &CLSID_mypage1>,
 public CComControl<Cmypage1>
{
public:


 Cmypage1()
 {
  m_bWindowOnly = TRUE;
 }

DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE |
 OLEMISC_CANTLINKINSIDE |
 OLEMISC_INSIDEOUT |
 OLEMISC_ACTIVATEWHENVISIBLE |
 OLEMISC_SETCLIENTSITEFIRST
)

DECLARE_REGISTRY_RESOURCEID(IDR_MYPAGE1)


BEGIN_COM_MAP(Cmypage1)
 COM_INTERFACE_ENTRY(Imypage1)
 COM_INTERFACE_ENTRY(IDispatch)
 COM_INTERFACE_ENTRY(IViewObjectEx)
 COM_INTERFACE_ENTRY(IViewObject2)
 COM_INTERFACE_ENTRY(IViewObject)
 COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
 COM_INTERFACE_ENTRY(IOleInPlaceObject)
 COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
 COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
 COM_INTERFACE_ENTRY(IOleControl)
 COM_INTERFACE_ENTRY(IOleObject)
 COM_INTERFACE_ENTRY(IPersistStreamInit)
 COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
 COM_INTERFACE_ENTRY(ISupportErrorInfo)
 COM_INTERFACE_ENTRY(IConnectionPointContainer)
 COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
 COM_INTERFACE_ENTRY(IQuickActivate)
 COM_INTERFACE_ENTRY(IPersistStorage)
#ifndef _WIN32_WCE
 COM_INTERFACE_ENTRY(IDataObject)
#endif
 COM_INTERFACE_ENTRY(IProvideClassInfo)
 COM_INTERFACE_ENTRY(IProvideClassInfo2)
#ifdef _WIN32_WCE // Windows CE , IObjectSafety
 COM_INTERFACE_ENTRY_IID(IID_IObjectSafety, IObjectSafety)
#endif
END_COM_MAP()

BEGIN_PROP_MAP(Cmypage1)
 PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
 PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
 PROP_ENTRY("AutoSize", DISPID_AUTOSIZE, CLSID_NULL)
 //
 // PROP_ENTRY("Property Description", dispid, clsid)
 // PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()

BEGIN_CONNECTION_POINT_MAP(Cmypage1)
 CONNECTION_POINT_ENTRY(__uuidof(_Imypage1Events))
END_CONNECTION_POINT_MAP()

BEGIN_MSG_MAP(Cmypage1)
 MESSAGE_HANDLER(WM_CREATE, OnCreate)
 CHAIN_MSG_MAP(CComControl<Cmypage1>)
 DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
// :
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

// ISupportsErrorInfo
 STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
 {
  static const IID* arr[] =
  {
   &IID_Imypage1,
  };

  for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
  {
   if (InlineIsEqualGUID(*arrIdea, riid))
    return S_OK;
  }
  return S_FALSE;
 }

// IViewObjectEx
 DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

// Imypage1
 void OnAutoSizeChanged()
 {
  ATLTRACE(_T("OnAutoSizeChanged\n"));
 }

 LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  CAxWindow wnd(m_hWnd);
  wnd.ModifyStyle(0, WS_HSCROLL | WS_VSCROLL);
  HRESULT hr = wnd.CreateControl(IDH_MYPAGE1);
  if (SUCCEEDED(hr))
  {
   CComObject<Cmypage1UI> *pObject = NULL;
   hr = CComObject<Cmypage1UI>::CreateInstance(&pObject);
   if (SUCCEEDED(hr) && pObject != NULL)
    hr = wnd.SetExternalDispatch(static_cast<Imypage1UI*>(pObject));
  }
  if (SUCCEEDED(hr))
   hr = wnd.QueryControl(IID_IWebBrowser2, (void**)&m_spBrowser);
  return SUCCEEDED(hr) 0 : -1;
 }

 STDMETHOD(TranslateAccelerator)(LPMSG pMsg)
 {
  CComPtr<IOleInPlaceActiveObject> spIOleInPlaceActiveObject;

  HRESULT hr = m_spBrowser->QueryInterface(&spIOleInPlaceActiveObject);
  if (SUCCEEDED(hr))
   hr = spIOleInPlaceActiveObject->TranslateAccelerator(pMsg);
  if (hr != S_OK)
   hr = IOleInPlaceActiveObjectImpl<Cmypage1>::TranslateAccelerator(pMsg);

  return hr;
 }

// Error comes from below:
 CComPtr<IWebBrowser2> m_spBrowser;
 hr = CoCreateInstance(CLSID_mypage1,NULL,CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2,(void  **)&m_spBrowser);
 //HRESULT hr = m_spBrowser.CoCreateInstance(CLSID_mypage1);
 //IUnknown *pUnknown;
 //pUnknown -> QueryInterface(&m_spBrowser);

 DECLARE_PROTECT_FINAL_CONSTRUCT()

 HRESULT FinalConstruct()
 {
  return S_OK;
 }

 void FinalRelease()
 {
 }
public:
 STDMETHOD(MethodInvoked)(void);
};

OBJECT_ENTRY_AUTO(__uuidof(mypage1), Cmypage1)

mypage1.cpp:

// mypage1.cpp : Cmypage1
#include "stdafx.h"
#include "mypage1.h"


// Cmypage1

STDMETHODIMP Cmypage1::MethodInvoked(void)
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState());

 // TODO:
 ::MessageBox(NULL, _T("I'm invoked"), _T("Your Container Message"), MB_OK);
 return S_OK;
}

STDMETHODIMP Cmypage1UI::HelloHTML(void)
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState());

 // TODO:
 ::MessageBox(NULL, _T("Here's your message"), _T("HelloHTML"), MB_OK);
 return S_OK;
}

STDMETHODIMP Cmypage1UI::GoToURL(void)
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState());

 // TODO:

//Error reported below: 

m_spBrowser->Navigate(CComBSTR("www.microsoft.com"), NULL, NULL, NULL, NULL);
 return S_OK;
}

 

IDL file:

// dhtml1.idl : dhtml1 IDL
//

// MIDL
// (dhtml1.tlb)

#include "olectl.h"
import "oaidl.idl";
import "ocidl.idl";

[
 object,
 uuid(80EC0F30-B738-4DA7-88C7-0AF356B8B80F),
 dual,
 nonextensible,
 helpstring("Imypage1 "),
 pointer_default(unique)
]
interface Imypage1 : IDispatch{
 [propput, bindable, requestedit, id(DISPID_AUTOSIZE)]
 HRESULT AutoSize([in]VARIANT_BOOL vbool);
 [propget, bindable, requestedit, id(DISPID_AUTOSIZE)]
 HRESULT AutoSize([out,retval]VARIANT_BOOL* pbool);
 [id(1), helpstring(" MethodInvoked")] HRESULT MethodInvoked(void);
};

[
 object,
 dual,
 nonextensible,
 uuid(28DBF874-F9F5-45B2-91D5-F36D92059F81),
 helpstring("Imypage1UI "),
 pointer_default(unique)
]
interface Imypage1UI : IDispatch
{
 // HTML
 HRESULT OnClick([in]IDispatch* pdispBody, [in]VARIANT varColor);
 [id(1), helpstring(" HelloHTML")] HRESULT HelloHTML(void);
 [id(2), helpstring(" GoToURL")] HRESULT GoToURL(void);
};
[
 uuid(421093AC-0A2E-4F4D-9C69-26DCC0D4F625),
 version(1.0),
 helpstring("dhtml1 1.0 ")
]
library dhtml1Lib
{
 importlib("stdole2.tlb");
 [
  uuid(B2CF24D1-3802-4898-8826-75BC276F4765),
  helpstring("_Imypage1Events ")
 ]
 dispinterface _Imypage1Events
 {
  properties:
  methods:
 };
 [
  uuid(B69683DE-9796-442F-84E3-A5343A769866),
  control,
  helpstring("mypage1 Class")
 ]
 coclass mypage1
 {
  [default] interface Imypage1;
  [default, source] dispinterface _Imypage1Events;
  interface Imypage1UI;
 };
};

 


 




Visual C++2