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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
//
// DClient.cpp - Dispatch client implementation
//
// This client connects to the IX dual interface
// through the dispinterface.
//
#include <windows.h>
#include "Util.h"
static inline void trace(char* msg)
{ Util::Trace("DClient", msg, S_OK) ;}
static inline void trace(char* msg, HRESULT hr)
{ Util::Trace("DClient", msg, hr) ;}
void test(wchar_t * progid)
{
DWORD clsctx ;
clsctx = CLSCTX_INPROC_SERVER ;
trace("Attempt to create in-proc component.") ;
// Get the CLSID for the application.
CLSID clsid ;
HRESULT hr = ::CLSIDFromProgID(progid, &clsid) ;
if(FAILED(hr))
{
trace("Failed to get CLSID.", hr) ;
return ;
}
// Create the component.
IDispatch* pIDispatch = NULL ;
hr = ::CoCreateInstance(clsid,
NULL,
clsctx,
IID_IDispatch,
(void**)&pIDispatch) ;
if (FAILED(hr))
{
trace("Create instance failed.", hr) ;
OleUninitialize() ;
return ;
}
trace("CoCreateInstance succeeded.") ;
// First we need to get the IDs for the function names.
trace("Get DispID for function \"Fx\".") ;
DISPID dispid ;
OLECHAR* name = L"Fx" ;
hr = pIDispatch->GetIDsOfNames(IID_NULL,
&name,
1,
GetUserDefaultLCID(),
&dispid) ;
if (FAILED(hr))
{
trace("Query GetIDsOfNames failed.", hr) ;
pIDispatch->Release() ;
return ;
}
// Prepare the arguments for Fx.
DISPPARAMS dispparamsNoArgs = {
NULL,
NULL,
0, // Zero arguments
0 // Zero named arguments
} ;
trace("Invoke the function \"Fx\".") ;
hr = pIDispatch->Invoke(dispid,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_METHOD,
&dispparamsNoArgs,
NULL,
NULL,
NULL) ;
if (FAILED(hr))
{
trace("Invoke call failed.", hr) ;
pIDispatch->Release() ;
return ;
}
//
// Now pass a BSTR to the component.
//
trace("Get DispID for function \"FxStringIn\".") ;
name = L"FxStringIn" ;
hr = pIDispatch->GetIDsOfNames(IID_NULL,
&name,
1,
GetUserDefaultLCID(),
&dispid) ;
if (FAILED(hr))
{
trace("Query GetIDsOfNames failed.", hr) ;
pIDispatch->Release() ;
return ;
}
// Pass the following string to the component.
wchar_t wszIn[] = L"This is the test." ;
// Convert the wide-character string to a BSTR.
BSTR bstrIn ;
bstrIn = ::SysAllocString(wszIn) ;
// Build up the parameters for the invoke call.
// Allocate and initialize a VARIANT argument.
VARIANTARG varg ;
::VariantInit(&varg) ; // Initialize the VARIANT.
varg.vt = VT_BSTR ; // Type of VARIANT data
varg.bstrVal = bstrIn ; // Data for the VARIANT
// Fill in the DISPPARAMS structure.
DISPPARAMS param ;
param.cArgs = 1 ; // Number of arguments
param.rgvarg = &varg ; // Arguments
param.cNamedArgs = 0 ; // Number of named args
param.rgdispidNamedArgs = NULL ; // Named arguments
trace("Invoke the function \"FxStringIn\".") ;
hr = pIDispatch->Invoke(dispid,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_METHOD,
¶m,
NULL,
NULL,
NULL) ;
if (FAILED(hr))
{
trace("Invoke call failed.", hr) ;
pIDispatch->Release() ;
return ;
}
// Clean up
::SysFreeString(bstrIn) ;
//
// Now get a BSTR back from the component.
//
// Get the dispid.
trace("Get DispID for function \"FxStringOut\".") ;
name = L"FxStringOut" ;
hr = pIDispatch->GetIDsOfNames(IID_NULL,
&name,
1,
GetUserDefaultLCID(),
&dispid) ;
if (FAILED(hr))
{
trace("Query GetIDsOfNames failed.", hr) ;
pIDispatch->Release() ;
return ;
}
// Allocate a variant for the returned parameter.
VARIANT varResult ;
::VariantInit(&varResult) ;
// Invoke the function.
trace("Invoke the function \"FxStringOut\".") ;
hr = pIDispatch->Invoke(dispid,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_METHOD,
&dispparamsNoArgs, //¶m,
&varResult,
NULL,
NULL) ;
if (FAILED(hr))
{
trace("Invoke call failed.", hr) ;
pIDispatch->Release() ;
return ;
}
// Display the returned string.
if (varResult.vt == VT_BSTR)
{
strstream sout ;
sout << "String returned from component: "
<< varResult.bstrVal
<< ends ;
trace(sout.str()) ;
// Free the string.
::SysFreeString(varResult.bstrVal) ;
}
//
// Show how to handle a function which returns an EXCEPINFO.
//
trace("Get DispID for function \"FxFakeError\"") ;
name = L"FxFakeError" ;
hr = pIDispatch->GetIDsOfNames(IID_NULL,
&name,
1,
GetUserDefaultLCID(),
&dispid) ;
if (FAILED(hr))
{
trace("Query GetIDsOfNames failed.", hr) ;
pIDispatch->Release() ;
return ;
}
EXCEPINFO excepinfo ;
trace("Invoke the function \"FxFakeError\".") ;
hr = pIDispatch->Invoke(dispid,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_METHOD,
&dispparamsNoArgs,
NULL,
&excepinfo,
NULL) ;
if (FAILED(hr))
{
trace("FxFakeError failed.", hr) ;
if (hr == DISP_E_EXCEPTION)
{
trace("We have error information from the component.") ;
if (excepinfo.pfnDeferredFillIn != NULL)
{
(*(excepinfo.pfnDeferredFillIn))(&excepinfo) ;
}
strstream sout ;
sout << "Information from component: "
<< excepinfo.bstrSource
<< "/"
<< excepinfo.bstrDescription
<< ends ;
trace(sout.str()) ;
}
}
// Release the dispatch interface.
pIDispatch->Release() ;
}
int main()
{
HRESULT hr = OleInitialize(NULL) ;
if (FAILED(hr))
{
trace("Failed to initialize.", hr) ;
return 1 ;
}
test(L"InsideCOM.Chap11");
test(L"CAMLIDL.Testcomp2");
// Uninitialize the OLE library.
OleUninitialize() ;
return 0 ;
}
|