Contents Index Topic Contents | ||
Previous Topic: IDispatch Methods Next Topic: IProvideMultipleClassInfo |
Examples
This JScript code in the function test() does the following:
- Create a new object by calling the "Object" constructor and assign a pointer to the new object to the variable "Obj".
- Create a new element ("Elem") in the object and assign to this element a pointer to the function "foo".
- Call this function. Since it is being called as a method, the "this" pointer refers to the object "Obj". The function adds a new element "Bar" to the object.
<HTML> <BODY> <SCRIPT LANGUAGE="JScript"> function foo() { // Create new element and assign the value 10 this.Bar = 10; } function test() { // Construct new object Obj = new Object(); // Create new element and assign function pointer Obj.Elem = foo; // Call Elem method ("this" == Obj) Obj.Elem(); // Obj.Bar now exists } test(); </SCRIPT> </BODY> </HTML>A control placed on this same web page could obtain from the browser a dispatch pointer to the script engines. The control could then implement the function test():
<HTML> <BODY> <SCRIPT LANGUAGE="JScript"> function foo() { // Create new element and assign the value 10 this.Bar = 10; } </SCRIPT> <OBJECT ID="test" <CLASSID="CLSID:9417DB5D-FA2A-11D0-8CB3-00C04FC2B085">> </OBJECT> </BODY> </HTML>Code from the control "test" that does the same thing as the JScript function test(). Note that these dispatch calls are made into the 'running' JScript engine and change the state of the engine:
- Obtain the dispatch pointer to the "foo" function using GetDispID().
- Obtain the dispatch pointer to the "Object" function using GetDispID().
- Construct an object by calling the "Object" function with InvokeEx() and obtain a dispatch pointer to the newly constructed object.
- Create a new element "Elem" in the object using GetDispID() with the fdexNameEnsure flag.
- Put the dispatch pointer to "foo" in the element using InvokeEx().
- Call the dispatch pointer to "foo" as a method by calling InvokeEx() and passing in the dispatch pointer to the constructed object as the "this" pointer.
- The "foo" method creates a new element "Bar" on the current "this" object.
- Verify that the new element "Bar" was created in the constructed object by enumerating through the elements using GetNextDispID().
BOOL test(IDispatchEx *pdexScript) { HRESULT hr; VARIANT var; BSTR bstrName; DISPID dispid, putid; IDispatchEx *pdexObj; IDispatch *pdispObj, *pdispFoo; DISPPARAMS dispparams, dispparamsNoArgs = {NULL, NULL, 0, 0}; // Get dispatch pointer for "foo" bstrName = SysAllocString(OLESTR("foo")); pdexScript->GetDispID(bstrName, 0, &dispid); SysFreeString(bstrName); pdexScript->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispparamsNoArgs, &var, NULL, NULL); pdispFoo = var.pdispVal; // Create object by calling "Object" constructor bstrName = SysAllocString(OLESTR("Object")); pdexScript->GetDispID(bstrName, 0, &dispid); SysFreeString(bstrName); pdexScript->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_CONSTRUCT, &dispparamsNoArgs, &var, NULL, NULL); pdispObj = var.pdispVal; pdispObj->QueryInterface(IID_IDispatchEx, (void **)&pdexObj); // Create new element in object bstrName = SysAllocString(OLESTR("Elem")); pdexObj->GetDispID(bstrName, fdexNameEnsure, &dispid); SysFreeString(bstrName); // Assign "foo" dispatch pointer to element putid = DISPID_PROPERTYPUT; var.vt = VT_DISPATCH; var.pdispVal = pdispFoo; dispparams.rgvarg = &var; dispparams.rgdispidNamedArgs = &putid; dispparams.cArgs = 1; dispparams.cNamedArgs = 1; pdexObj->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUTREF, &dispparams, NULL, NULL, NULL); // Invoke method with "this" pointer putid = DISPID_THIS; var.vt = VT_DISPATCH; var.pdispVal = pdispObj; dispparams.rgvarg = &var; dispparams.rgdispidNamedArgs = &putid; dispparams.cArgs = 1; dispparams.cNamedArgs = 1; pdexObj->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparams, NULL, NULL, NULL); // Confirm that new element "Bar" is in object hr = pdexObj->GetNextDispID(fdexEnumAll, DISPID_STARTENUM, &dispid); while (hr != S_FALSE) { pdexObj->GetMemberName(dispid, &bstrName); if (!wcscmp(bstrName, OLESTR("Bar"))) return TRUE; SysFreeString(bstrName); hr = pdexObj->GetNextDispID(fdexEnumAll, dispid, &dispid); } return FALSE; }
Top of Page
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.