Windows Programming using Dev C Introduction to Window

  • Slides: 52
Download presentation
Windows Programming using Dev C++

Windows Programming using Dev C++

Introduction to Window Elements

Introduction to Window Elements

Window Program: Message Box #include<windows. h> int _stdcall Win. Main ( HINSTANCE h. Instance,

Window Program: Message Box #include<windows. h> int _stdcall Win. Main ( HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmdline, int n. Cmd. Show) { Message. Box ( 0, "Hello!", "Title", 0 ) ; return 0; }

Terms Used • _stdcall – conventional prefix • Win. Main() – main function for

Terms Used • _stdcall – conventional prefix • Win. Main() – main function for windows prog • HINSTANCE – typedef for unsigned int • LPSTR – typedef for char * • HINSTANCE h. Instance – window ID • HINSTANCE h. Prev. Instance – prev window info • LPSTR lpsz. Cmdline – similar to argv (command line arguments) • int n. Cmd. Show – window type (max, normal or min) • Message. Box() – Can be used for displaying the message and pausing the program control just like getch() function: Turbo C++ • Message. Box ( HWND h. Wnd, /*Handle for the parent window*/ LPCSTR lpsz. Text, /* Text to be displayed */ LPCSTR lpsz. Title, /*Title of the message box */ int n. Buttons /*Bottons or icons to be displayed*/ ); Examples: • Message. Box ( 0, lpsz. Cmdline, "Title", 0 ) ; • Message. Box ( 0, “Are you sure”, “Confirmation”, MB_YESNO ) ; • Message. Box ( 0, “Print to the Printer”, “Caption”, MB_YESNOCANCEL) ; • Message. Box ( 0, “icon is all about style”, “Caption”, MB_OK | MB_ICONINFORMATION ) ;

Windows Programming Model

Windows Programming Model

EVENTS in the form of Messages • Applications respond to the events by processing

EVENTS in the form of Messages • Applications respond to the events by processing messages sent by the operating system. • An event could be a keystroke, a mouse click, or a command for a window to repaint itself, among other things. • The entry point for a Windows program is a function named Win. Main(), but most of the action takes place in a function known as the window procedure.

 • The window procedure processes messages sent to the window. Win. Main() creates

• The window procedure processes messages sent to the window. Win. Main() creates that window and then enters a message loop, alternately retrieving messages and dispatching them to the window procedure. Messages wait in a message queue until they are retrieved. • A typical Windows application performs the bulk of its processing in response to the messages it receives, and in between messages, it does little except wait for the next message to arrive.

 • The message loop ends when a WM_QUIT message is retrieved from the

• The message loop ends when a WM_QUIT message is retrieved from the message queue, signaling that it's time for the application to end. • This message usually appears because the user selected Exit from the File menu, clicked the close button (the small button with an X in the window's upper right corner), or selected Close from the window's system menu. • When the message loop ends, Win. Main() returns and the application terminates.

A Real World Window Program

A Real World Window Program

STEP 1. Include the following Library: #include <windows. h>

STEP 1. Include the following Library: #include <windows. h>

STEP 2. Create main Win. Main function and Register the Window Class. int WINAPI

