How to use variant

Naveen Verma ernaveenverma at gmail.com
Mon Jul 9 01:34:03 PDT 2007


Hi,

Thanks a lot for your help.

It was easy, but I am still stuck in an issue. In another function which
return and array of struct and one of the struct member is variant. When I
call the function from another program. I receive "DBus::Error" , "what():
unknown type". The interesting part is I did not receive this error if I
return only structure not the array of structure, or return the array of
structure without variant as members.

So something is wrong in combination of array and variant. The function
example which gives and error is as:

std::vector< DBus::Struct<DBus::String, DBus::UInt32,
             DBus::String, DBus::Variant> >
CManager::GetParameters(const DBus::String &pto)
{
  std::cerr << "Method GetParameters\n";

  DBus::Struct< DBus::String, DBus::UInt32,
    DBus::String,DBus::Variant > param;

  param._1 = "protocol";
  param._2 = 1;
  param._3 = "string";

  // Add the variant
  DBus::MessageIter i = param._4.writer();
  i << DBus::String("None");

  std::vector < DBus::Struct< DBus::String, DBus::UInt32,
    DBus::String,DBus::Variant> > temp;
  temp.push_back(param);

  return temp;
}

In this function if I removed variant from the structure or remove vector
and just keep struct then it works fine, otherwise the remote caller
function terminate with "DBus::Error" , "what(): unknown type" error.

Could anyone please help me to figure out the problem?

-Thanks & Regards
Naveen

On 7/9/07, P. Durante <shackan at gmail.com> wrote:
>
> Hi,
>
> On 7/8/07, Naveen Verma <ernaveenverma at gmail.com> wrote:
> > Hi,
> >
> > I have a function which return a variant. But whenever I am trying to
> call
> > the function. I receive an error and program terminates. I am unable to
> > understand what is the reason.
> > May be I am not creating the variant in correct way. Could anyone please
> > check the code and error below, and give me an idea what is wrong?
> >
> >
> > DBus::Variant  Test::getTestValue()
> > {
> >      DBus::Variant var;
> >      DBus::MessageIter i = var.writer();
> >      MessageIter j = i.new_variant("s");
> >      j.append_string("Test");
> >      i.close_container(j);
> >
> >      return var;
> > }
> >
> >
> > And the error message is:
> >
> > process 6933: arguments to
> > dbus_message_iter_open_container() were incorrect,
> > assertion "(type == DBUS_TYPE_STRUCT && contained_signature == NULL) ||
> > (type == DBUS_TYPE_DICT_ENTRY && contained_signature == NULL) ||
> > contained_signature != NULL" failed in file dbus-message.c line 2330.
> > This is normally a bug in some application using the D-Bus library.
> >
>
> it's actually simpler than that
>
> DBus::Variant  Test::getTestValue()
> {
>         DBus::Variant var;
>         DBus::MessageIter i = var.writer();
>         i.append_string("hello");
>         return var;
> }
>
> or
>         DBus::MessageIter i = var.writer();
>         i << DBus::String("hello")
> or
>         DBus::MessageIter i = var.writer();
>         i << std::string("hello");
>
> of course, if you want to stuff more values into a variant, you'll use
> a structure or an array accordingly
>
> regards,
> Paolo
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20070709/e300b033/attachment-0001.htm 


More information about the dbus mailing list