1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// The remote machine
CString strRemoteMachine;
strRemoteMachine = "198.256.254.45";
// array of interfaces we will query for each server
MULTI_QI m_arrMultiQI [7];
// Re-intialize Multi-Query Interface:
for (int i = 0; i < sizeof (m_arrMultiQI) / sizeof (MULTI_QI); i++)
{
m_arrMultiQI [i].pItf = NULL;
m_arrMultiQI [i].hr = 0;
}
// Load up the Interface ID's we hope to get pointers for when we
// call CoCreateInstanceEx():
m_arrMultiQI [0].pIID = &IID_IOPCServer;
m_arrMultiQI [1].pIID = &IID_IOPCCommon;
m_arrMultiQI [2].pIID = &IID_IConnectionPointContainer;
m_arrMultiQI [3].pIID = &IID_IOPCItemProperties;
m_arrMultiQI [4].pIID = &IID_IOPCBrowseServerAddressSpace;
m_arrMultiQI [5].pIID = &IID_IOPCServerPublicGroups;
m_arrMultiQI [6].pIID = &IID_IPersistFile;
// First we need to initialize a server info structure:
COSERVERINFO tCoServerInfo;
ZeroMemory (&tCoServerInfo, sizeof (tCoServerInfo));
// Allocate memory for the machine name string:
int nSize = strRemoteMachine.GetLength () * sizeof (WCHAR);
tCoServerInfo.pwszName = new WCHAR [nSize];
// Copy the machine name string into the server info structure:
#ifdef _UNICODE
// For Unicode builds, the contents of m_strRemoteMachine will
// already be in wide character format, as demanded by COM, so
// copy it as is.
lstrcpyn (tCoServerInfo.pwszName, strRemoteMachine, nSize);
#else
// For ANSI builds, the contents of m_strRemoteMachine will not
// be in wide character format, as demanded by COM, so we need
// to reformat:
mbstowcs (tCoServerInfo.pwszName, strRemoteMachine, nSize);
#endif//_UNICODE
// CoCreateInstanceEx will launch the OPC Server if necessary, and
// call its QueryInterface for us (bumping its reference count):
hr = CoCreateInstanceEx (
CLSID_StdComponentCategoriesMgr, // CLSID
NULL, // No aggregation
CLSCTX_REMOTE_SERVER, // remote servers
&tCoServerInfo, // remote machine name
sizeof (m_arrMultiQI) / sizeof (MULTI_QI), // number of IIDS to query
m_arrMultiQI); // array of IID pointers to query
// COM requires us to free memory allocated for [out] and [in/out]
// arguments (i.e. name string).
delete [] tCoServerInfo.pwszName;
switch(hr) {
case S_OK : TRACE("S_OK"); break;
case REGDB_E_CLASSNOTREG : TRACE("REGDB_E_CLASSNOTREG"); break;
case CLASS_E_NOAGGREGATION : TRACE("CLASS_E_NOAGGREGATION"); break;
case CO_S_NOTALLINTERFACES : TRACE("CO_S_NOTALLINTERFACES"); break;
case E_NOINTERFACE : TRACE("E_NOINTERFACE "); break;
} |
Partager