Developer

The importance of dependencies in VB

How do you know what files are required for your application? That's the question we tackle in this installment of Visual Basic Q&A.


Q: I made a chat application in VB using the Winsock ActiveX control under Windows 98. I copied the .exe file from that machine and tried to run it under Windows 2000 Professional. When I did this, an error occurred: "MSWINSCK.OCX component or one of its dependencies is not installed or is missing.” How do I fix this?
-Ganesh V.


A: Every VB developer has run across a variation of this error message at one time or another. The problem here is likely that the mswinsck.ocx file is either not present on your Windows 2000 machine or it is not correctly registered. A simple way to fix this problem would be to copy the mswinsck.ocx file from the Windows 98 machine into the Windows 2000 machine’s WinNT\System32 folder and run the following command from either the Start menu’s Run option or a command prompt:
 
regsvr32.exe C:\WinNT\System32\mswinsck.ocx
 

You have questions…
Got a Visual Basic question? Builder can help. Send us your VB question, and we'll take a crack at it.

So what’s that do? Assuming that our Windows folder paths are the same, it creates the registry keys that Visual Basic needs to locate and use the Winsock control at runtime, otherwise known as registering the control. For a little more in-depth explanation of what it means to register a control, see “Demystifying version compatibility settings in Visual Basic.”

The answer depends…
Your particular problem is so easy to fix because your application is a simple one. A more complex VB program may have tens or even hundreds of such files, or dependencies, that are required for it to run properly. How do you know which files are required? Well, you can open your project’s VBP file in a text editor, like Notepad, and have a look for yourself. For illustration, I built a simple VB project, called Project1, that uses Winsock. Here is an excerpt from my VBP file:
Type=Exe
Form=Form1.frm
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\WINNT\System32\stdole2.tlb#OLE Automation
Object={248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0; MSWINSCK.OCX
Startup="Form1"
Command32=""
Name="Project1"


Looking very carefully at the third through sixth lines (which would really be just the third line if it hadn’t wrapped in your browser), we can see that Project1 is going to need files called stdole2.tlb and mswinsck.ocx.

But this still isn’t the complete picture. Project1 actually has more dependencies, some required by Visual Basic itself and some required by the Winsock control. To see them all, use Visual Studio’s Depends tool, which is typically found in the Common\Tools subfolder of your Visual Studio root folder. Depends examines a binary file, like a .exe, .dll, or .ocx, and gives you a dependency tree that lists all files needed by the binary, even those required indirectly. Figure A shows the dependency trees for Project1.exe and the Winsock control. They have both been compressed for space.

Figure A
Dependency trees for Project1.exe and Mswinsck.ocx


An ounce of prevention
Of course, the simplest way of ensuring that all your project’s required files are present is to build an installation program that installs any missing ones. A variety of commercial software is available from companies like WISE and InstallShield that can take care of this for you. Visual Studio itself comes with two such application installment tools, the Package and Deployment Wizard, and the Setup Toolkit.

The Package and Deployment Wizard (PDW) is an easy-to-use tool that scans your VB project for dependencies. It then creates a package, or cab file, that contains your program and all its dependencies, and a simple setup program that can take care of installing them. More information on the PDW can be found at MSDN Online.

The Setup Toolkit is a VB project that is actually used by the PDW to install your application. You can use it as a template to create your own setup program if you want more control over the process than the PDW gives you, which isn’t a lot. Again, MSDN Online offers more information on the Setup Toolkit.

All of this just goes to show that installing a VB application onto a computer isn’t as simple as it seems. Now you know a couple of different ways to find out which files your program needs, so you can ensure that they are on the target computer if you have trouble running your application. You’ve also seen two Visual Studio tools you can use to create your own installation programs and make sure that the files you need are installed along with your application. Happy coding.

Editor's Picks