STEP 2. Create main Win. Main function and Register the Window Class. int WINAPI Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { /* HINSTANCE h. Instance: WINDOW ID, Handle to the programs executable module (the. exe file in memory) HINSTANCE h. Prev. Instance: Always NULL for Win 32 programs. h. Prev. Instance used to be the handle to the previously run instance of your program (if any) in Win 16 LPSTR lp. Cmd. Line: The command line arguments as a single string. In Win 32 Data type for char* is LPSTR. int n. Cmd. Show: Window type (max, normal or min). An integer value which may be passed to Show. Window(). */ WNDCLASSEX wc; /* A properties struct of our window */ HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */ MSG msg; /* A temporary location for all messages */ /* REGISTERING THE WINDOW SETTING ESSENTIAL PARAMETERS */ BY memset(&wc, 0, sizeof(wc)); /* zero out the struct and set the stuff we want to modify */ wc. cb. Size = sizeof(WNDCLASSEX); /* set the size of the structure */ wc. lpfn. Wnd. Proc = Wnd. Proc; /* This is where we will send messages to */ /* Pointer to the window procedure for this window*/ wc. h. Instance = h. Instance; /* Handle to the application instance */ wc. lpsz. Class. Name = "Window. Class"; /*Name to identify the class with. */

/* IF THE WINDOW IS NOT REGISTERED DISPLAY ERROR MESSAGE */ if(!Register. Class. Ex

/* IF THE WINDOW IS NOT REGISTERED DISPLAY ERROR MESSAGE */ if(!Register. Class. Ex (&wc)) { Message. Box (NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION|MB_OK); return 0; } /* Call Register. Class. Ex() and check for failure, if it fails pop up a message which says so and abort the program by returning from the Win. Main() function. */

STEP 3: Create and Show the Window and the Message Loop hwnd = Create.

STEP 3: Create and Show the Window and the Message Loop hwnd = Create. Window. Ex( WS_EX_CLIENTEDGE, /* extended window style for window border*/ "Window. Class", /* give registered window name as it tells which window to create*/ "Caption", /* Title of your window */ WS_VISIBLE|WS_OVERLAPPEDWINDOW, /*Window style parameter */ CW_USEDEFAULT, /* X*/ CW_USEDEFAULT, /* Y */ 640, /* width */ 480, /* height */ /* X and Y co-ordinates for the top left corner of your window, and the width and height of the window. The units are pixels. */ NULL, /* Parent Window handle. The parent handle is NULL as this is our main or Top Level window*/ NULL, /* Menu handle */ h. Instance, /* Application instance handle*/ NULL); /* A pointer to window creation data*/ /* IF THE WINDOW IS NOT CREATED DISPLAY ERROR MESSAGE AND RETURN*/ if(hwnd == NULL) { Message. Box(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION|MB_ OK); return 0; } /*Show Window Function*/ Show. Window(hwnd, n. Cmd. Show); /* The n. Cmd. Show parameter is optional and is same as SW_SHOWNORMAL for normal sized window. However, using this parameter passed into Win. Main() gives whoever is running your program to specify whether or not they want your window to start off visible, maximized, minimized (SW_SHOWMINIMIZED, SW_SHOWMAXIMIZED) etc. */

/* THE MESSAGE LOOP This is the heart of our program where all input

/* THE MESSAGE LOOP This is the heart of our program where all input is processed and sent to Wnd. Proc. Get. Message() is requesting the next available message to be removed from the message queue and returned for processing. Note that Get. Message blocks code flow until it receives something, so this loop will not produce unreasonably high CPU usage */ while(Get. Message(&msg, NULL, 0, 0) ) { /* If no error is received. . . */ Translate. Message(&msg); /* Translate key codes to chars if present */ Dispatch. Message(&msg); /* Send it to Wnd. Proc */ } return msg. w. Param; }

STEP 4: The Window Procedure /* This is where all the messages that are

STEP 4: The Window Procedure /* This is where all the messages that are sent to our window get processed. LRESULT is a typedef of a long int CALLBACK is a typedef of _stdcall. */ LRESULT CALLBACK Wnd. Proc ( HWND hwnd, /* HWND parameter is the handle of your window, the one that the message applies to */ UINT Message, /* Win 32 Unsigned Int datatype: UINT message received*/ WPARAM w. Param, /* Parameters passed for different types of messages */ LPARAM l. Param /* Parameters passed for different types of messages */ ) { switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); break; /* When close (X) button is pressed Destroy. Window() sends WM_DESTROY message to the window getting destroyed, and then destroys any remaining child windows before finally removing target window from the system. Since this is the only window in this program, we want the program to exit, so we call Post. Quit. Message(). This posts the WM_QUIT message to the message loop. */ case WM_DESTROY: Post. Quit. Message(0); break; /* Upon destruction, tell the main thread to stop */ default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); /* All other messages (a lot of them) are processed using default procedures */ } return 0; }

Complete Windows Real World Program

Complete Windows Real World Program

#include <windows. h> LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param,

#include <windows. h> LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param, LPARAM l. Param) {switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); break; case WM_DESTROY: Post. Quit. Message(0); break; default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); } return 0; } int WINAPI Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { WNDCLASSEX wc; HWND hwnd; MSG msg; memset(&wc, 0, sizeof(wc)); wc. cb. Size = sizeof(WNDCLASSEX); wc. lpfn. Wnd. Proc = Wnd. Proc; wc. h. Instance = h. Instance; wc. lpsz. Class. Name = "Window. Class"; if(!Register. Class. Ex (&wc)) { Message. Box (NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = Create. Window. Ex(WS_EX_CLIENTEDGE, "Window. Class", "Caption", WS_VISIBLE| WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 640, 480, NULL, h. Instance, NULL); if(hwnd == NULL) { Message. Box(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION|MB_OK); return 0; } Show. Window(hwnd, n. Cmd. Show); while(Get. Message(&msg, NULL, 0, 0)) { Translate. Message(&msg); Dispatch. Message(&msg); } return msg. w. Param; }

