---
title: "Dynamic Invocation"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation
language: cpp
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Dynamic Invocation

Programming language: C++. Other languages: [C#](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/java.md), [Python](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/python.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/swift.md).

A typed proxy marshals the parameters of an operation and unmarshals its results with code generated from the Slice definition of the operation. The [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx_ad8e9a5bc7efd45641c1059a3174055ab.html#ad8e9a5bc7efd45641c1059a3174055ab) method of [ObjectPrx](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx.html) invokes an operation by name instead: the application supplies the marshaled in-parameters and receives the marshaled reply. An application that forwards requests it does not unmarshal, or that calls operations without the generated code for the target interface, uses `ice_invoke`.

## Invoking an Operation

`ice_invoke` accepts the following arguments:

- the name of the operation
- the operation mode, `Normal` or `Idempotent`
- an encapsulation holding the marshaled in-parameters
- an optional [request context](https://docs.zeroc.com/ice/3.8/runtime/invocation/request-contexts/cpp.md)

`Ice::ObjectPrx` provides a synchronous [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx_ad8e9a5bc7efd45641c1059a3174055ab.html#ad8e9a5bc7efd45641c1059a3174055ab) and asynchronous [`ice_invokeAsync`](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx_a4c456a506fc4db4c7c8a7503dec13282.html#a4c456a506fc4db4c7c8a7503dec13282) overloads that return a future or accept callbacks.

### Operation Mode

The operation mode is `Idempotent` for a Slice operation declared [idempotent](https://docs.zeroc.com/ice/3.8/slice/operations/cpp.md#idempotent-operations), and `Normal` otherwise. The Ice runtime uses this mode to decide whether it can [retry](https://docs.zeroc.com/ice/3.8/runtime/invocation/automatic-retries/cpp.md#automatic-retries-for-idempotent-operations) an invocation that fails after the request was sent, and sends the mode with the request. Pass `Idempotent` when you know the operation is idempotent, or when you forward a request that arrived with this mode.

## Marshaling the In-Parameters

You marshal the in-parameters into an [encapsulation](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md#encoding-for-encapsulations): the required parameters in the order of their declaration, followed by the optional parameters in the order of their tags, each marshaled as described in [Ice Encoding](https://docs.zeroc.com/ice/3.8/encoding.md). For an operation without in-parameters, you can pass an empty byte sequence; the Ice runtime then sends an empty encapsulation.

You marshal the in-parameters with an `Ice::OutputStream`. The following example invokes the `greet` operation of the `Greeter` interface, which takes a `string` parameter:

```cpp
Ice::ObjectPrx greeter{communicator, "greeter:tcp -h localhost -p 4061"};

Ice::OutputStream outputStream{communicator};
outputStream.startEncapsulation();
outputStream.write("alice");
outputStream.endEncapsulation();
std::vector<std::byte> inParams;
outputStream.finished(inParams);

std::vector<std::byte> outParams;
bool ok = greeter.ice_invoke("greet", Ice::OperationMode::Normal, inParams, outParams);
```

[Input and Output Streams](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/cpp.md) shows how to read and write each kind of Slice type with these streams.

## Reading the Reply

For a twoway invocation, `ice_invoke` returns a success flag and an encapsulation:

- When the flag is `true`, the operation completed successfully, and the encapsulation holds the required out-parameters, followed by the required return value, followed by the optional out-parameters and optional return value in the order of their tags.
- When the flag is `false`, the operation completed with a user exception, and the encapsulation holds the [marshaled user exception](https://docs.zeroc.com/ice/3.8/encoding/data-encoding-for-exceptions.md).

For any other failure, `ice_invoke` throws a [local or dispatch exception](https://docs.zeroc.com/ice/3.8/runtime/local-and-dispatch-exceptions/cpp.md).

For a oneway or datagram invocation, `ice_invoke` returns `true` and an empty byte sequence once the transport accepts the request. For a batch oneway or batch datagram invocation, it returns `true` and an empty byte sequence once it adds the request to the batch; the Ice runtime sends the request when it [flushes](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocations/cpp.md) the batch.

You unmarshal the reply with an `Ice::InputStream`. `throwException` unmarshals a user exception and throws it:

```cpp
Ice::InputStream inputStream{communicator, outParams};
inputStream.startEncapsulation();
if (ok)
{
    std::string greeting;
    inputStream.read(greeting);
    inputStream.endEncapsulation();
}
else
{
    try
    {
        inputStream.throwException();
    }
    catch (const Ice::UserException&)
    {
        inputStream.endEncapsulation();
        throw;
    }
}
```

With the callback overload of `ice_invokeAsync`, the Ice runtime does not call `response` for a oneway or datagram invocation: it calls `sent` once the transport accepts the request. When it adds a request to a batch, it calls none of the callbacks.

## Forwarding Requests

An application can forward the requests that its object adapter receives with `ice_invoke`, without unmarshaling their parameters. The forwarding code passes the encapsulation of the in-parameters from the incoming request to `ice_invoke` on the target proxy, and builds the response from the success flag and the encapsulation that `ice_invoke` returns. The operation name, mode and [context](https://docs.zeroc.com/ice/3.8/runtime/invocation/request-contexts/cpp.md) of the outgoing request are the forwarder's to set: a forwarder usually passes on those of the incoming request, and an intermediary such as Glacier2 adds or removes context entries along the way.

A forwarding dispatcher derives from `Ice::Object` and overrides `dispatch`.

The [Ice forwarder demo](https://github.com/zeroc-ice/ice-demos/tree/3.8/cpp/Ice/forwarder) provides a complete example of a forwarding server in C++.

## See Also

- [Dispatcher API](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/cpp.md)
- [Basic Data Encoding](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md)
- [Reply Message Body](https://docs.zeroc.com/ice/3.8/protocol/protocol-messages/cpp.md#reply-message-body)
