+ All Categories
Home > Documents > Introduction to Visual C++- Part 1 API

Introduction to Visual C++- Part 1 API

Date post: 18-Nov-2014
Category:
Upload: pratik
View: 117 times
Download: 3 times
Share this document with a friend
Description:
This presentation introduces fundamental of VC++ with API. Next part containing MFC will be uploaded soon...
47
EMBEDDED SYSTEMS & VC++ (Introduction to API Programming) By PRATIK Gohil By PRATIK Gohil Sr. Lecturer, SVICS, Kadi. p2 gohil@gmail com p2.gohil@gmail.com Embedded System & VC++ By Pratik Gohil ([email protected])
Transcript
Page 1: Introduction to Visual C++- Part 1 API

EMBEDDED SYSTEMS & VC++

(Introduction to API Programming)

By PRATIK GohilBy PRATIK Gohil

Sr. Lecturer, SVICS, Kadi.

p2 gohil@gmail [email protected]

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 2: Introduction to Visual C++- Part 1 API

The Windows EnvironmentThe Windows Environment Windows is a GUI.

Windows and Windows applications have GUI (Office Package etc.) because Windows provides these programs with a number of built in functions and data with a number of built-in functions and data.

These functions offer functionality including drawing text with size and styles, graphic functions etc.y , g p .

These system function that an application can call are provided by interface known as API.

Every Windows has its own unique API.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 3: Introduction to Visual C++- Part 1 API

DOS vs Windows ProgrammingDOS vs. Windows Programming Most DOS programs deal directly with the computer’s p g y p

hardware to access the screen, read key strokes and so on.

Many DOS programs write directly to the video memory and the printer port.

St D iM

Application

Storage Device

Graphic Device

Memory

Other Applications

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 4: Introduction to Visual C++- Part 1 API

DOS vs Windows ProgrammingDOS vs. Windows Programming Windows introduces a layer of abstraction called the GDI.

Windows provides drivers and your program need not know the type of hardware attached to system.

I d f dd h d ll GDI f Instead of addressing hardware, programs call GDI functions that reference a data structure called device context.

Windows maps the device context structure to a physical Windows maps the device context structure to a physical device and issues the appropriate input/output instructions.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 5: Introduction to Visual C++- Part 1 API

DOS vs Windows ProgrammingDOS vs. Windows Programming Windows programs are structured to wait until they p g y

receive messages from Windows before they can continue with processing.

Windows moves programs and data around in memory to make room for other program’s modules and data.

GraphicsMenusMouse SupportMouse SupportFonts & FilesClipboard

Microsoft Windows

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 6: Introduction to Visual C++- Part 1 API

DOS vs Windows ProgrammingDOS vs. Windows Programming Windows is capable of running multiple programs, the

Windows programs are designed to share various system resources including VDU display, processor time etc.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 7: Introduction to Visual C++- Part 1 API

Windows ProgramWindows Program Every Windows program makes use of number of functions

that are part of system libraries of OS.

These libraries include functions for accessing screen, printers keyboard mouse and function for working various printers, keyboard, mouse and function for working various Windows objects including menus, bitmaps etc.

This collection of working functions is maintained in files, g ,which are: GDI32.DLL:This is GDI API library. It contains functions for

drawing graphics, accessing output device etc.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 8: Introduction to Visual C++- Part 1 API

Windows ProgramWindows Program USER32.DLL: It is a user library to provide support for user

i f i i l di i d i Wi d interface routines including creating and managing Windows etc.

KERNEL32.DLL: It is core Windows component library, and p y,contains functions for allocation memory, accessing files etc.

Each of these programs call driver files for specific function. Drivers are special programs which are required for accessing hardware Drivers are special programs which are required for accessing hardware

device.device.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 9: Introduction to Visual C++- Part 1 API

Wi d M ltit ki g E i tWindows – Multitasking Environment Windows gives user, the ability to execute several programs

simultaneously – called multitasking.

There are two popular multitasking schemes followed by windows:windows: Preemptive Cooperativep

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 10: Introduction to Visual C++- Part 1 API

Static & Dynamic LinkingStatic & Dynamic Linking Static Linking: Executable file is large, but execution is fast. Program needs recompilation if the library is updated. Memory requirements are fairly high since there is no sharing Memory requirements are fairly high, since there is no sharing

of code.

Dynamic Linking:y g Executable is small but execution is slow. Only the library needs to be updated. All programs share the same code, thus memory requirements

reduce.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 11: Introduction to Visual C++- Part 1 API