#include <windows. h> int WINAPI Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance,

#include <windows. h> int WINAPI Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { MSG msg; WNDCLASSEX wc; HWND hwnd; memset(&wc, 0, sizeof(wc)); wc. cb. Size = sizeof(WNDCLASSEX); wc. lpfn. Wnd. Proc = Wnd. Proc; wc. h. Instance = h. Instance; wc. lpsz. Class. Name = "Window. Class"; if(!Register. Class. Ex (&wc)) {Message. Box (NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = Create. Window. Ex(WS_EX_CLIENTEDGE, "Window. Class", "Caption", WS_VISIBLE| WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 640, 480, NULL, h. Instance, NULL); if(hwnd == NULL) {Message. Box(NULL, "Window Creation. Failed!", "Error!", MB_ICONEXCLAMATION|MB_OK); r eturn 0; } Show. Window(hwnd, n. Cmd. Show); while(Get. Message(&msg, NULL, 0, 0) ) { Translate. Message(&msg); Dispatch. Message(&msg); } return msg. w. Param; } helper. h file /* perform application initialization. */ Init. Instance ( h. Instance, n. Cmd. Show, "title" ) ; /*For the sake of simplicity, for further conventions of these slides, we will be using helper. h header file in which the Init. Instance() function is defined and performs all the tasks of initializing, registering, creating and showing the window. It takes 3 parameters: handle or WINDOW ID, n. Cmd. Show: Window type (max, normal or min) Caption or title of the present window. */

LRESULT CALLBACK Wnd. Proc ( HWND, UINT, WPARAM, LPARAM ) ; HINSTANCE h. Inst

LRESULT CALLBACK Wnd. Proc ( HWND, UINT, WPARAM, LPARAM ) ; HINSTANCE h. Inst ; // current instance BOOL Init. Instance ( HINSTANCE h. Instance, int n. Cmd. Show, char* p. Title ) { char classname[ ] = "My. Window. Class" ; HWND h. Wnd ; WNDCLASSEX wcex ; wcex. cb. Size = sizeof ( WNDCLASSEX ) ; wcex. style = CS_HREDRAW | CS_VREDRAW ; wcex. lpfn. Wnd. Proc = ( WNDPROC ) Wnd. Proc ; wcex. cb. Cls. Extra = 0 ; wcex. cb. Wnd. Extra = 0 ; wcex. h. Instance = h. Instance ; wcex. h. Icon = NULL ; wcex. h. Cursor = Load. Cursor ( NULL, IDC_ARROW ) ; wcex. hbr. Background = ( HBRUSH )( COLOR_WINDOW + 1 ) ; wcex. lpsz. Menu. Name = NULL ; wcex. lpsz. Class. Name = classname ; wcex. h. Icon. Sm = NULL ; if ( !Register. Class. Ex ( &wcex ) ) return FALSE ; h. Inst = h. Instance ; // Store instance handle in our global variable h. Wnd = Create. Window ( classname, p. Title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, NULL, h. Instance, NULL ) ; if ( !h. Wnd ) return FALSE ; Show. Window ( h. Wnd, n. Cmd. Show ) ; Update. Window ( h. Wnd ) ; return TRUE ; } PROCEDURE to include helper. h 1. 2. 3. 4. Create a new file named helper. h (use. h extension) Copy the contents given on the left as it is in this newly created file. Save it in current working directory of your project. Use this file by writing #include “helper. h” in your window programs REMEMBER: To copy this file to your project directory—the directory in which you are going to create the program.

THE NEW PROGRAMS WILL LOOK AS: #include <windows. h> #include "helper. h" int __stdcall

