您的位置:首页 > 其它

孙鑫VC学习笔记:第十七讲 (一) 用剪贴板实现进程间的通信

2007-12-25 14:33 519 查看
孙鑫教学视频笔记(17)进程间通信
---------------------------------------------------------------------------------
进程间通信的四种方式
剪贴板
匿名管道
命名管道
邮槽

---------------------------------------------------------------------------------
剪贴板编程

1.将数据存入剪贴板:

相关函数:
---------------------------------------------------------------------------------
CWnd::OpenClipboard
BOOL OpenClipboard( );
Return Value
Nonzero if the Clipboard is opened via CWnd, or 0 if another application or window has the Clipboard open.
Remarks
Opens the Clipboard. Other applications will not be able to modify the Clipboard until the CloseClipboard Windows function is called.
The current CWnd object will not become the owner of the Clipboard until the EmptyClipboard Windows function is called.
打开了剪贴板之后,还必须调用EmptyClipboard ,才能使当前窗口拥有剪贴板。

EmptyClipboard
The EmptyClipboard function empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the window that currently has the clipboard open.
本函数将清空剪贴板释放数据句柄,然后将剪贴板的所有权分配给当前打开它的程序。
BOOL EmptyClipboard(VOID)

Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Before calling EmptyClipboard, an application must open the clipboard by using the OpenClipboard function. If the application specifies a NULL window handle when opening the clipboard, EmptyClipboard succeeds but sets the clipboard owner to NULL.

SetClipboardData
The SetClipboardData function places data on the clipboard in a specified clipboard format. The window must be the current clipboard owner, and the application must have called the OpenClipboard function. (When responding to the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages, the clipboard owner must not call OpenClipboard before calling SetClipboardData.)

HANDLE SetClipboardData(
UINT uFormat, // clipboard format
HANDLE hMem // data handle
);

Parameters
uFormat
Specifies a clipboard format. This parameter can be a registered format or any of the standard clipboard formats. For more information, see Registered Clipboard Formats and Standard Clipboard Formats.
hMem
Handle to the data in the specified format. This parameter can be NULL, indicating that the window provides data in the specified clipboard format (renders the format) upon request. If a window delays rendering, it must process the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages.
After SetClipboardData is called, the system owns the object identified by the hMem parameter. The application can read the data, but must not free the handle or leave it locked. If the hMem parameter identifies a memory object, the object must have been allocated using the GlobalAlloc function with the GMEM_MOVEABLE and GMEM_DDESHARE flags.
Return Values
If the function succeeds, the return value is the handle of the data.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
The uFormat parameter can identify a registered clipboard format, or it can be one of the standard clipboard formats. For more information, see Registered Clipboard Formats and Standard Clipboard Formats.
The system performs implicit data format conversions between certain clipboard formats when an application calls the GetClipboardData function. For example, if the CF_OEMTEXT format is on the clipboard, a window can retrieve data in the CF_TEXT format. The format on the clipboard is converted to the requested format on demand. For more information, see Synthesized Clipboard Formats.

GlobalAlloc
The GlobalAlloc function allocates the specified number of bytes from the heap. Win32 memory management does not provide a separate local heap and global heap.
Note The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE, the clipboard functions, and OLE data objects.
This function is provided only for compatibility with 16-bit versions of Windows.
HGLOBAL GlobalAlloc(
UINT uFlags,// 指定如何分配内存,NULL则用缺省的GMEM_FIXED
DWORD dwBytes // number of bytes to allocate
);