Message Driven ArchitectureMessage Driven Architecture A program in Windows environment never does anything p g y g

until Windows sends it a message.Event (e.g. Mouse Button Clicked)

I/O Event(e.g. KeyPress)

Windows

A li ti 2

ApplicationApplication 1 Application 3

Application 2

Embedded System & VC++ By Pratik Gohil ([email protected])

Traditional Program Event Handling In Windows

Page 12: Introduction to Visual C++- Part 1 API

CPU TimeCPU Time A single tasking OS gives control to an application as long g g g pp g

as the application runs. Windows gives control to an application only when an

event occurs that affects the applicationevent occurs that affects the application.Events

Time

Program Begins

Program Ends

Program Begins

Program Ends

Time

Embedded System & VC++ By Pratik Gohil ([email protected])

g g

Page 13: Introduction to Visual C++- Part 1 API

Structure of Windows ProgramConsider Example: APIHelloWorld

Structure of Windows Program A Windows program performs two basic tasks:p g p Creation of program’s own windows and performing startup

activity. Processing messages from Windows Processing messages from Windows.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 14: Introduction to Visual C++- Part 1 API

HelloWorld ProgramHelloWorld Program Each running copy of a program is called an instance.

To refer to a particular instance of a program, a handle is required.

A h dl d h W d ll A handle is unsigned integer that Windows uses internally to keep track of objects in memory. It is simply a number assigned by the system to a particular entity. It serves to g y y p y.identify the entity, including a window, a control or a block of memory.

A handle is a pointer to a pointer to a memory location.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 15: Introduction to Visual C++- Part 1 API

HelloWorld ProgramHelloWorld Program In the given program, LPSTR is another new Windows g p g ,

data type that is used, it is used to store Long Pointer to a String (e.g. char *)

Handle

Handle Table

Physical Address

Program

Windows Kernel

Embedded System & VC++ By Pratik Gohil ([email protected])Memory

Page 16: Introduction to Visual C++- Part 1 API

HelloWorld ProgramHelloWorld Program <windows.h> Header File: It is one of the key header files for Windows program. It

contains definitions needed by all Windows programs, including prototypes for Windows functions, definitions of structures, p yp , ,constants and derived data types.

WinMain Function: This is the windows equivalent of the normal C language

function main and every Windows program must have a WinMain functionWinMain function.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 17: Introduction to Visual C++- Part 1 API

WinMain() ParametersWinMain() Parameters Function accepts 4 arguments: hInstance:This is the instance handle for the running

application. Windows creates this unique ID number when the application starts. If we start more than one instances of a pp .program, each will have a unique instance handle.

hPrevInstance: More than one copy of the same Windows li i h i If h i d application can run at the same time. If another copy is started,

hPrevInstance will contain the hInstance value for the last copy started.

lpszCmdLine: A pointer to a character string containing the command line arguments passed to the program.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 18: Introduction to Visual C++- Part 1 API