THE NEW PROGRAMS WILL LOOK AS: #include <windows. h> #include "helper. h" int __stdcall Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { MSG msg ; /* perform application initialization */ Init. Instance ( h. Instance, n. Cmd. Show, "title" ) ; /* message loop */ while(Get. Message(&msg, NULL, 0, 0)) { Translate. Message(&msg); Dispatch. Message(&msg); } return 0; } LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param, LPARAM l. Param) { switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); break; case WM_DESTROY: Post. Quit. Message(0); break; default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); } return 0; }

Flow Chart

Flow Chart

Drawing to a Window

Drawing to a Window

Using Graphics in Dev C++ STEP 1: Create a new Windows Application project in

Using Graphics in Dev C++ STEP 1: Create a new Windows Application project in Dev C++ and provide a new name to your project. STEP 2: Save the newly recommended and created main. cpp file. STEP 3: Download graphics. h to the include/ subdirectory of the Dev-C++ directories, from the following location: https: //www. cs. colorado. edu/~main/b gi/dev-c++/graphics. h STEP 4: Download libbgi. a to the lib/ In order to use the Win. BGIm subdirectory of the Dev-C++ directories.

Using Graphics in Dev C++ STEP 5: Go to Project> Project Options>Parameters STEP 6:

Using Graphics in Dev C++ STEP 5: Go to Project> Project Options>Parameters STEP 6: Include the following commands to the linker portion and press OK: -lbgi -lgdi 32 -lcomdlg 32 -luuid -loleaut 32 -lole 32

Drawing to a Window • Drawing to a window involves handling the WM_PAINT message.

Drawing to a Window • Drawing to a window involves handling the WM_PAINT message. • This message is generated whenever the client area of the window needs to be redrawn. • This redrawing would be required in the following situations: – When the Window is displayed for the first time. – When the window is minimized and then maximized. – When some portion of the window is overlapped by another window and the overlapped window is dismissed. – When the size of the window changes on stretching its boundaries. – When the window is dragged out of the screen and then brought back into the screen.

Drawing to a Window • When the switch-case structure inside Wnd. Proc( ) finds

Drawing to a Window • When the switch-case structure inside Wnd. Proc( ) finds that the message ID passed to Wnd. Proc( ) is WM_PAINT, it calls the function On. Paint( ). • Within On. Paint( ), API function Begin. Paint( ) is called. • This function obtains a handle to the device context. • Additionally it also fills the PAINTSTRUCT structure with information about the area of the window which needs to be repainted. • Lastly it removes WM_PAINT from the message queue. • After obtaining the device context handle, the control enters a loop.

Window Program for Drawing Shapes #include <windows. h> #include "helper. h " int __stdcall

Window Program for Drawing Shapes #include <windows. h> #include "helper. h " int __stdcall Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { MSG msg ; Init. Instance ( h. Instance, n. Cmd. Show, "title" ) ; while(Get. Message(&msg, NULL, 0, 0)) { Translate. Message(&msg); Dispatch. Message(&msg); } return 0; } LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param, LPARAM l. Param) { switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); brea k; case WM_DESTROY: Post. Quit. Message(0); break; case WM_PAINT : On. Paint ( hwnd ) ; break ; default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); } return 0; } For drawing: - Line - Rectangle

