Interface Definition Language

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers Applications
AP 01/01 Interface Definition Language Types are problematic: –Weakly typed C language: short / int / long –Architecture-specific data types IDL is a strongly.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Ni.com Understanding COM/ActiveX Jeff Paulter Staff Software Engineer Thurs Aug 17 10:15-11:30 a.m., 1:45-3:00 p.m. Ash (10A) Jeff Paulter Staff Software.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
OLE Automation 主講人:虞台文. Content BSTR  String data type in OLE VARIANT & VARIANTARG IDispatch Create COM Objects Using ATL.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Dynamic Allocation in C
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
C++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
Chapter 7 Pointers and C-Strings
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Jim Fawcett CSE775 – Distributed Objects Spring 2009
APPENDIX a WRITING SUBROUTINES IN C
Templates.
Motivation and Overview
Java Primer 1: Types, Classes and Operators
Native / Managed Interop
C Basics.
Programmazione I a.a. 2017/2018.
8 Pointers.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Dynamic Memory Allocation
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
An Introduction to Java – Part I, language basics
Lecture 4: RPC Remote Procedure Call Coulouris et al: Chapter 5
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Lecture 4: RPC Remote Procedure Call CDK: Chapter 5
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Objects Managing a Resource
Introduction to Data Structure
C++ Pointers and Strings
Out-of-Process Components
Java Basics Data Types in Java.
The C Language: Intro.
Chapter 9: Pointers and String
Lecture 7: RPC (exercises/questions)
C++ Pointers and Strings
Automation and IDispatch
CMSC 202 Exceptions.
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2006
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Introduction to Classes and Objects
Presentation transcript:

Interface Definition Language Jim Fawcett CSE 775 - Distributed Objects copyright © 2001-2005

Error Codes Error codes are returned as HRESULTS by all COM interface functions, with the exception of AddRef() and Release(). Visual studio smart pointer class _com_ptr simulates return by value and throws exceptions on errors by wrapping the proxy’s interface functions in wrapper classes that take care of those details. Test HRESULTS using the macros: #define SUCCEEDED(hr) (long(hr) >= 0) #define FAILED(hr) (long(hr) < 0) Specific Error Codes: S_OK : successful normal operation S_FALSE : return logical false as a success code E_FAIL : generic failure E_OUTOFMEMORY : memory allocation failed E_NOTIMPL : method not implemented E_UNEXPECTED : method call at incorrect time

Data Types Marshaling depends on exact knowledge of the sizes of data types. C and C++ do not define the sizes of their types. Each compiler and platform may define the sizes as they wish. COM bases its types on the NDR (Network Data Representation) types which have specified sizes. COM types suitable for marshaling are defined in wtypes.idl. These declarations include a lot of Windows specific data types as well as types useful for general COM programming. We can use all these types if we import wtypes.idl in our IDL file.

Decorations In order to marshal efficiently COM needs to know the direction of data flow, e.g.: [in], [out], [in, out], [out, retval] and will marshal data only in the direction(s) specified. For languages that have run-time support like Java and Visual Basic, an out parameter may be decorated with retval, indicating that those environments make the call look like a function return value: IDL : HRESULT Method([in] short arg, [out, retval] short *ret) Visual Basic: Function Method(arg as Integer) As Integer Since C++, has no such support, its interface looks like this: C++ : virtual HRESULT __stdcall Method(short arg, short *ret)

Memory Allocation Memory for [in] parameters is always allocated and freed by caller. Can use any kind of allocation, e.g., stack, heap, static. Memory for [out] parameters is always allocated by the method and always freed by the caller. Method : CoTaskMemAlloc(ULONG size); Client : CoTaskMemFree(LPVOID pv); Memory for [in,out] is allocated by caller, may be reallocated by method using: CoTaskMemRealloc(LPVOID pv, ULONG size) Must be freed by caller.

Pointer Decorations In order to marshal efficiently COM needs to know how pointers will be used: [ref] : pointers are initialized with valid (non-null) addresses at method invocation. This value can not change during method execution. All [out] pointers must be [ref] [unique] : pointers may be null, can not be aliased. [ptr] : same as unique except it can be aliased – requires much more work of marshaler, as it requires duplicate detection. Example: HRESULT method([in,out,ref] int *pInt);

