Clipboard - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Clipboard

Description:

The linker should know that DLL functions are available ... This will tell the linker that func() is exportable. Writing a DLL, DLLMain ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 29
Provided by: conest
Category:
Tags: clipboard | linker

less

Transcript and Presenter's Notes

Title: Clipboard


1
Clipboard
2
Transferring Data BetweenApplications with the
Clipboard
  • The Windows Clipboard
  • A global memory block maintained by the Windows
    OS
  • Available since the earliest versions of Windows
  • Provides a common mechanism for getting data from
    application to application
  • Also be used for single-program cut/copy and
    paste
  • Many programs have "Cut", "Copy", "Paste" menu
    items
  • Cut or Copy transfers data to clipboard
  • Paste retrieves data from clipboard
  • Clipboard contents are available to any running
    app

3
Global memory allocation functions
  • 1. GlobalAlloc ()
  • hMem GlobalAlloc(allocation_flags,
    numbytes_to_allocate)
  • Allocation_falgs GMEM_MOVEABLE GMEM_ZEROINT
  • Returns a handle to the block that can be used in
    subsequent calls

4
Global memory allocation functions
  • 2- pBlock GlobalLock(hMem)
  • Fixes the block in memory and returns a pointer
    to it
  • Now we can use the returned pointer to read/write
    data from/to the      block of memory.

5
Global memory allocation functions
  • 3- GlobalUnlock(hMem)
  • After we're all done accessing the block of
    memory, we must make it movable again.
  • 4- GlobalFree(hMem)
  • Release memory

6
Clipboard functions
  • OpenClipboard(hWnd)
  • Opens the Clipboard for access by the program
  • EmptyClipboard()
  • Empties the clipboard memory block and frees all
    data currently in the clipboard
  • GetClipboardData(uFormat)
  • Gets a handle to read-only data in the Clipboard.
    The uFormat parameter specifies the data format

7
Clipboard functions
  • SetClipboardData(uFormat, hData)
  • Passes the memory block whose handle is specified
    to the clipboard
  • returns a handle to the data
  • CloseClipboard()
  • Closes the clipboard so that other programs can
    access it

8
Sending Data to the Clipboard
  • Allocate and lock a global memory block
  • Copy the data into the global memory block
  • Unlock the block
  • Open the clipboard with OpenClipboard()
  • Empty the current contents of the clipboard with
    EmptyClipboard()
  • Transfer the block to the Clipboard with
    SetClipboardData()  (Windows now controls the
    block.)
  • Close the clipboard with CloseClipboard().

9
Getting Data from the Clipboard
  • Open the clipboard with OpenClipboard()
  • Get a handle to the clipboard data with
    GetClipboardData()
  • Allocate and lock a memory block to hold the
    clipboard data
  • Lock the clipboard data block
  • Copy the data from the clipboard data block to
    the program's data block
  • Unlock both memory blocks
  • Close the clipboard with CloseClipboard().

10
Some Precautions
  • Close the clipboard as soon as possible after you
    open it
  • After getting data from the clipboard, an
    application must not leave the   block locked
  • After calling SetClipboardData(), an application
    should not use the block,   since it belongs to
    Windows

11
Clipboard Data Formats
  • Custom formats can be created using
    RegisterClipboardFormat().
  • IsClipboardFormatAvailable(uFormat) determines
    if the specified data format is currently in the
    clipboard
  • can be used without opening the clipboard
  • an alternative to checking for a NULL result on
    the call to GetClipboardData()

12
Example
  • Clip1.cpp

13
Multiple Clipboard Data Formats
  • The clipboard can hold one block of data for each
    clipboard format
  • The application that uses the clipboard can check
    each format stored to see if the data is as
    expected or can choose the format it can work
    with "best."

14
Example
  • Clip2.cpp

15
Dynamic Link Libraries
16
DLL
  • Basic building block of Windows
  • provide reusable code that can be shared by many
    applications
  • provide reusable code that can be shared by many
    applications
  • A file containing functions that can be called by
    other programs or DLLs
  • Provides a way for a process to call a function
    that is not part of its   executable code

17
Two methods of linking
  • Implicit linking
  • Also called
  • Load-time linking
  • Static loading
  • Early binding
  • Explicit linking
  • Also called
  • Run-time linking
  • Dynamic loading
  • Late binding

18
Implicit linking
  • DLL is automatically (implicitly) loaded (if not
    already  there) when the program that needs it is
    loaded
  • The links to the DLL functions are established as
    soon as the app and the DLL  have been loaded
    into memory
  • This requires that the executable module of the
    application be built by linking with the DLL's
    import library (.lib file)

19
Explicit linking
  • DLL can be loaded after execution of the app
    begins
  • The application loads the DLL by calling
    LoadLibrary() when it is required
  • When the application no longer needs the DLL it
    should call FreeLibrary()

20
More about DLLs
  • DLL functions are not directly executable, they
    must be called
  • A DLL usually doesn't display a window or receive
    messages
  • A DLL Can be accessed by any number of apps
  • DLL Code is loaded only once any pgm using a ftn
    in an already-loaded

21
Advantages of dynamic linking
  • many processes can use a DLL simultaneously,
    sharing a single   copy in memory saves the
    space, swapping reduced
  • Functions in a DLL can be changed without
    recompiling/relinking the applications  that use
    them
  • Programs written in different programming
    languages can call the same DLL functions

22
Potential disadvantages
  • The app is not self-contained. The DLL module
    must be present
  • Different applications may use the same name for
    their DLLs, or different versions of DLLs with
    the same name can exist

23
DLL operation details
  • DLLs don't have their own stack, instead use the
    stack of the calling program
  • DLLs have their own local heap for static
    variables (may cause problem)
  • If a DLL has to manage data for calling programs,
    it should allocate a separate memory block for
    each calling program's data
  • However, global blocks allocated by a DLL
    "belong" to the calling program

24
Build details
  • The linker should know that DLL functions are
    available
  • this is done by declaring DLL functions as
    "EXPORT
  • EXPORT BOOL CALLBACK func()   
  • EXPORT is a macro
  • define EXPORT extern __declspec(dllexport)
  • This will tell the linker that func() is
    exportable.

25
Writing a DLL, DLLMain
  • Most DLL's contain the function DllMain(), like
    WinMain()
  • First function executed when DLL is loaded into
    memory by Windows
  • Performs initialization stuff
  • Runs only once (when DLL is loaded)
  • int CALLBACK DllMain (HINSTANCE hInstance, DWORD
    fdwReason, PVOID  pvReserved)

26
DLLMain( ) parameters
  • hInstance DLL's instance handle
  • fdwReason one of four values that indicate why
    Windows is calling DllMain().
  • pReserved Reserved

27
DllMain() details
  • If initialization was a success, DllMain()
    return nonzero
  • If initialization failed, Windows will not run
    the program that calls the DLL
  • For most simple cases, all that DllMain() has to
    do is    return(1)

28
Example
  • dllcall.cpp
  • revstr.cpp
Write a Comment
User Comments (0)
About PowerShow.com