VB - Linking a DLL in implicit way

后端 未结 3 1159
日久生厌
日久生厌 2020-12-02 02:25

I\'m working on a VB6 graphic interface and I need to make an implicit linking to a DLL.

The motivation for this comes from my previous question. The DLL in question

相关标签:
3条回答
  • 2020-12-02 02:54

    Create an IDL file for your DLL that describes your exported functions in a moduleclause.

    Compile with the MIDL compiler and reference the resulting tlb file from your VB6 project (Project - References).
    And remove all Declare Functions.

    The tlb file is only used for compilation (in this case), you don't have to include it into the setup.

    0 讨论(0)
  • 2020-12-02 03:09

    Finally I was able to solve the problem thanks to GSerg and David Heffernan help.

    Here the IDL to be used to generate the .tlb

    [
        uuid(12345678-1234-1234-1234-123456789ABC),
        version(1.0)
    ] 
    
        library myTypeLib
        {
            [dllname("myLib.dll")]
    
            module myLib
            { 
    
                [entry("myFunc")]
                int __stdcall myFunc( LPSTR  filename_in,  LPSTR   filename_out, LPSTR  ErrMsg);
            };
        };
    

    To compile it use the command "midl" in the Visual Studio command prompt.

    The resulting .tlb file should be placed in the same directory of the VB6 project, together with the DLL.

    In the VB6 project under Project->References it's possible to add the .tlb file.

    If all is gone well, pressing F2, would be possible to notice "myTypeLib" in the list of the available library.

    Now it's possible to call "myFunc" inside the VB6 project!

    However there are two issue to point out:

    1)Some variable types are not compatible between VB6 and C. An example of this issue is rapresented by char arrays. While in VB6 they are declared as Dim myStr as String, in C they are usually declared as char myStr[MAX_DIM];. To make possible the translation between VB6 and C, without modifing the DLL, it's possible to declare on VB6 side the strings as Dim myStr as String * 256, while in the IDL file the corrispondent string should be passed to the function as LPSTR myStr.

    2)VB6 does not link the DLLs until the .exe is created. But if a DLL is not linked, then its functions are not visible. For this reasons, all the function of the implicitly linked DLLs that have to be used in the VB6 project, must be included in the IDL file.

    Moreover, for the same reason, even after all the functions have been included in the IDL file, won't be possible to run the program from the IDE (it will crash) as to debug it. The only way to run the application is to create the .exe.

    0 讨论(0)
  • 2020-12-02 03:12

    Here is a sample IDL that import functions from standard OS dlls

    [
      uuid(YOURTYPE-LIBG-UIDH-ERE0-000000000000),
      version(1.0),
      helpstring ("My Type Library 1.0")
    ]
    library MyTypeLib
    {
        importlib("stdole2.tlb");
    
        typedef struct {
            long    Data1;
            short   Data2;
            short   Data3;
            BYTE    Data4[8];
        } VBGUID;
    
        typedef VBGUID CLSID;
    
        [dllname("OLEAUT32")]
        module OleAut32
        {
            [entry("SysAllocString")]
            BSTR SysAllocString([in] long lpStr);
            ...
        };
    
        [dllname("USER32")]
        module User32
        {
            [entry("RegisterClipboardFormatA")]
            UINT RegisterClipboardFormat([in] LPSTR lpszFormat);
            [entry("FillRect")]
            DWORD FillRect([in] DWORD hDC, [in] int lpRect, [in] DWORD hBrush);
            ...
        };
    
        [dllname("BOGUS")]
        module Strings
        {
            const LPSTR CLSID_DsQuery = "{8A23E65E-31C2-11D0-891C-00A024AB2DBB}";
            const LPSTR CLSID_DsFindObjects = "{83EE3FE1-57D9-11D0-B932-00A024AB2DBB}";
            ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题