Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Knowledge Base > Tutors' Corner > Advanced VB6 DLL Construction


Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2007, 08:28 PM
Mathimagics's Avatar
Mathimagics Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
Default Advanced VB6 DLL Construction


It is generally well-known that an ActiveX DLL can be made to export selected entrypoints, allowing one to call these DLL's directly via the API.

This is achieved by customising the final step (the link step) in the MAKE process, which produces the DLL from an ActiveX project.

This method, both well-documented and widely used for some years now, only solves one aspect the general VB6 API DLL problem, however - a VB6 DLL called directly is still very restricted in what it can do, and will often abort the client process, because it will not perform some critical runtime initialisation which a normally-deployed ActiveX DLL does automatically.

That's simply because the required runtime initialisation is not embedded in the DLL's entrypoint function (DllMain), but is done by the DLL's DllGetClassObject function. For ActiveX DLL's that are built conventionally, the DLL is of course a class library, and this function gets called whenever a client application requests the creation of a new object instance from that DLL.

This initialisation is COM-related, so I call it "DLL COM Initialisation", even though the DLL's we wish to build are not providing objects (they are intended to provide functions that are directly callable via the Win32 API).

The key point is that this COM-initialisation is always required in any application involving a VB6 module. If the appn is a VB6 exe, it's done by the app itself. If the appn is a non-VB client, then it would normally only involve a VB6 DLL if it was using CreateObject to get an object instance from a registered ActiveX DLL, and the required initialisation is performed automatically, as I described above.

If a non-VB client calls the DLL directly via the API, then the DLL must itself perform the COM initialisation, otherwise the client application will invariably crash.

About 12 months ago, I wrote a technical paper in which I described this process, and how a custom-linked DLL could be made to do this COM initialisation (a surprisingly simple process).

I went one step further, also, showing that it was also possible for such a DLL to support a GUI - that is, to display VB6 Forms - just so long as they were managed properly.

Attached to this thread are the technical paper itself, together with a copy of the link-customisation tool used in the examples.

Here is the "abstract" entry from the document:
Quote:
VB6 programs at runtime make significant use of COM interfaces, even if the source code does not use COM objects explicitly. Some critical runtime support mechanisms, including error handling, are COM-based (eg. the Err object). VB6 applications (EXE’s) automatically initialise their COM environment when executed, but VB6 DLL’s only initialise COM when the client application uses the DLL via a COM interface, not as an API (a shared library).

If a client application calls a VB6 DLL function directly, the DLL’s runtime state has no COM capability, and so the DLL will abort if it makes any sort of COM object reference. This has fatal consequences for the entire client process.

We present a method for building a VB6 DLL that can initialise its COM environment automatically. This allows VB6 DLL’s to be deployed as shared libraries, so they can be serve as API’s for clients written in other languages.

We begin by showing how a client can manually initialise a VB6 library, via the DLL’s DllGetClassObject function. Then we show how this method can be automated by implementing it in the DLL’s own entrypoint function.

Finally, we look at the issue of VB6 Form display, and show that a VB6 library can indeed provide a non-modal GUI, even with a non-VB client, with some careful programming.
Attached Files
File Type: pdf VB6_Library_Guide_Rev1.pdf (159.4 KB, 1242 views)
File Type: zip vbLinkTool.zip (21.7 KB, 1002 views)
__________________
Cogito, ergo codo

Last edited by Mathimagics; 04-27-2007 at 10:19 PM.
Reply With Quote
  #2  
Old 12-21-2010, 01:49 AM
Mathimagics's Avatar
Mathimagics Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
Arrow Update - Fix Link Problem for Projects not on C-Drive

The "MVBLC Link Control Tool" provided in Appendix I will not function correctly if your DLL project is not on drive C.

To assure correct operation for any drive, a single line of code needs to be inserted in the routine RunCustomLink. (Ref: Document page 25)

The code given includes these 2 lines:
Code:
Open "c:\vbLink.bat" For Output As #F
Print #F, "cd """ & VB6FOLDER & """"
Insert the following line in between them:
Code:
Print #F, Left$(VB6FOLDER, 2) ' ensure project drive selected
The routine vbTidy immediately below does not need to change because it already includes the required drive-assertion command.
__________________
Cogito, ergo codo
Reply With Quote
  #3  
Old 03-10-2011, 10:01 PM
Mathimagics's Avatar
Mathimagics Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
Default Update - non-standard VB6 install locations

Ref: Document p31

In the link tool program the assumed location of the installed VB6 program files (where files VB6.EXE and LINK.EXE are found) is hard coded thus:
Code:
Const VB6FOLDER = "C:\Program Files\MicroSoft Visual Studio\VB98"
You will need to adjust this line if you have installed VB6 to a different folder.
__________________
Cogito, ergo codo
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->