Automatic Updating of Visual Basic Applications: Part II

 

Scott Swigart
Swigart Consulting LLC

July 2006

Applies to:
   Microsoft Visual Basic 6.0
   Microsoft Visual Basic 2005
   Microsoft Visual Basic Express 2005 Edition
   Microsoft Visual Studio 2005

Summary: This article shows you how to automatically update Visual Basic 6.0 applications and COM objects, using the .NET Framework 2.0 and ClickOnce deployment. (9 printed pages)

Click here to download the code sample for this article.

Return to the previous article.

Contents

Introduction
Getting Started
Advanced Deployments
Reg-Free COM
Conclusion

Introduction

Many applications in production today can automatically detect when a new version is available, and download the update. In Part I of this article, you saw how you could enhance existing Visual Basic 6.0 applications to include such auto-updating functionality. The key was to utilize a new feature of the .NET Framework 2.0 known as ClickOnce deployment. ClickOnce provides the functionality to detect application updates, prompt the user to download, install the updates, and launch the new version of the application. ClickOnce can even be used for the initial application deployment, thus simplifying the process of getting the application out to a large number of users.

Part I of this article was limited to deploying and automatically updating a stand-alone Visual Basic 6.0 application. In this article, you will learn how to deploy more complex applications that require the deployment of COM objects or ActiveX controls with the application.

Getting Started

To utilize ClickOnce deployment, you must have the latest version of Visual Studio. If you already have Visual Studio 2005 installed, you're ready to get started; otherwise, I strongly recommend that you download and install the free Visual Basic Express. Visual Basic Express provides all the functionality that you need in order to ClickOnce deploy applications. It's a free, trimmed down version of the full Visual Studio 2005, but it doesn't time out like typical evaluation software.

This article also assumes that you have read Part I. If you have not read Part I, I strongly recommend that you review that article now, and then return here for information about more advanced deployments. This article also assumes that you know the basics of ClickOnce deployment, which are covered in Part I. Finally this article assumes that you've configured Internet Information Server (IIS) to support ClickOnce deployments, following the steps listed in Part I.

In Part I of this article, you created a Visual Basic .NET application that utilizes the .NET Framework ClickOnce feature to detect and install new application versions. Included with the Visual Basic .NET application was the Visual Basic 6.0 application that you really wanted to deploy. The Visual Basic .NET application didn't really do anything except check for new versions and launch the Visual Basic 6.0 application. If you completed the walkthrough steps in Part I of this article, you're ready to just continue with the steps listed here in Part II. If you did not do the walkthrough in Part I, simply download the code associated with this article, and you will be at a good starting point for the steps in Part II.

Advanced Deployments

It's typical for Visual Basic 6 applications to utilize COM components that must be deployed with the application. In order for your application to find the required COM objects at run-time, those COM objects must be properly registered. Usually, your application installer will register needed COM objects when the application is installed. However, the technology being used for automatic application deployment and updating, known as ClickOnce, is not designed to register COM objects in the traditional way. Complete the following steps to illustrate the problem. Then, you will see how this issue can be solved, and how you can use ClickOnce to deploy applications with COM objects.

Creating a simple COM object

  1. Download the code files for this article and open the MyAppLauncher - Starter folder.

  2. Double-click Project1.vbp to open the Visual Basic 6.0 application in the development environment.

  3. On the File menu, click Add Project.

  4. In the Add Project dialog box, select ActiveX Dll and click Open.

  5. In the Project 2 - Class 1 code file, enter the following.

    Public Function HelloWorld() As String
    HelloWorld = "Hello from a DLL."
    End Function
    

    At this point, you've created a very simple COM object that should be deployed with your Visual Basic 6.0 application.

  6. In the Project Explorer, select Project1 (Project1.vpb).

  7. On the Project menu, click References.

  8. In the References dialog box, select Project2 and click OK.

  9. In the Project Explorer, double-click Form1 to open the form designer.

  10. From the toolbox, add a new button to Form1.

  11. Double-click the button to generate the event handler. Enter the following code.

    Private Sub Command2_Click()
    Dim c As Project2.Class1
    Set c = New Project2.Class1
    MsgBox c.HelloWorld
    End Sub
    

  12. Press F5 to run the application.

  13. When Form1 appears, click the new button and make sure that the application shows the message box.

  14. On the File menu, click Make Project Group.

  15. In the Microsoft Visual Basic dialog box, click Build.

    The Visual Basic 6.0 application will now call into an associated COM DLL. Now it's time to deploy the Visual Basic application and DLL through ClickOnce, to see what happens.