nShowCmd: An integer value that is passed to the function. g pThis number tells the program whether the window that it creates should appear minimized, as an icon, normal or maximized when first displayed.p y[Reference #1 For More Options]

Return Value: If the function succeeds, terminating when it receives a WM QUIT If the function succeeds, terminating when it receives a WM_QUIT

message, it should return the exit value contained in that message's wParam parameter. If the function terminates before entering the message loop, it h ld t it should return zero.

Embedded System & VC++ By Pratik Gohil ([email protected])

Coming up next…

Page 19: Introduction to Visual C++- Part 1 API

Function Calling ConventionFunction Calling Convention In _pascal or PASCAL calling convention, the parameters _p g , p

used to get passed to a function from left to right. PASCAL calling convention does not allow functions that h bl b f have variable number of arguments.

In _stdcall calling convention, values are passed from right to leftto left.

nCmdShow

l C dLi

hInstance

hP I t

Pascal Calling

ConventionlpszCmdLine

hPrevInstance

hPrevInstance

lpszCmdLine

Convention

C Calling

Embedded System & VC++ By Pratik Gohil ([email protected])

hInstance nCmdShowC Calling Convention

Page 20: Introduction to Visual C++- Part 1 API

A Sample Windows ApplicationConsider Example: APIFirstWindow

A Sample Windows Application Given program creates an application instance and its own

Window on screen.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 21: Introduction to Visual C++- Part 1 API

U d t di g Fi tWi d P gUnderstanding FirstWindow Program

Windows begins execution from the function WinMain( ).g ( )

HWND stores the handle to the created window.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 22: Introduction to Visual C++- Part 1 API

U d t di g Fi tWi d P gUnderstanding FirstWindow Program The MSG structure contains message information from a

th d' thread's message queue. typedef struct{ {

HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt;

} MSG;

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 23: Introduction to Visual C++- Part 1 API

U d t di g Fi tWi d P gUnderstanding FirstWindow Program hwnd Handle to the window whose window procedure receives the

message.

message message Specifies the message identifier. Applications can only use the

low word; the high word is reserved by the system.

wParam Specifies additional information about the message. The exact

d d h l f h bmeaning depends on the value of the message member.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 24: Introduction to Visual C++- Part 1 API

U d t di g Fi tWi d P gUnderstanding FirstWindow Program lParam Specifies additional information about the message. The exact

meaning depends on the value of the message member.

time time Specifies the time at which the message was posted.

ptpt Specifies the cursor position, in screen coordinates, when the

message was posted.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 25: Introduction to Visual C++- Part 1 API

The WNDCLASS structure contains the window class attributes that are registered by the RegisterClass functionregistered by the RegisterClass function.

typedef struct { UINT style; WNDPROC lpfnWndProc;WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance;HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName;

} WNDCLASS;

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 26: Introduction to Visual C++- Part 1 API

style Specifies the class style(s). This member can be any combination

of the Class Styles. Reference #2 For More Options# Op

lpfnWndProc Pointer to the window procedure.

cbClsExtra Specifies the number of extra bytes to allocate following the

d l h l h b window-class structure. The system initializes the bytes to zero.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 27: Introduction to Visual C++- Part 1 API

cbWndExtra Specifies the number of extra bytes to allocate following the

window instance. The system initializes the bytes to zero.

hInstance hInstance Handle to the instance that contains the window procedure for

the class.

hIcon Handle to the class icon. This member must be a handle to an

f h b h d icon resource. If this member is NULL, the system provides a default icon.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 28: Introduction to Visual C++- Part 1 API

hCursor Handle to the class cursor. This member must be a handle to a

cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into p y pthe application's window.

hbrBackground Handle to the class background brush. This member can be a

handle to the physical brush to be used for painting the background or it can be a color valuebackground, or it can be a color value.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 29: Introduction to Visual C++- Part 1 API

GetStockObject( ):

Th G tSt kObj t f ti t i h dl t f th t k The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes.

HGDIOBJ GetStockObject( int f Obj t );HGDIOBJ GetStockObject( int fnObject );Reference #3 For More Options

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 30: Introduction to Visual C++- Part 1 API

lpszMenuName Pointer to a null-terminated character string that specifies the

resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the . y g y ,MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 31: Introduction to Visual C++- Part 1 API

lpszClassName Pointer to a null-terminated string or is an atom. If this

parameter is an atom, it must be a class atom created by a previous call to the RegisterClass function. If p eg ste C ass .lpszClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass

f th d fi d t l l or any of the predefined control-class names.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 32: Introduction to Visual C++- Part 1 API

Use of RegisterClass( )Use of RegisterClass( ) The WNDCLASS structure must be registered with g

Windows before it can be used to create windows.

This is one time affair, therefore it is only done when hPrevInstance is zero.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 33: Introduction to Visual C++- Part 1 API

Understanding CreateWindow( )Understanding CreateWindow( ) The CreateWindow( ) creates new window.( )

The function is used both in WinMain( ) to create the application’s main window, and also within the program to create child window and child window controls.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 34: Introduction to Visual C++- Part 1 API

CreateWindow( ) SyntaxCreateWindow( ) Syntax HWND CreateWindow(LPCTSTR lpClassName,

LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance LPVOID lpParam);HINSTANCE hInstance, LPVOID lpParam); lpClassName: Pointer to a null-terminated string (class name) created by a previous call

h l to the RegisterClass( ).

lpWindowName: Pointer to a null-terminated string that specifies the window name. If the g p

window style specifies a title bar, the window title pointed to by lpWindowName is displayed in the title bar.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 35: Introduction to Visual C++- Part 1 API

dwStyle Specifies the style of the window being created.

Reference #4 For More Options

x Specifies the initial horizontal position of the window. For an overlapped

or pop-up window, the x parameter is the initial x-coordinate of the window's upper-left corner, in screen coordinates.

y Specifies the initial vertical position of the window. For an overlapped or

pop-up window, the y parameter is the initial y-coordinate of the p p p , y p ywindow's upper-left corner, in screen coordinates.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 36: Introduction to Visual C++- Part 1 API

nWidth Specifies the width of the window.

nHeight Specifies the height of the window.p g

hWndParent Handle to the parent or owner window of the window being created. To

create a child window or an owned window supply a valid window create a child window or an owned window, supply a valid window handle. This parameter is optional for pop-up windows.

hMenudl l d d h Handle to a menu. For an overlapped or pop-up window, hMenu

identifies the menu to be used with the window; it can be NULL if the class menu is to be used.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 37: Introduction to Visual C++- Part 1 API

hInstance Handle to the instance of the module to be associated with the window.

lpParam Pointer to a value to be passed to the window through the p g

CREATESTRUCT structure passed in the lpParam parameter the WM_CREATE message. If an application calls CreateWindow to create a multiple-document interface (MDI) client window, lpParam must point to a CLIENTCREATESTRUCT structure.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 38: Introduction to Visual C++- Part 1 API

ShowWindow( ) Syntax & UseShowWindow( ) Syntax & Use BOOL ShowWindow(HWND hWnd, int nCmdShow); hWnd Handle to the window.

nCmdShownCmdShow Specifies how the window is to be shown. This parameter is ignored the

first time an application calls ShowWindow. Otherwise, the first time ShowWindow is called the value should be the value obtained by the ShowWindow is called, the value should be the value obtained by the WinMain( ) in its nShowCmd parameter.Reference #5 For More Options

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 39: Introduction to Visual C++- Part 1 API

Understanding GetMessage( )Understanding GetMessage( ) The GetMessage( ) function retrieves a message from the g ( ) g

application queue of the application.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 40: Introduction to Visual C++- Part 1 API

GetMessage( ) SyntaxGetMessage( ) Syntax BOOL GetMessage(LPMSG lpMsg, HWND hWnd, UINT

wMsgFilterMin, UINT wMsgFilterMax ); lpMsg Pointer to an MSG structure that receives message information from the Pointer to an MSG structure that receives message information from the

thread's message queue.

hWndH dl h i d h b i d Handle to the window whose messages are to be retrieved.

wMsgFilterMin Specifies the integer value of the lowest message value to be retrieved. g g

Use WM_KEYFIRST to specify the first keyboard message or WM_MOUSEFIRST to specify the first mouse message.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 41: Introduction to Visual C++- Part 1 API

wMsgFilterMax Specifies the integer value of the highest message value to be retrieved.

Use WM_KEYLAST to specify the last keyboard message or WM_MOUSELAST to specify the last mouse message.

Return Value: If the function retrieves a message other than WM_QUIT, the return

value is nonzero.

If the function retrieves the WM_QUIT message, the return value is zero.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 42: Introduction to Visual C++- Part 1 API

Understanding TranslateMessage( )Understanding TranslateMessage( ) The TranslateMessage( ) is required only if the application

needs to interact with the keyboard. The keyboard input is translated into virtual key codes, which all Windows application s understandapplication s understand.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 43: Introduction to Visual C++- Part 1 API

Understanding DispatchMessage( )Understanding DispatchMessage( ) The DispatchMessage( ) gets the message processed by

sending it to the appropriate processing unction, in this case, ProcessMessagefn.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 44: Introduction to Visual C++- Part 1 API

Understanding DefWindowProc( )Understanding DefWindowProc( ) Windows generates a lot of messages of different types, a

typical Windows application may not process all the messages generated by the application.

For all such unprocessed messages the application must make For all such unprocessed messages the application must make a call to the DefWindowProc( ) which does default processing.p g

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 45: Introduction to Visual C++- Part 1 API

LRESULT DefWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); hWnd Handle to the window procedure that received the message Handle to the window procedure that received the message.

Msg Specifies the message.

wParam Specifies additional message information. The content of this parameter

depends on the value of the Msg parameter. g

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 46: Introduction to Visual C++- Part 1 API

lParam Specifies additional message information. The content of this parameter

depends on the value of the Msg parameter.

Embedded System & VC++ By Pratik Gohil ([email protected])

Page 47: Introduction to Visual C++- Part 1 API

Understanding WM DESTROYUnderstanding WM_DESTROY The WM_DESTROY message is generated when the

window is destroyed. (WM stands for Window Message)

The PostQuitMessage( ) indicates to the system that a thread has made a request to terminate (quit)thread has made a request to terminate (quit). void PostQuitMessage(int nExitCode); nExitCode

Specifies an application exit code.

Return Value;

It doesn’t return anything.It doesn t return anything.

To Be Continued….

Embedded System & VC++ By Pratik Gohil ([email protected])


Recommended