Glib g_dbus_connection_call_sync

#include <iostream>
#include <gio/gio.h>
#include <glib.h>
#include <bits/stdc++.h>
using namespace std;

static GDBusNodeInfo *introspection_data = NULL;

/* Introspection data for the service we are exporting */
static const gchar introspection_xml[] =
    "<node>"
    "  <interface name='org.gtk.GDBus.TestPeerInterface'>"
    "    <method name='HelloWorld'>"
    "      <arg type='s' name='greeting' direction='in'/>"
    "      <arg type='s' name='response' direction='out'/>"
    "    </method>"
    "  </interface>"
    "</node>";


bool flag=true;

static void
handle_method_call (GDBusConnection       *connection,
                   const gchar           *sender,
                   const gchar           *object_path,
                   const gchar           *interface_name,
                   const gchar           *method_name,
                   GVariant              *parameters,
                   GDBusMethodInvocation *invocation,
                   gpointer               user_data)
{
    cout<<__PRETTY_FUNCTION__<<" "<<method_name<<endl;
}

static const GDBusInterfaceVTable interface_vtable =
    {
        handle_method_call,
        NULL,
        NULL,
        { 0 }
};


int main()
{
    GError *error = NULL;
    GDBusConnection *connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
    if (!connection) {
        g_error("Error connecting to session bus: %s", error->message);
    }

    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
    g_assert (introspection_data != NULL);

    auto registration_id = g_dbus_connection_register_object (connection,
                                                        "/org/gtk/GDBus/TestObject",
                                                        introspection_data->interfaces[0],
                                                        &interface_vtable,
                                                        NULL,  /* user_data */
                                                        NULL,  /* user_data_free_func */
                                                        NULL); /* GError** */

    while(flag)
    {
        g_main_context_iteration(g_main_context_default(),false);


        gchar *greeting = "test";
        auto value = g_dbus_connection_call_sync (connection,
                                            NULL, /* bus_name */
                                            "/org/gtk/GDBus/TestObject",
                                            "org.gtk.GDBus.TestPeerInterface",
                                            "HelloWorld",
                                            g_variant_new ("(s)", greeting),
                                            G_VARIANT_TYPE ("(s)"),
                                            G_DBUS_CALL_FLAGS_NONE,
                                            -1,
                                            NULL,
                                            &error);

        if(error)
        {
            cout<<error->message<<endl;
            g_free(error);
            error=0;
        }

    }
    return 0;
}

The above code fails to call HelloWorld , this code is stripped from gdbus-example-peer.c
I am unable to figure out why the method is not getting called.

I get GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod

it works when I use:

g_bus_own_name(G_BUS_TYPE_SESSION,“com.example.MyApp1212”,G_BUS_NAME_OWNER_FLAGS_NONE,0,0,0,0,0);
and
g_dbus_connection_call_sync (connection,
“com.example.MyApp1212”,

You’re passing bus_name = NULL into g_dbus_connection_call_sync; you should only really do that when using a broker-less / peer-to-peer D-Bus connection. But you’re doing this on a session bus connection, which of course has a broker.

The D-Bus spec does say what happens in this case:

When the message bus receives a method call, if the DESTINATION field is absent, the call is taken to be a standard one-to-one message and interpreted by the message bus itself. For example, sending an org.freedesktop.DBus.Peer.Ping message with no DESTINATION will cause the message bus itself to reply to the ping immediately; the message bus will not make this message visible to other applications.

So it’s essentially the same as passing bus_name = "org.freedesktop.DBus". And of course the broker doesn’t have an object at /org/gtk/GDBus/TestObject, hence you’re getting the error.