VB - DLL binding implicitly

I am working on a VB6 GUI and I need to do implicit DLL bindings.

The motivation for this comes from my previous question . This DLL uses static TLS, __declspec(thread) , and, of course, this fails when the DLL is directly connected to LoadLibray.

I would really like to avoid changes to the DLL, so does anyone know how to trick the VB6 executable to reference a particular DLL implicitly?

+3
multithreading windows dll visual-c ++ vb6
source share
3 answers

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

Compilation with the MIDL compiler and a link to the resulting tlb file from your VB6 project (Project - References).
And delete all Declare Function s.

The tlb file is used only for compilation (in this case), you do not need to include it in the configuration.

+5
source share

Here is an example IDL that imports functions from standard DLL systems

 [ 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}"; ... } } 
+2
source share

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

Here is the IDL that will be used to create 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 "midl" command on the Visual Studio command prompt.

The resulting .tlb file must be placed in the same directory of the VB6 project along with the DLL.

In the VB6 project, in Project-> References, you can add the .tlb file.

If everything went well by pressing F2, you could see "myTypeLib" in the list of available libraries.

Now you can call "myFunc" inside the VB6 project!

However, there are two questions:

1) Some types of variables are not compatible between VB6 and C. An example of this problem is represented 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 it possible to translate between VB6 and C without modifying the DLL, you can declare strings on the VB6 side as Dim myStr as String * 256 , while in the IDL file, the correspondent string must be passed to the function as LPSTR myStr .

2) VB6 does not bind DLLs until .exe is created. But if the DLL is not connected, then its functions are not visible. For this reason, the entire function of the implicitly linked DLLs that should be used in the VB6 project must be included in the IDL file.

In addition, for the same reason, even after all the functions have been included in the IDL file, it will not be possible to start the program from the IDE (it will crash) in order to debug it. The only way to start the application is to create .exe.

0
source share

All Articles