大网数据广告活动
网络与域名解析

Interface Development Using Context Handles

· 52CE 我爱测

Typically, you create a context handle by specifying the \ context_handle\ attribute on a type definition in the IDL file.

Typically, you create a context handle by specifying the [context_handle] attribute on a type definition in the IDL file. The type definition also implicitly specifies a context run-down routine, which you must provide. If communication between the client and server breaks down, the server run time invokes this routine to perform any needed cleanup. For more information on context run-down routines, see Server Context Run-down Routine.

An interface that uses a context handle must have a binding handle for the initial binding, which has to take place before the server can return a context handle. You can use an automatic, implicit, or explicit binding handle to create the binding and establish the context.

A context handle must be of the void * type, or a type that resolves to void *. The server program casts it to the required type.

Note:
The use of [in, out] for context handle parameters is discouraged except for routines that close context handles. If context handles parameters marked [in, out] are used, do not pass a NULL or uninitialized context handle from the client to the server. A NULL pointer to a context handle should be passed instead. Please note, context handle parameters marked [in] do not accept NULL pointers.

The following fragment of a sample interface definition shows how a distributed application can use a context handle to have a server open and update a data file for each client.

The interface must contain a remote procedure call to initialize the handle and set it to a non-null value. In this example, the RemoteOpen function performs this operation. It specifies the context handle with an [out] directional attribute. Alternatively, you could return the context handle as the procedure's return value. However in this example, we'll pass the context handle out through the parameter list.

In this example, the context information is a file handle. It keeps track of the current location in the file. The interface packages the file handle as a context handle and passes it as a parameter to remote procedure calls. A structure contains the file name and the file handle.

/* file: cxhndl.idl (fragment of interface definition file) */
typedef [context_handle] void * PCONTEXT_HANDLE_TYPE;
typedef [ref] PCONTEXT_HANDLE_TYPE * PPCONTEXT_HANDLE_TYPE;
 
short RemoteOpen([out] PPCONTEXT_HANDLE_TYPE pphContext,
    [in, string] unsigned char * pszFile);
 
void RemoteRead(
    [in] PCONTEXT_HANDLE_TYPE phContext,
    [out, size_is(cbBuf)] unsigned char achBuf[],
    [in, out] short *pcbBuf);
 
short RemoteClose([in, out] PPCONTEXT_HANDLE_TYPE pphContext);

The RemoteOpen function creates a valid, non-null context handle. It passes the context handle to the client. Subsequent remote procedure calls, such as RemoteRead, use the context handle as an in pointer.

In addition to the remote procedure that initializes the context handle, the interface must contain a procedure that frees the server context and sets context handle to NULL. In the preceding example, the RemoteClose function performs this operation.

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。