To deploy the Visual Basic 6.0 application, it is piggy-backed onto a Visual Basic .NET application. Visual Studio 2005 knows how to publish Visual Basic .NET applications to a website, allowing users to perform a simple automated install. Once the Visual Basic .NET application is installed from the website, it will automatically check for new versions of itself on launch. If a new version is available, it will prompt the user to download the new version, and run that instead. The Visual Basic .NET application can also include "content files." These might be help files, database files, or anything else that you want deployed along with your Visual Basic .NET application. In this case, the content files are the Visual Basic 6.0 executable and the COM object that you just created. Anytime your Visual Basic 6.0 application or COM object changes, you can simply republish the Visual Basic .NET application, and the changed Visual Basic 6.0 files will be republished along with it.

Deploying the application with ClickOnce

  1. Switch to Windows Explorer, and then double-click MyAppLauncher.sln to open the ClickOnce deployment project in either Visual Studio 2005 or Visual Basic Express.

  2. On the Project menu, click Add Existing Item.

  3. In the Add Existing Item dialog box, in the Files of type box, select All Files (*.*).

  4. Select Project2.dll and click Add.

    The DLL should now be part of your Visual Basic .NET solution, as shown in Figure 1.

    Aa697429.atup01(en-US,VS.80).gif

    Figure 1. Including the Visual Basic 6.0 DLL

  5. In the Solution Explorer, right-click Project2.dll, and then click Properties.

  6. In the Properties window, change the Copy to Output Directory property to Copy if newer.

    This ensures that when you build and publish the Visual Basic .NET application, the DLL is included in the deployment.

  7. In the Solution Explorer, double-click My Project.

  8. Click the Publish tab.

  9. Click Publish Now.

    The application will be published to your local Web server, and an installation Web page will be displayed as shown in Figure 2.

    Click here for larger image

    Figure 2. Application installation Web page (Click on the image for a larger picture)

    From this page, anyone can install the application. If the user does not have the .NET Framework 2.0 installed, he or she will be prompted to download and install it. The result is very little effort on your part to create a deployment that has wide reach, and that is easy for users to install.

  10. Click Install.

    The application will download, install, and run, as shown in Figure 3.

    Aa697429.atup03(en-US,VS.80).gif

    Figure 3. Deployment of a Visual Basic 6.0 application with ClickOnce

Impressive, but this is where the trouble starts. If you click the Hello button, a "Hello World" message will be displayed. However, if you click the Call DLL button, the form will attempt to call into the COM DLL that you created at the beginning of this article. ClickOnce did deploy that DLL, but it did not register it. As a result, you're treated to the dreaded error shows in Figure 4.

**Note   **If you install the application on the computer where it was developed, you will not see this error. This is because the COM object is registered by the Visual Basic 6.0 development environment when it's compiled. To see the error, install the application onto a different machine.

Aa697429.atup04(en-US,VS.80).gif

Figure 4. Error when the COM object can't be found

Reg-Free COM

With the introduction of Windows XP, Microsoft provided a mechanism for deploying COM objects without requiring registry entries. This allows you to deploy COM objects as DLL files that install in the same directory as your application .exe. If your application includes a .manifest file, it can find and use the COM objects, even though the COM objects are not registered.

This feature was originally added to Windows XP to alleviate the much ballyhooed "DLL Hell" problem. In a nutshell, DLL Hell is when you have an application that uses a specific COM object, and you install a different application that may replace that COM object with a newer version. If the new version isn't truly backward-compatible, installing a new application breaks the existing one.

Beginning with Windows XP, you could install Side-by-Side Assemblies, meaning that you could install multiple versions of the same COM objects, as long as the COM objects were isolated to the application that would use them.

ClickOnce deployment can utilize this functionality to deploy COM objects along with .NET applications. When you deploy a .NET application that uses COM objects, Visual Studio creates a .manifest file that allows the .NET application to use included COM objects, without requiring those COM objects to be registered.

