Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Dynamic cast to void pointer
Forum Updated to NodeBB v4.3 + New Features

[Solved]Dynamic cast to void pointer

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jeremy_kJ Offline
    jeremy_kJ Offline
    jeremy_k
    wrote on last edited by
    #5

    For the sake of clarity, try separating the call to QAxBase::dynamicCall() from the call to QVariant::value(). Then verify that the QVariant is valid, at least for development purposes. Finally, cast it to a void pointer, and inspect it again.

    QAxBase::dynamicCall() is documented as returning an invalid QVariant if the call fails, or if the called method doesn't return a value. Maybe something is going wrong prior to the cast.

    Asking a question about code? http://eel.is/iso-c++/testcase/

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ashokb
      wrote on last edited by
      #6

      Hi jeremy_k

      I have verified that QVariant is valid. qDebug prints this,
      QVariant(int, 419171632).

      that means dynamic call is working, there is issue with casting.
      So, i have a QVariant and i want to convert it to void *.
      I am doing
      @
      HANDLE hBlock = QVariant.value<void *>;
      @
      where QVariant is QVariant(int, 419171632)
      i get 0x0 in hBlock.

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #7

        Try a reinterpret_cast<void *>

        @#include <cstdio>
        #include <QVariant>

        int main(int argc, char *argv[])
        {
        QVariant var(0x12345678);
        void * ptr1 = var.value<void *>();
        void * ptr2 = reinterpret_cast<void *>(var.value<int>());
        printf("%p %p\n", ptr1, ptr2);
        return 0;
        }@

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ashokb
          wrote on last edited by
          #8

          Hi

          That works !!!
          value<void *>() still returns zero.

          qDebug Output:

          hBlock:QVariant(int, 418057520)
          ptr1: 0x0
          ptr2: 0x18eb0d30

          Thanks...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ambershark
            wrote on last edited by
            #9

            Sorry about the 5 thing, I missed a parenthesis when I was reading the code. :)

            Glad the reinterpret_cast worked for you though.

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            0
            • A Offline
              A Offline
              ashokb
              wrote on last edited by
              #10

              Hi, ambershark
              You should not sorry for that , actually i should sorry for posting some unreadable code.

              1. reinterpret_cast worked for me, but yesterday i was reading about it and found that its dangerous to use.

              2. If using QVariant.value<>() is a correct way to convert QVariant to any other type then why it is not working in my case?

              3. On many forums its recommended not to use reinterpret_cast, then what might be the other way in this case(QVariant to void *)?

              1 Reply Last reply
              0
              • jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #11

                reinterpret_cast tells the compiler "trust me, I know what I'm doing". There is no other options (that I know of) for transforming to or from void *. Nothing inherits from void *, and it doesn't inherit from anything. There is no inherently meaningful way to convert it to a pointer to a valid object without risking converting it into a pointer to an invalid object.

                Because of it's lack of meaning, arithmetic and dereferencing void * are also meaningless.

                So no, if you don't understand what it is or how it can be used, staying away from both reinterpret_cast and void * is a good idea. The same caveat goes for C-style casting.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #12

                  I was slightly hasty dismissing static_cast<void *>. That works from a pointer to void *, and back again.

                  More detail: http://www.cplusplus.com/doc/tutorial/typecasting/

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ashokb
                    wrote on last edited by
                    #13

                    Hi jeremy_k

                    Thanks for useful info and help....

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      ambershark
                      wrote on last edited by
                      #14

                      I agree with what jeremy_k said. I use reinterpret_cast a bit in code. It can be dangerous because it has no type checking. When dealing with void *s though there is rarely any type checking and using reinterpret_cast is just fine. Like jeremy said it is basically telling the compiler you know what is in that data and want it cast to a certain type.

                      The "dangerous" part comes in if you are not paying attention as a programmer. If you know for sure your pointer is a valid one of class X then a reinterpret_cast is just fine.

                      If you think of it in C style it is the same as saying:

                      @
                      void myVoid = (void)new MyClass;
                      MyClass *c = (MyClass *)myVoid;
                      @

                      Both of those above examples are the same as reinterpret_cast's in c++. In fact you can use the C style casting method in C++ just fine.

                      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        ashokb
                        wrote on last edited by
                        #15

                        Hi
                        Got it...
                        Thanks to both of you.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved