ole automation 主講人:虞台文. content bstr string data type in ole variant & variantarg...

Post on 17-Jan-2018

241 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

OLE Automation BSTR  String data type in OLE

TRANSCRIPT

OLE Automation

主講人:虞台文

Content BSTR String data type in OLE VARIANT & VARIANTARG IDispatch Create COM Objects Using ATL

OLE Automation

BSTR String data type in OLE

BSTR A BSTR is a Basic STRing in which the string is stored as a DWORD count of characters followed by the characters themselves

char count characters \0

Number of bytes equal to the count

LPTSTR psz

BSTR APIs

String Manipulation Functions

Descriptions

SysAllocString Creates and initializes a string.

SysAllocStringByteLen Creates a zero-terminated string of a specified length (32-bit only).

SysAllocStringLen Creates a string of a specified length.

SysFreeString Frees a previously created string.

SysReAllocString Changes the size and value of a string.

SysReAllocStringLen Changes the size of an existing string.

SysStringByteLen Returns the length of a string in bytes (32-bit only).

SysStringLen Returns the length of a string.

BSTR in MFC

BSTR CString::AllocSysString ( ) const;

throw( CMemoryException );

BSTR CString::SetSysString ( BSTR* pbstr ) const;

OLE AutomationVARIANT

&VARIANTARG

VARIANT & VARIANTARG

struct tagVARIANT { VARTYPE vt; WORD wReserved1; WORD wReserved2; WORD wReserved3; union { // C++ Type Union Name Type Tag Basic Type // -------- ---------- -------- ---------- long lVal; // VT_I4 ByVal Long unsigned char bVal; // VT_UI1 ByVal Byte short iVal; // VT_I2 ByVal Integer float fltVal; // VT_R4 ByVal Single double dblVal; // VT_R8 ByVal Double VARIANT_BOOL boolVal; // VT_BOOL ByVal Boolean SCODE scode; // VT_ERROR CY cyVal; // VT_CY ByVal Currency DATE date; // VT_DATE ByVal Date BSTR bstrVal; // VT_BSTR ByVal String IUnknown *punkVal; // VT_UNKNOWN IDispatch *pdispVal; // VT_DISPATCH ByVal Object SAFEARRAY *parray; // VT_ARRAY|* ByVal array // A bunch of other types that don't matter here... VARIANT *pvarVal; // VT_BYREF|VT_VARIANT ByRef Variant void * byref; // Generic ByRef };};

The VARIANT System Functions

Variant Manipulation Functions Descriptions

VariantChangeType Converts a variant to another type.

VariantChangeTypeEx Converts a variant to another type, using a locale identifier (LCID).

VariantClear Releases resources and sets a variant to VT_EMPTY.

VariantCopy Copies a variant.

VariantCopyInd Copies variants that may contain a pointer.

VariantInit Initializes a variant.

Data Type Conversion APIs

HRESULT VariantChangeType( VARIANTARG *pvargDest, VARIANTARG *pvarSrc, unsigned short wFlags, VARTYPE vt );

HRESULT VariantChangeTypeEx( VARIANTARG *pvargDest, VARIANTARG *pvarSrc, LCID lcid, unsigned short wFlags, VARTYPE vt);

Data Type Conversion APIs

VARIANT in MFCCOleVariant::COleVariantCOleVariant( );COleVariant( const VARIANT& varSrc );COleVariant( const COleVariant& varSrc );COleVariant( LPCVARIANT pSrc );COleVariant( LPCTSTR lpszSrc );COleVariant( LPCTSTR lpszSrc, VARTYPE vtSrc );COleVariant( CString& strSrc );COleVariant( BYTE nSrc );COleVariant( short nSrc, VARTYPE vtSrc = VT_I2 );COleVariant( long lSrc, VARTYPE vtSrc = VT_I4 );COleVariant( const COleCurrency& curSrc );COleVariant( float fltSrc );COleVariant( double dblSrc );COleVariant( const COleDateTime& dateSrc );COleVariant( const CByteArray& arrSrc );COleVariant( const CLongBinary& lbSrc );

OLE Automation

IDispatch

IDispatchinterface IDispatch : public IUnknown{ virtual ULONG GetTypeInfoCount(unsigned int FAR* pctinfo) = 0; virtual HRESULT GetTypeInfo(unsigned int iTInfo, LCID lcid, ITypeInfo FAR* FAR* ppTInfo ) = 0; virtual ULONG GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgDispId ) = 0; virtual ULONG Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR* pDispParams, VARIANT FAR* pVarResult, EXCEPINFO FAR* pExcepInfo, unsigned int FAR* puArgErr ) = 0;};

Creation of Dispatch API Functions

Functions DescriptionCreateDispTypeInfo Creates simplified type information for an object.

CreateStdDispatch Creates a standard IDispatch implementation for an object.DispGetIDsOfNames Converts a set of names to DISPIDs.DispGetParam Retrieves and coerces elements from a DISPPARAMS structure.

DispInvoke Calls a member function of an IDispatch interface.

OLE Automation

Create COM Objects Using ATL

Step 1: Running the ATL COM Wizard

Step 1: Running the ATL COM Wizard

Step 1: Running the ATL COM Wizard

Step 1: Running the ATL COM Wizard

Practice and view the files generated by the wizard.

Step 2: Creating a new ATL object

Step 2: Creating a new ATL object

Step 2: Creating a new ATL object

Step 2: Creating a new ATL object

Step 2: Creating a new ATL object

Step 2: Creating a new ATL object

Practice and view the files generated by the wizard.

Step 3: Adding a method

Step 3: Adding a method

Step 3: Adding a method

Step 3: Adding a method

Step 3: Adding a method

STDMETHODIMP CFirst_ATL::AddNumbers(long Num1, long Num2, long *ReturnVal){ // TODO: Add your implementation code here *ReturnVal = Num1 + Num2;

return S_OK;}

Step 4: Compiling the DLL

Step 5: Testing the COM Server with Visual Basic

Step 5: Testing the COM Server with Visual Basic

Step 5: Testing the COM Server with Visual Basic

Step 5: Testing the COM Server with Visual Basic

Step 5: Testing the COM Server with Visual Basic

Step 5: Testing the COM Server with Visual Basic

Step 6: Testing the COM Server with Visual C++

View Source Code

Exercises

1. Implement inprocess sever of CalculatorMemory via ATL.

2. Implement CalculatorMemory’s clients using VB and VC.

top related