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. Load Vb6 DLL Function
Forum Updated to NodeBB v4.3 + New Features

Load Vb6 DLL Function

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 3.3k Views 2 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.
  • N Offline
    N Offline
    newbieQTDev
    wrote on 3 Apr 2018, 13:50 last edited by
    #1

    Hi.
    I need to load a vb6 dll and run a function.
    This is an example where the function return a int but i want return a vb6 string how i do?

    #include <QCoreApplication>
    #include <QLibrary>
    #include <QDebug>
    
    typedef int (*call_func)();
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QLibrary library( "C:\\Windows\\SysWOW64\\myDLL.dll" );
        library.load();
        if( !library.isLoaded() )
        {
            qDebug() << "Cannot load library.";
            return 0;
        }
        else qDebug()<<"I have load a library";
        call_func func = (call_func)library.resolve( "myIntFunction" );
        if( func)
        {
            qDebug()<<  func() << endl;
        } else qDebug() << "I not call function"<<endl;
    
        return a.exec();
    }
    
    

    Every help is appreciated. Thanks

    A K 2 Replies Last reply 3 Apr 2018, 20:18
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 3 Apr 2018, 14:23 last edited by
      #2

      Hi
      Since VB6 string is a vb type, im not sure you can use it directly.
      https://stackoverflow.com/questions/5231879/vb6-how-to-pass-strings-to-a-dll-written-in-c-through-a-tlb-file

      So it seems you should use LPSTR
      and declare the stings in VB as
      Dim file_name_in As String * 256

      K 1 Reply Last reply 3 Apr 2018, 20:13
      2
      • M mrjj
        3 Apr 2018, 14:23

        Hi
        Since VB6 string is a vb type, im not sure you can use it directly.
        https://stackoverflow.com/questions/5231879/vb6-how-to-pass-strings-to-a-dll-written-in-c-through-a-tlb-file

        So it seems you should use LPSTR
        and declare the stings in VB as
        Dim file_name_in As String * 256

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 3 Apr 2018, 20:13 last edited by
        #3

        @mrjj said in Load Vb6 DLL Function:

        So it seems you should use LPSTR

        I find it hard to believe the function should return a char *, but it's certainly not impossible.

        @newbieQTDev said in Load Vb6 DLL Function:

        This is an example where the function return a int but i want return a vb6 string how i do?

        You need to find what's the function prototype exactly, i.e. what is the C-style return type you should expect. I'm not into VB, but if it's LPSTR, as @mrjj mentions, then you're not only responsible to get the return value, but also probably to clean up the memory afterwards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • N newbieQTDev
          3 Apr 2018, 13:50

          Hi.
          I need to load a vb6 dll and run a function.
          This is an example where the function return a int but i want return a vb6 string how i do?

          #include <QCoreApplication>
          #include <QLibrary>
          #include <QDebug>
          
          typedef int (*call_func)();
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              QLibrary library( "C:\\Windows\\SysWOW64\\myDLL.dll" );
              library.load();
              if( !library.isLoaded() )
              {
                  qDebug() << "Cannot load library.";
                  return 0;
              }
              else qDebug()<<"I have load a library";
              call_func func = (call_func)library.resolve( "myIntFunction" );
              if( func)
              {
                  qDebug()<<  func() << endl;
              } else qDebug() << "I not call function"<<endl;
          
              return a.exec();
          }
          
          

          Every help is appreciated. Thanks

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 3 Apr 2018, 20:18 last edited by
          #4

          @newbieQTDev

          just to add to @mrjj and @kshegunov, the interesting thing to know is the function signature, as typically written in a C header file.

          is there any documentation for that library or did you write it yourself ?

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • N newbieQTDev
            3 Apr 2018, 13:50

            Hi.
            I need to load a vb6 dll and run a function.
            This is an example where the function return a int but i want return a vb6 string how i do?

            #include <QCoreApplication>
            #include <QLibrary>
            #include <QDebug>
            
            typedef int (*call_func)();
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                QLibrary library( "C:\\Windows\\SysWOW64\\myDLL.dll" );
                library.load();
                if( !library.isLoaded() )
                {
                    qDebug() << "Cannot load library.";
                    return 0;
                }
                else qDebug()<<"I have load a library";
                call_func func = (call_func)library.resolve( "myIntFunction" );
                if( func)
                {
                    qDebug()<<  func() << endl;
                } else qDebug() << "I not call function"<<endl;
            
                return a.exec();
            }
            
            

            Every help is appreciated. Thanks

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 3 Apr 2018, 20:20 last edited by
            #5

            Something which @aha_1980's comment reminded me of ... you could in principle decode the decorated name from the binary. I will explain how if it gets to this, however it's much simpler if you have docs or otherwise information what's the C equivalent of the VB types.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • N Offline
              N Offline
              newbieQTDev
              wrote on 4 Apr 2018, 11:21 last edited by
              #6

              Thanks for all.
              I will try to recover the documentation of the dll. I found some information about the conversion between the vb and c ++ data types.
              The equivalent type should be BSTR.
              Unraveling Strings in Visual C++
              My work scenario is quite strange. I have to call in C ++ (QT) some functions of a dll written for vb6. I am migrating an application from vb6 to C ++ (QT). My vb6 application uses the dll. Then i will include the functionality of the dll in my C ++ application (QT)

              1 Reply Last reply
              0
              • N Offline
                N Offline
                newbieQTDev
                wrote on 4 Apr 2018, 14:41 last edited by
                #7

                As suggested by kshegunov I found the prototype of the function. this is

                STDMETHOD(get_SoftwareCode)(/*[out, retval]*/ BSTR *pVal);
                

                Which is the right method for the call?
                Thanks

                K 1 Reply Last reply 5 Apr 2018, 09:43
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 4 Apr 2018, 19:34 last edited by mrjj 4 Apr 2018, 19:35
                  #8

                  Hi
                  Does the documentation mention who has ownership of the pVal?

                  Anyway, you can try
                  QString string("WHATEVER");
                  BSTR bstr = SysAllocString(string.utf16());
                  // use...
                  SysFreeString(bstr); // unless DLL owns it..

                  Notice this is native API and not Qt. So you might need extra includes
                  and/or link to libs.

                  Note that it seems to be other way. you call the DLL and it put data in the pVal.
                  In that case, you might need t know maximum it needs.

                  hskoglundH 1 Reply Last reply 4 Apr 2018, 19:53
                  2
                  • M mrjj
                    4 Apr 2018, 19:34

                    Hi
                    Does the documentation mention who has ownership of the pVal?

                    Anyway, you can try
                    QString string("WHATEVER");
                    BSTR bstr = SysAllocString(string.utf16());
                    // use...
                    SysFreeString(bstr); // unless DLL owns it..

                    Notice this is native API and not Qt. So you might need extra includes
                    and/or link to libs.

                    Note that it seems to be other way. you call the DLL and it put data in the pVal.
                    In that case, you might need t know maximum it needs.

                    hskoglundH Offline
                    hskoglundH Offline
                    hskoglund
                    wrote on 4 Apr 2018, 19:53 last edited by
                    #9

                    @mrjj For an [out] COM BSTR, the caller of the function (i.e. Qt, not VB6) assumes responsibility of the memory management for the BSTR MSDN doc

                    M 1 Reply Last reply 4 Apr 2018, 19:58
                    4
                    • hskoglundH hskoglund
                      4 Apr 2018, 19:53

                      @mrjj For an [out] COM BSTR, the caller of the function (i.e. Qt, not VB6) assumes responsibility of the memory management for the BSTR MSDN doc

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 4 Apr 2018, 19:58 last edited by
                      #10

                      @hskoglund
                      haha cool you answered. i was actually thinking of you as if anyone knew what vb6 likes in 2018
                      it might be you :)
                      Yes so get_SoftwareCode wants a buffer and the caller must free it.
                      Do you know a Qt way of constructing a BSTR besides the SysAllocString ?
                      Im afraid the details escapes me after a decade ;)

                      hskoglundH 1 Reply Last reply 4 Apr 2018, 20:45
                      0
                      • M mrjj
                        4 Apr 2018, 19:58

                        @hskoglund
                        haha cool you answered. i was actually thinking of you as if anyone knew what vb6 likes in 2018
                        it might be you :)
                        Yes so get_SoftwareCode wants a buffer and the caller must free it.
                        Do you know a Qt way of constructing a BSTR besides the SysAllocString ?
                        Im afraid the details escapes me after a decade ;)

                        hskoglundH Offline
                        hskoglundH Offline
                        hskoglund
                        wrote on 4 Apr 2018, 20:45 last edited by
                        #11

                        @mrjj Had a look inside Qt's sources, found this function in qt-everywhere-src-5.10.1/qtbase/src/plugins/platforms/windows/accessible/comutils.h:

                        BSTR QStringToBSTR(const QString &str)
                        {
                            return SysAllocStringLen(reinterpret_cast<const OLECHAR *>(str.unicode()), UINT(str.length()));
                        }
                        
                        M 1 Reply Last reply 4 Apr 2018, 20:52
                        3
                        • hskoglundH hskoglund
                          4 Apr 2018, 20:45

                          @mrjj Had a look inside Qt's sources, found this function in qt-everywhere-src-5.10.1/qtbase/src/plugins/platforms/windows/accessible/comutils.h:

                          BSTR QStringToBSTR(const QString &str)
                          {
                              return SysAllocStringLen(reinterpret_cast<const OLECHAR *>(str.unicode()), UINT(str.length()));
                          }
                          
                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 4 Apr 2018, 20:52 last edited by
                          #12

                          @hskoglund
                          Oh, good found. That should make it just work !

                          1 Reply Last reply
                          0
                          • N newbieQTDev
                            4 Apr 2018, 14:41

                            As suggested by kshegunov I found the prototype of the function. this is

                            STDMETHOD(get_SoftwareCode)(/*[out, retval]*/ BSTR *pVal);
                            

                            Which is the right method for the call?
                            Thanks

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 5 Apr 2018, 09:43 last edited by kshegunov 4 May 2018, 09:44
                            #13

                            @newbieQTDev said in Load Vb6 DLL Function:

                            STDMETHOD(get_SoftwareCode)(/*[out, retval]*/ BSTR *pVal);
                            

                            This is a method definition, according to MSDN, which means it applies to an object, so you also need to have the actual object to use it.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              newbieQTDev
                              wrote on 5 Apr 2018, 13:49 last edited by
                              #14

                              Hi kshegunov.
                              I am sorry.
                              I may not have explained my problem well.
                              I'm trying to use a dll that was written using COM technology. It was written in C but to be used in VB6.
                              The signature of the function I inserted in the post is one of the many methods that the dll provides. After your consideration maybe the code I used is not appropriate to my case?

                              1 Reply Last reply
                              0

                              1/14

                              3 Apr 2018, 13:50

                              • Login

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