Getting Visual Studio to generate a .manifest file for a .NET application is trivial. This .manifest file can then be modified so that your Visual Basic 6.0 application gets the same nifty Reg-free COM functionality.

  1. Switch to Visual Studio 2005 or Visual Basic Express.

  2. In the Solution Explorer, double-click My Project.

  3. Click the References tab.

  4. Click Add.

  5. In the Add Reference dialog box, click the COM tab.

  6. Select Project2 and click OK.

  7. Under References, select Interop.Project2, and then press F4 to view the properties window.

  8. Set the Isolated property to True.

    Now that the COM DLL has been referenced from the .NET launcher application, a .manifest file will be created the next time the application is published. Because the Isolated property has been set to True, Visual Studio knows to include the needed information in the manifest to deploy this DLL using Reg-free COM.

  9. Click the Publish tab.

  10. Click Publish Now.

    To find the .manifest file, you will need to view some of the hidden files that are part of your project.

  11. On the Project menu, click Add existing item.

  12. Expand the hidden bin > Debug folder.

  13. In the Files of type box, select All files (*.*).

  14. Select MyAppLauncher.exe.manifest and click Add.

  15. In the Solution Explorer, rename MyAppLauncher.exe.manifest to Project1.exe.manifest.

    This manifest file was generated for the Visual Basic .NET application, but it will be modified to work for your Visual Basic 6.0 application.

  16. In the Solution Explorer, right-click Project1.exe.manifest, and then click Properties.

  17. In the Properties window, for Build Action, select Content.

  18. For Copy to output directory, select Copy if newer.

  19. In the Solution Explorer, double-click Project1.exe.manifest.

  20. Edit the file down to just the following lines.

    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly
    xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
    manifestVersion="1.0"
    xmlns:dsig="https://www.w3.org/2000/09/xmldsig#"
    xmlns="urn:schemas-microsoft-com:asm.v2"
    xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
    xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">

<asmv1:assemblyIdentity name="Project1.exe" version="1.0.1.5" language="neutral" processorArchitecture="x86" type="win32" />

<file name="Project2.dll" asmv2:size="20480" xmlns="urn:schemas-microsoft-com:asm.v1"> <typelib tlbid="{be74c02e-c9d6-43f3-94d4-bb99486c1313}" version="3.0" helpdir="" resourceid="0" flags="HASDISKIMAGE" /> <comClass clsid="{a70878a5-5f9c-4e58-a8d8-6814f260afad}" threadingModel="Apartment" tlbid="{be74c02e-c9d6-43f3-94d4-bb99486c1313}" progid="Project2.Class1" /> </file> </asmv1:assembly>

Note the changes to the **name** and **processorArchitecture** attributes.

You should now be able to ClickOnce deploy the Visual Basic 6.0 application and the included COM component.
  1. In the Solution Explorer, double-click My Project.

  2. Click the References tab.

  3. Remove the reference to Interop.Project2.

    This reference was originally added to get Visual Studio to generate the beginnings of the manifest file that you need for your Visual Basic 6.0 application. Now that Project1.manifest.exe is complete, you should remove this reference.

  4. Click the Publish tab.

  5. Click Publish Now.

  6. Install the application. When the application runs, click the button that calls into the DLL.

    The application should run without error, and you should see the output shown in Figure 5.

    Aa697429.atup05(en-US,VS.80).gif

    Figure 5. ClickOnce-deployed application and COM object

Conclusion

In this two-part article series, you have seen how you can build Visual Basic 6.0 applications that easily deploy, and that automatically detect and install new versions. This functionality comes for free with the .NET Framework 2.0, and it can be leveraged by piggy-backing a Visual Basic 6.0 application onto a trivial Visual Basic .NET launcher application. In this article, you have also seen how you can deploy COM objects with your Visual Basic 6.0 application. The COM objects are isolated to the application by using Reg-free COM, a technology available on Windows XP and later operating systems.

About the author

Scott Swigart spends his time consulting, authoring, and speaking about converging and emerging technologies. With development experience going back over 15 years, and by staying in constant contact with future software development technologies, Scott is able to help organizations get the most out of today's technology while preparing to leverage the technology of tomorrow. Scott is also the author of several .NET books, a certified Microsoft trainer (MCT) and developer (MCSD), and a Microsoft MVP. Feel free to contact the Scott at scott@swigartconsulting.com, or check out his latest musings at blog.swigartconsulting.com.

© Microsoft Corporation. All rights reserved.