Strings All characters in COM are represented using the OLECHAR data type: typedef wchar_t OLECHAR IDL uses the string decoration to tell the marshaler that a null terminated wide char string is being sent: HRESULT method([in,string] const OLECHAR *pOC); You can initialize an OLECHAR string this way: Const OLECHAR *pOC = OLESTR(“this is a string”); The C Run-Time Library provides two conversion functions: size_t mbstowcs(wchar_t *pOC, const char *pC, size_t count); Size_t wcstombs(char *pC, const wchar_t *pOC, size_t count);

Support for OLECHAR Strings The C Run-Time Library provides wide char string support that parallels its ANSI char string support, e.g.: wcslen : return number of characters in string (not equal to number of bytes) wcscpy : copy a wide source string to a wide destination string. You have to allocate enough memory for destination. wcscspn : find a substring in a wide char string wcschr: : find first occurrence of a char in a wide char string. wcsrchr : find the last occurrence of a char in wide char string. The C++ Standard library iostreams and strings module also provide support with: wcout, wcin wstring

BSTRs The BSTR type is a derived type used in Visual Basic and Microsoft Java (and presumably C#). BSTRs are recognized by the standard marshalers and used frequently by COM developers. BSTRs are length-prefixed, null terminated strings of OLECHARs.

BSTR Memory Allocation COM expects BSTRs to use a COM memory allocator, and provides several API functions for handling BSTRs, declared in oleauto.h: // allocate and initialize BSTR SysAllocString(const OLECHAR *pOC); BSTR SysAllocStringLen(BSTR *pBSTR, const OLECHAR *pOC, UINT count); // reallocate and initialize INT SysReAllocString(BSTR *pBSTR, const OLECHAR *pOC); INT SysReAllocStringLen(BSTR *pBSTR, const OLECHAR *pOC, UINT count); // free a BSTR void SysFreeString(BSTR bstr); // peek at length count as OLECHAR count or byte count UINT SysStringLen(BSTR bstr); UINT SysStringByteLen(BSTR bstr)

BSTR Memory Management When passing BSTRs as [in] parameters, the caller invokes SysAllocString prior to calling the method and SysFreeString after the method has completed. When passing strings from a method as an [out] parameter, it is the responsibility of the method to call SysAllocString before passing back the string. The caller releases the memory by calling SysFreeString. When passing BSTRs as [in, out] parameters, you treat them like [in] parameters. Reference: If you are going to use BSTRs in your project code, make sure you look carefully at “Strings the OLE Way”, Bruce McKinney, in MSDN online or in help. CComBSTR class provides a lot of help handling BSTRs. Check it out in MSDN.

Arrays Fixed arrays have sized determined at compile-time: HRESULT method([in] double arr[8]); Conformal arrays have size determined at run-time: HRESULT method([in] long dim, [in,size_is(dim)] double *da); Varying array sends only part of array: HRESULT method([in,out] long *first, [in,out] long *last, [in,out,first_is(first),length_is(last-first+1),size_is(100)] long *la); Open array sends part of array – size is determined at run-time. Same as above, except argument of size_is( ) is a variable.

Other Data Types We will encounter the Variant and Safe Array data types when we discuss Automation and the IDispatch interface. A variant is a discriminated (tagged) union that will hold any of a large subset of the IDL data types. There are a set of system functions designed to help manipulate variants. These are declared in oleauto.h Reference: “The Ultimate Data Type”, Bruce McKinney, MSDN A Safe Array is a structure that holds, possibly multi-dimensioned, arrays with descriptors of their sizes. There are a set of system functions designed to help manipulate safe arrays. These are declared in oleauto.h Reference: “The Safe OLE Way of Handling Arrays”, Bruce McKinney, MSDN

References for IDL MSDN/Platform SDK/Component Services/Microsoft Interface Definition Language Essential IDL, Martin Gudgin, Addison Wesley, 2001 Essential COM, Don Box, Addison Wesley, 1998 COM IDL & Interface Design, Al Major, WROX, 1999