Click here to Skip to main content
15,913,487 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
Generalbstr, wchar_t, and code pages Pin
Samsung15-Jan-05 7:05
Samsung15-Jan-05 7:05 
GeneralSTL error Pin
mansour_ahmadian15-Jan-05 1:30
mansour_ahmadian15-Jan-05 1:30 
GeneralRe: STL error Pin
Michael Dunn15-Jan-05 10:29
sitebuilderMichael Dunn15-Jan-05 10:29 
GeneralCustomising Connection Tab in data link dialog Box Pin
Member 1266497613-Jan-05 20:44
Member 1266497613-Jan-05 20:44 
GeneralOnSize : Positioning Static Controls Pin
tnguyen44412-Jan-05 12:40
tnguyen44412-Jan-05 12:40 
GeneralRe: OnSize : Positioning Static Controls Pin
SteveKing14-Jan-05 1:04
SteveKing14-Jan-05 1:04 
GeneralAccessing IwebBrowser2 outside toolbar class Pin
tnguyen44412-Jan-05 12:11
tnguyen44412-Jan-05 12:11 
GeneralSorry! ATL : get_, put_, CString and BSTR Pin
Malcolm Smart11-Jan-05 9:34
Malcolm Smart11-Jan-05 9:34 
I have looked into this - honest and really beating my head against a wall. This is one of my first forays into COM and ATL and can't get over the first hurdle. Using the ATL wizard, I created a ATL COM Server, added a Simple COM object, and then added a single property with get and put. It should simply set and get a string - in this case - _firstname. COM class below, with the IDL snippet and implementation for the property get/put methods...

I eventually 'got' the property into my MFC dialog but can't set it. That follows the COM code...

class ATL_NO_VTABLE CFamily : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CFamily, &CLSID_Family>,
	public IDispatchImpl<IFamily, &IID_IFamily, &LIBID_TESTCOM_V6Lib>
{
public:
	CFamily()
	{
	_firstname = "Angel";
	}

DECLARE_REGISTRY_RESOURCEID(IDR_FAMILY)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CFamily)
	COM_INTERFACE_ENTRY(IFamily)
	COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()

// IFamily
public:
	STDMETHOD(get_FirstName)(/*[out, retval]*/ BSTR *pVal);
	STDMETHOD(put_FirstName)(/*[in]*/ BSTR newVal);
private:
	CComBSTR _firstname;//this and the assignment in the constructor is the only change I made
};

// get put stuff...

STDMETHODIMP CFamily::get_FirstName(BSTR *pVal)
{
	*pVal = _firstname.Copy();
	return S_OK;
}

STDMETHODIMP CFamily::put_FirstName(BSTR newVal)
{
	_firstname.m_str = newVal;
	return S_OK;
}


And now the MFC dialog stuff....I've #IMPORTed the library so I can use the smart pointers...

#import "../debug/TestCOM_V6.dll" no_namespace

//dialog code here....

void CComTesterDlg::OnSetFirstName() 
{
	UpdateData();
	::CoInitialize(NULL);
	IFamilyPtr first;

	HRESULT hr = first.CreateInstance(__uuidof(Family));
	if (SUCCEEDED(hr))	
	first->PutFirstName(m_FirstName.AllocSysString());

	first = NULL;
	::CoUninitialize();
	
	
}

void CComTesterDlg::OnShowFirstName() 
{
	::CoInitialize(NULL);
	IFamilyPtr first;

	HRESULT hr = first.CreateInstance(__uuidof(Family));
	if (SUCCEEDED(hr))	
		m_ShowFirstName  = (char*)(first->GetFirstName());

//	::CoUninitialize();	

	UpdateData(FALSE);
}


The OnShowFirstName retrieves the string from the COM but I can't set it with OnSetFirstName. The CreateInstances both work as I've stepped through it. I appreciate you should initialse the COM object once, but this is just a test harness. Also, (in for a penny(cent) in for a pound(dollar) ), if I uncomment the ::CoUninitialize in the OnShowFirstName the app bombs out with a 'read error'.

So

1) Why is the CComBSTR _firstname not being set in my COM object when I call OnSetFirstName?
2) Is my get implementation the best method of doing this?
3) Why does the ::CoUninitialize crash on the second method but not the first?
4) I am really sorry for these 'hopefully trivial' questions...

Regards

Angel.

*********************************************
The sooner you fall behind, the longer you have to catch up.
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Martin Koorts12-Jan-05 1:19
Martin Koorts12-Jan-05 1:19 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Malcolm Smart12-Jan-05 3:14
Malcolm Smart12-Jan-05 3:14 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Martin Koorts12-Jan-05 4:36
Martin Koorts12-Jan-05 4:36 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Malcolm Smart12-Jan-05 5:10
Malcolm Smart12-Jan-05 5:10 
GeneralRe: Sorry! ATL : get_, put_, CString and BSTR Pin
Martin Koorts12-Jan-05 5:17
Martin Koorts12-Jan-05 5:17 
GeneralAdd Event for ATL based ActiveX Control Pin
greekgoddj10-Jan-05 21:43
greekgoddj10-Jan-05 21:43 
GeneralGDI+ Drawing shape on usercontrol shifts form up Pin
abcxyz8210-Jan-05 6:23
abcxyz8210-Jan-05 6:23 
Question&amp;#xxx to character? Pin
Samsung10-Jan-05 2:30
Samsung10-Jan-05 2:30 
Generalie toolbar to block sites based on domain/ip address of the domain Pin
mr_mark_hewitt10-Jan-05 0:28
mr_mark_hewitt10-Jan-05 0:28 
GeneralRe: ie toolbar to block sites based on domain/ip address of the domain Pin
Martin Koorts10-Jan-05 3:49
Martin Koorts10-Jan-05 3:49 
GeneralRe: ie toolbar to block sites based on domain/ip address of the domain Pin
mr_mark_hewitt10-Jan-05 4:40
mr_mark_hewitt10-Jan-05 4:40 
GeneralRe: ie toolbar to block sites based on domain/ip address of the domain Pin
Martin Koorts10-Jan-05 5:27
Martin Koorts10-Jan-05 5:27 
Generalatomic clock and time zone programming Pin
mr_mark_hewitt9-Jan-05 20:32
mr_mark_hewitt9-Jan-05 20:32 
GeneralRe: atomic clock and time zone programming Pin
Martin Koorts9-Jan-05 23:04
Martin Koorts9-Jan-05 23:04 
GeneralDynamic Menu, WTL Pin
Johnny1239-Jan-05 19:41
Johnny1239-Jan-05 19:41 
GeneralChanging Button Style With WTL Pin
Johnny1238-Jan-05 20:28
Johnny1238-Jan-05 20:28 
GeneralRe: Changing Button Style With WTL Pin
Michael Dunn9-Jan-05 4:00
sitebuilderMichael Dunn9-Jan-05 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.