In ASP development, we often use a call like this ousername=request ("UserName"). To be sure, what we need to get is a string value, in fact, we also use Ousername as a character value.
As a complete invocation should be this: Ousername=cstr (Request.item ("UserName"))
Next we see how the Request object is using COM automation technology to support calls that are shaped like request ("UserName").
By looking at the type library, it is known that the request object derives from the interface Irequest,irequest provides the following read-only property declaration.
[ID (00000000), propget]
HRESULT Item (
[In] BSTR Bstrvar,
[Out, retval] idispatch** Ppobjreturn);
The read-only property of item has a DISPID of 0, indicating that it is a default property that does not have to be explicitly written out when invoked through a script. The passed-in parameter of the property is a string, and the outgoing value is a idispatch* interface pointer. Originally, the above obtained ousername is actually a IDispatch interface, is not a string value, just in the subsequent call will automatically implement the IDispatch interface to the string conversion, so on the surface we can directly take it as a string to see.
The following question is how the IDispatch interface will be automatically converted. In automation programming, conversions between various types of automation are performed primarily through Variantchangetype and Variantchangetypeex.
The following is the declaration of the function Variantchangetypeex
HRESULT Variantchangetypeex (
Variantarg * Pvargdest,
Variantarg * PVARSRC,
LCID LCID,
unsigned short wflags,
VARTYPE VT
);
This is an explanation of the function in MSDN.
An object was coerced to a value by invoking the object's Value property (Dispid_value).
This object is cast to a value by calling the object's Value property (the property ID is dispid_value).
Where Dispid_value is 0,variantchangetype in the conversion, if found PVARSRC is a IDispatch interface, will automatically call the DispID 0 property method, This property method is thus used to implement the conversion of the IDispatch interface to other values concretely.
The IDispatch interface is specifically converted into what type of value, determined by the property method of DispID 0.
Let's write a simple example:
Define an object CTest, implement interface Itest,itest and derive from Idispatch;itest define a property item
The first step: Mr. Cheng interface ITest and Object CTest
Interface Itest:idispatch {
[ID (00000000), propget]
HRESULT Item (
[Out, retval] variant* Pvariantreturn);
};
Class atl_no_vtable CTest:
Public CComObjectRootEx,
Public CComCoClass,
Public IDispatchImpl
{
Public
........................
Public
ITest Methods
Public
STDMETHOD (get_item) (VARIANT * Pvariantreturn)
{
CComVariant Pvresult=l "This is a test";
Pvresult.detach (Pvariantreturn);
return S_OK;
}
Step Two:
In other objects to be implemented
STDMETHOD (get_item) (BSTR bstrvar, lpdispatch * ppobjreturn)
{
CComObject *ptest;
Ccomobject::createinstance (&ptest);
Return Ptest->queryinterface (IID_IDispatch, (void**) ppobjreturn);
}