Parameters
uFlags
Specifies how to allocate memory. If zero is specified, the default is GMEM_FIXED. This value can be one or more of the following flags, except for the incompatible combinations that are specifically noted.
可以有以下值的组合
GMEM_FIXED
如果指定为GMEM_FIXED,那么GlobalAlloc 分配内存将其地址直接转换为32位的HGLOBAL 的值返回。 也就是说直接返回内存分配的地址。
GMEM_MOVEABLE
如果指定的是GMEM_MOVEABLE,那么返回的HGLOBAL 的值不是实际分配的地址,而是一个指向进程中句柄表条目的指针,该条目包含实际分配的指针。剪贴板中要用这个值。
分配可移动的内存. In Win32, memory blocks are never moved in physical memory, but they can be moved within the default heap. 在Win32中,物理内存块是不能被移动的,但是它们可以在缺省的堆中被移动。当我们创建进程的时候,系统要为进程分配缺省的堆。
The return value is a handle to the memory object. To translate the handle into a pointer, use the GlobalLock function.
This flag cannot be combined with the GMEM_FIXED flag.
GPTR
Combines the GMEM_FIXED and GMEM_ZEROINIT flags.
GHND
Combines the GMEM_MOVEABLE and GMEM_ZEROINIT flags.
GMEM_DDESHARE
GMEM_SHARE
This flag is provided primarily for compatibility with 16-bit Windows. However, this flag may be used by some applications to enhance the performance of DDE operations and therefore can be specified if the memory is to be used for DDE. .
GMEM_DISCARDABLE
Ignored. This flag is provided only for compatibility with 16-bit Windows.
In Win32, you must explicitly call the GlobalDiscard function to discard a block.
This flag cannot be combined with the GMEM_FIXED flag.
GMEM_LOWER
Ignored. This flag is provided only for compatibility with 16-bit Windows.
GMEM_NOCOMPACT
Ignored. This flag is provided only for compatibility with 16-bit Windows.
GMEM_NODISCARD
Ignored. This flag is provided only for compatibility with 16-bit Windows.
GMEM_NOT_BANKED
Ignored. This flag is provided only for compatibility with 16-bit Windows.
GMEM_NOTIFY
Ignored. This flag is provided only for compatibility with 16-bit Windows.
GMEM_ZEROINIT
Initializes memory contents to zero.
dwBytes
Specifies the number of bytes to allocate. If this parameter is zero and the uFlags parameter specifies the GMEM_MOVEABLE flag, the function returns a handle to a memory object that is marked as discarded.

GlobalLock
The GlobalLock function locks a global memory object and returns a pointer to the first byte of the object's memory block.
This function is provided only for compatibility with 16-bit versions of Windows.
LPVOID GlobalLock(
HGLOBAL hMem // handle to the global memory object
);

Note The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions.
Remarks
The internal data structures for each memory object include a lock count that is initially zero.
每一个内存对象的内存数据结构包含了一个初始化为0的锁计数。
For movable memory objects, GlobalLock increments the count by one, and the GlobalUnlock function decrements the count by one.
对于GlobalLock将锁计数加1,GlobalUnLock将锁计数减1。
For each call that a process makes to GlobalLock for an object, it must eventually call GlobalUnlock. Locked memory will not be moved or discarded, unless the memory object is reallocated by using the GlobalReAlloc function.
加锁的内存不能移动和销毁,除非调用GlobaReAlloc重新分配了内存对象。
The memory block of a locked memory object remains locked until its lock count is decremented to zero, at which time it can be moved or discarded.
被锁定的内存将保持锁定直到锁计数为0,这时才可以移动或者废弃它。
Memory objects allocated with the GMEM_FIXED flag always have a lock count of zero. For these objects, the value of the returned pointer is equal to the value of the specified handle.
If the specified memory block has been discarded or if the memory block has a zero-byte size, this function returns NULL.
Discarded objects always have a lock count of zero.

BOOL CloseClipboard( VOID );
The CloseClipboard function closes the clipboard.

BOOL EmptyClipboard( VOID );
The EmptyClipboard function empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the window that currently has the clipboard open.

---------------------------------------------------------------------------------
2.剪贴板接收端编程

检查数据是否符合要求:
BOOL IsClipboardFormatAvailable(UINT format);
The IsClipboardFormatAvailable function determines whether the clipboard contains data in the specified format.

从剪贴板读取数据:

---------------------------------------------------------------------------------
相关函数:
GetClipboardData
从剪贴板中获取指定格式的数据。
The GetClipboardData function retrieves data from the clipboard in a specified format.
The clipboard must have been opened previously.
HANDLE GetClipboardData(
UINT uFormat // clipboard format
);

IsClipboardFormatAvailable
用来检测剪贴板中有没有指定格式的数据,如果有数据则返回非0值,如果剪贴板格式不可用则返回0值。
The IsClipboardFormatAvailable function determines whether the clipboard contains data in the specified format.
BOOL IsClipboardFormatAvailable(
UINT format // clipboard format
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: