Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Load Vb6 DLL Function



  • 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


  • Lifetime Qt Champion

    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


  • Moderators

    @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.


  • Lifetime Qt Champion

    @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 ?


  • Moderators

    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.



  • 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)



  • 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


  • Lifetime Qt Champion

    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.



  • @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


  • Lifetime Qt Champion

    @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 ;)



  • @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()));
    }
    

  • Lifetime Qt Champion

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


  • Moderators

    @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.



  • 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?


Log in to reply