Windows API Reference

RpcBindingCreate Function

Defined in: Rpcrt4.dll

Tip: This topic is part of the Remote Procedure Call (RPC) documentation. Understanding RPC concepts is beneficial for using this function effectively.

The RpcBindingCreate function creates a binding handle for the specified protocol sequence and endpoint.

Syntax

RPC_STATUS RpcBindingCreate(
      [in] handle_t      ObjectUuid,
      [in] unsigned char *ProtocolSequence,
      [in] unsigned char *NetworkAddress,
      [in] unsigned char *Endpoint,
      [in] unsigned char *Options,
      [out] RPC_BINDING_HANDLE *BindingHandle
    );
    

Parameters

Return Value

The function returns RPC_S_OK on success, or one of the following error codes on failure:

Remarks

The RpcBindingCreate function is used to create a client-side binding handle. This handle represents a connection to a remote procedure call server. The function takes various parameters to specify how the connection should be established, including the protocol sequence, network address, and endpoint.

The ProtocolSequence parameter is crucial and defines the transport layer used for communication. Common examples include:

The NetworkAddress and Endpoint parameters are used to locate the server. If they are NULL, the RPC runtime may use default values or rely on other mechanisms like name resolution services.

After successfully creating a binding handle, it should be released using the RpcBindingFree function when it is no longer needed to prevent resource leaks.

Example

#include <windows.h>
#include <rpcdce.h>
#include <stdio.h>

#pragma comment(lib, "rpcrt4.lib")

int main() {
    RPC_STATUS status;
    RPC_BINDING_HANDLE hBinding = NULL;
    unsigned char *pszProtocolSequence = (unsigned char *)"ncacn_ip_tcp";
    unsigned char *pszNetworkAddress = NULL; // Use default or DNS resolution
    unsigned char *pszEndpoint = (unsigned char *)"8080"; // Example endpoint
    unsigned char *pszOptions = NULL;

    printf("Creating binding handle...\n");

    // Create the binding handle
    status = RpcBindingCreate(
        NULL,                 // ObjectUuid (not used for basic binding)
        pszProtocolSequence,
        pszNetworkAddress,
        pszEndpoint,
        pszOptions,
        &hBinding
    );

    if (status == RPC_S_OK) {
        printf("Binding handle created successfully.\n");
        // Now you can use hBinding to make RPC calls
        // For example: RpcStringBindingInvoke, etc.

        // Free the binding handle when done
        status = RpcBindingFree(&hBinding);
        if (status == RPC_S_OK) {
            printf("Binding handle freed successfully.\n");
        } else {
            fprintf(stderr, "Error freeing binding handle: %lu\n", status);
        }
    } else {
        fprintf(stderr, "Failed to create binding handle. Error: %lu\n", status);
    }

    return 0;
}
        

See Also

RpcBindingFree
RpcStringBindingCompose
RpcBindingFromStringBinding