Instantiating _com_ptr_t objects remotely | CodeGuru

Instantiating _com_ptr_t objects remotely

. I was going along, cheerfully using COM with Microsoft’s smart pointers when I discovered a glaring omission in the underlying _com_ptr_t code. It turns out that there is no way to use CreateInstance() to instantiate an object remotely. And there is no equivalent member function to CreateInstanceEx(). After a bit of digging around I […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 25, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


.


I was going along, cheerfully using COM with Microsoft

s smart pointers when I discovered a glaring omission in the underlying _com_ptr_t

code. It turns out that there is no way to use CreateInstance() to instantiate an object remotely. And there is no equivalent member function to CreateInstanceEx().


After a bit of digging around I ended up writing this (partially tested) macro that I believe allows remote or local instantiation of Microsoft’s smart COM pointers.


Normally you create an COM instances using Microsoft’s Smart Pointers as follows:


m_pMyCOMObj.CreateIntance(CLSID_MyCOMObj);
 

When using this macro, replace the previous code with the following:


HRESULT hr = CreateInstanceEx<IIMyCOMObjPtr>(m_pMyCOMObj, CLSID_MyCOMObj, _T(“RemoteHostname”));

This should allow you to connect to your remote host simply by specifying the hostname or IP address in the 3rd argument. Don’t forget to check the return value for success.


The code follows:


// This template is used to invoke either local or

remote

instances of
// C++ builtin smart COM objects.
template <typename ObjPtr>
HRESULT CreateInstanceEx(ObjPtr& output, const CLSID& rclsid,
const CString& hostname = _T(“”), IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
{
USES_CONVERSION;
HRESULT hr;

if (!hostname.IsEmpty()) {
// Create Distributed Object

// Declare some locals
IUnknown* pIUnknown;
COSERVERINFO* pServerInfo = NULL;
MULTI_QI rgmqResults;
COSERVERINFO ServerInfo;

// Zero structures
memset(&rgmqResults, 0, sizeof(rgmqResults));
memset(&ServerInfo, 0, sizeof(ServerInfo));

// Initialize data items
dwClsContext = CLSCTX_REMOTE_SERVER;
ServerInfo.pwszName = T2W(hostname);
pServerInfo = &ServerInfo;
rgmqResults.pIID = &IID_IUnknown;

// Create the IUnknown object for the remote object object
hr = CoCreateInstanceEx(rclsid, pOuter, dwClsContext, pServerInfo, 1, &rgmqResults);

// If successful, then get the internal interface we really wanted.
if (SUCCEEDED(hr)) {
pIUnknown = rgmqResults.pItf;
if (SUCCEEDED(hr)) {
hr = pIUnknown->QueryInterface(ObjPtr::GetIID(), reinterpret_cast<void**>(&output));
}
pIUnknown->Release(); // IUnknown has served it’s purpose so nuke it.
}
} else {
// It’s not remote so use the simpler call.
hr = CoCreateInstance(rclsid, pOuter, dwClsContext, ObjPtr::GetIID(), reinterpret_cast<void**>(&output));
}

return hr;
}

 


Caveats


Don’t forget to include _WIN32_DCOM in the preprocessor definitions of your project or you get a syntax error telling you that CoCreateInstanceEx() is not defined. You also need to make sure that OleInit() has been called prior to using the CreateInstanceEx() macro.


Last Modified: October 22, 1998

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.