Window Program for Drawing Shapes void On. Paint ( HWND hwnd ) { HDC

Window Program for Drawing Shapes void On. Paint ( HWND hwnd ) { HDC hdc ; // Handle to the Device PAINTSTRUCT ps ; //PAINTSTRUCT object HBRUSH hbr ; //Handle to the new brush HGDIOBJ holdbr ; //Handle to the old default brush hdc = Begin. Paint ( hwnd, &ps ) ; /* Obtaining a handle for Device Context (DC)*/ hbr = Create. Solid. Brush ( RGB ( 255, 0, 0 ) ) ; /*Set color of the new solid brush and return handle in hbr */ holdbr = Select. Object ( hdc, hbr ) ; /*The handle of the default brush in DC is collected in holdbr */ Move. To. Ex ( hdc, 10, NULL ) ; /* Draw a line in hdc from x, y coordinates */ Line. To ( hdc, 200, 10 ) ; /*specifies the end points of the line*/ Rectangle ( hdc, 10, 200, 100 ) ; /*draw rectangle with x 1, y 1, x 2, y 2 */ Select. Object ( hdc, holdbr ) ; /*The handle of the default brush is selected back in DC via holdbr*/ Delete. Object (hbr ); /*Delete new solidbrush*/ End. Paint ( hwnd, &ps ) ; /*Exit paint*/ }

Revision of Windows Programming – I Programs

Revision of Windows Programming – I Programs

Windows Program for Open Window #include<windows. h> int _stdcall Win. Main ( HINSTANCE h.

Windows Program for Open Window #include<windows. h> int _stdcall Win. Main ( HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show){ HWND h ; h = Create. Window ( "BUTTON", "Press Me", WS_OVERLAPPEDWINDOW, 10, 150, 100, 0, 0 ) ; Show. Window ( h, n. Cmd. Show ) ; Message. Box ( 0, "Hi", "Waiting", MB_OK ) ; }

Windows Program for Array of Windows #include<windows. h> int _stdcall Win. Main ( HINSTANCE

Windows Program for Array of Windows #include<windows. h> int _stdcall Win. Main ( HINSTANCE i, HINSTANCE j, LPSTR k, int n. Cmd. Show ){ HWND h[2] ; for(int i=0; i<2; i++) { h[i] = Create. Window ( "BUTTON", "Hit Me", WS_OVERLAPPEDWINDOW, 10+100*i, 150, 100, 0, 0 ); Show. Window ( h[i], n. Cmd. Show ) ; } Message. Box ( 0, "Hi", "Waiting", MB_OK ) ; }

Windows Program for Close Window #include <windows. h> #include "helper. h" int __stdcall Win.

Windows Program for Close Window #include <windows. h> #include "helper. h" int __stdcall Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { MSG msg ; Init. Instance ( h. Instance, n. Cmd. Show, “Title" ) ; while(Get. Message(&msg, NULL, 0, 0)) { Translate. Message(&msg); Dispatch. Message(&msg); } return 0; } LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param, LPARAM l. Param) { switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); break; case WM_DESTROY: Post. Quit. Message(0); break; default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); } return 0; }

Windows Program for Maximized Window #include <windows. h> #include "helper. h" int __stdcall Win.

Windows Program for Maximized Window #include <windows. h> #include "helper. h" int __stdcall Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { MSG msg ; Init. Instance ( h. Instance, SW_SHOWMAXIMIZED, “Title" ) ; // instead of n. Cmd. Show pass SW_SHOWMAXIMIZED for maximized window while(Get. Message(&msg, NULL, 0, 0)) { Translate. Message(&msg); Dispatch. Message(&msg); } return 0; } LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param, LPARAM l. Param) { switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); break; case WM_DESTROY: Post. Quit. Message(0); break; default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); } return 0; }

Windows Program for Minimized Window #include <windows. h> #include "helper. h" int __stdcall Win.

Windows Program for Minimized Window #include <windows. h> #include "helper. h" int __stdcall Win. Main (HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, LPSTR lpsz. Cmd. Line, int n. Cmd. Show) { MSG msg ; Init. Instance ( h. Instance, SW_SHOWMINIMIZED, “Title" ) ; // instead of n. Cmd. Show pass SW_SHOWMINIMIZED for minimized window while(Get. Message(&msg, NULL, 0, 0)) { Translate. Message(&msg); Dispatch. Message(&msg); } return 0; } LRESULT CALLBACK Wnd. Proc (HWND hwnd, UINT Message, WPARAM w. Param, LPARAM l. Param) { switch(Message) { case WM_CLOSE: Destroy. Window(hwnd); break; case WM_DESTROY: Post. Quit. Message(0); break; default: return Def. Window. Proc(hwnd, Message, w. Param, l. Param); } return 0; }

Component Object Model (COM)

Component Object Model (COM)

Why COM ? • Now-a-days, software industry cannot support applications that are of static

Why COM ? • Now-a-days, software industry cannot support applications that are of static nature and cannot be modified after they are shipped. • The solution lies in breaking these monolithic software applications into separate pieces also known as “components”. • As the technology advances, new components can be replaced by the existing components that make up these applications.

What is COM ? • “Component Object Model” is a specification for a way

What is COM ? • “Component Object Model” is a specification for a way of creating components and building applications from these components. • COM is a specification for creating reusable software components. • COM was developed by Microsoft for making Microsoft Applications more flexible, dynamic and customizable. • Currently all Microsoft Applications use COM components.

Features of COM • Dynamic Linking: Helps to replace components in the application while

Features of COM • Dynamic Linking: Helps to replace components in the application while the application is running. • Encapsulation: Everything is linked via Interfaces. Hence the change is the component definition without change in the Interface along with the dynamic linking results in encapsulation. An interface defines a set of methods that an object can support, without dictating anything about the implementation. In C++, the nearest equivalent to an interface is a pure virtual class —that is, a class that contains only pure virtual methods and no other members.

Features of COM • Language Independence: It defines the binary interface between an application

Features of COM • Language Independence: It defines the binary interface between an application and a software component. As a binary standard, COM is language-neutral. • Application Customization: Applications can be easily customized or updated by adding new components or changing existing components. • Component Libraries: Supports rapid application development. One can choose components from a component library and integrate them together to form applications. • Distributed Components: Building today’s world distributed client-server applications becomes much easier with the help of components. • Interoperability and Versioning: Support for updation of newly developed content without effecting other system components. Also assures the interoperability of different binary code contents.

COM Fundamentals for developing Windows Applications 5 Fundamental concepts of Component Object Model are:

COM Fundamentals for developing Windows Applications 5 Fundamental concepts of Component Object Model are: • A binary standard for function calling between components: ü The binary standard allows components written in different languages to call each other's functions. ü It supports the standard way of calling functions via pointers. So, any language that supports pointers can be used to write and call its functions.

COM Fundamentals for developing Windows Applications • A provision for strongly-typed groupings of functions

COM Fundamentals for developing Windows Applications • A provision for strongly-typed groupings of functions into interfaces: ü Interfaces are logical groups of related functions-functions that together provide some well-defined capability. ü COM Interface: is a collection of abstract operations one can perform on an object. ü COM Class (or coclass): A named body of code used to produce COM objects. ü Class object (or Class factory): initializes the creation of an object. ü Class loader : on demand, load all coclasses.

COM Fundamentals for developing Windows Applications • A base interface: ü COM defines one

COM Fundamentals for developing Windows Applications • A base interface: ü COM defines one special interface, IUnknown, to implement some of the essential functionality. ü All COM components are required to implement the IUnknown interface and are derived from IUnknown. ü IUnknown has three methods: o Query. Interface: Mechanism that a client uses to get an interface pointer from a COM component and also checks whether a COM interface is supported by a COM component or not. o Add. Ref: Called when another COM component is using the interface. o Release: Called when COM component no longer requires that interface

COM Fundamentals for developing Windows Applications • IUnknown through its Query. Interface method defines

COM Fundamentals for developing Windows Applications • IUnknown through its Query. Interface method defines a way for components to dynamically discover the interfaces implemented by other components. • IUnknown via its Add. Ref and Release methods support reference counting to allow components to track their own lifetime and delete themselves when appropriate. • A COM component implements IUnknown to control its lifespan and to provide access to the interfaces it supports.

COM Fundamentals for developing Windows Applications • A mechanism to identify components and their

COM Fundamentals for developing Windows Applications • A mechanism to identify components and their interfaces uniquely, worldwide: ü Globally Unique Identifiers (GUIDs): COM uses globally unique identifiers (GUIDs), which are 128 -bit integers that are unique all across the universe, to identify every interface and every COM component class. ü GUIDs provide a unique identifier for each class and interface, thereby preventing naming conflicts.

COM Fundamentals for developing Windows Applications • A "component loader" to set up and

COM Fundamentals for developing Windows Applications • A "component loader" to set up and manage component interactions: ü COM Library: is implemented as part of the operating system that provides the mechanics of COM. ü The COM Library provides the ability to make IUnknown calls across processes. ü It encapsulates and provides the bridge for launching components and establishing connections between components.

COM Fundamentals for developing Windows Applications

COM Fundamentals for developing Windows Applications

Sample Steps for showing Open Dialog using COM in Windows Application 1. Initialize the

Sample Steps for showing Open Dialog using COM in Windows Application 1. Initialize the COM library. 2. Create the Common Item Dialog class object and get a pointer to the object's IFile. Open. Dialog interface. 3. Call the object's Show method, which shows the dialog box to the user. This method blocks until the user dismisses the dialog box. 4. Call the object's Get. Result method. This method returns a pointer to a second COM object, called a Shell item object. The Shell item, which implements the IShell. Item interface, represents the file that the user selected. 5. Call the Shell item's Get. Display. Name method. This method gets the file path, in the form of a string. 6. Show a message box that displays the file path. 7. Uninitialize the COM library.

Flow Chart

Flow Chart