Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Call a Visual Basic .NET Class Library from Visual Basic for Applications in Microsoft Office


View products that this article applies to.

This article was previously published under Q317535

↑ Back to the top


Summary

Use this step-by-step guide to call a .NET class library from Visual Basic for Applications. This sample shows how a Visual Basic for Applications program can use a Visual Basic .NET class library to encrypt and decrypt a string. You can use the cryptography namespace, included in the .NET Framework, for the encryption/decryption.

Create the Visual Basic .NET Class Library

  1. Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Visual Basic Projects, select Class Library. Name the class CryptoClass and click OK. Class1 is created by default.
  2. Replace the contents of Class1 with the following code:

    Imports System.Security.Cryptography
    
    <ComClass(Class1.ClassId, Class1.InterfaceId, Class1.EventsId)> Public Class Class1
        Public Const ClassId As String = "98349785-8BE2-4604-848D-F5B103D61715"
        Public Const InterfaceId As String = "36613EE9-125F-493d-9968-771E18C2226A"
        Public Const EventsId As String = "A036F02F-F87E-4548-A536-7DD7EA8E62B5"
    
        Const sKey As String = "MyKey"
    
        Public Function EncryptTripleDES(ByVal sIn As String) As String
            Dim DES As New TripleDESCryptoServiceProvider()
            Dim hashMD5 As New MD5CryptoServiceProvider()
    
            ' Compute the MD5 hash.
            DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))
            ' Set the cipher mode.
            DES.Mode = CipherMode.ECB
            ' Create the encryptor.
            Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
            ' Get a byte array of the string.
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn)
            ' Transform and return the string.
            Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))
        End Function
    
        Public Function DecryptTripleDES(ByVal sOut As String) As String
            Dim DES As New TripleDESCryptoServiceProvider()
            Dim hashMD5 As New MD5CryptoServiceProvider()
    
            ' Compute the MD5 hash.
            DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))
            ' Set the cipher mode.
            DES.Mode = CipherMode.ECB
            ' Create the decryptor.
            Dim DESDecrypt As ICryptoTransform = DES.CreateDecryptor()
            Dim Buffer As Byte() = Convert.FromBase64String(sOut)
            ' Transform and return the string.
            Return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))
        End Function
    End Class
    						

    Note You can use the GUID generator to generate new GUIDs for the ClassId, InterfaceId, and EventsId. To generate new GUIDs, click Create GUID on the Tools menu.
  3. From Project Properties, select Configuration Properties. Click Build and then select the Register for COM Interop check box. Click OK.
  4. On the Build menu, click Build Solution to create the DLL.

Create a Visual Basic for Applications Macro to Call The .NET Class Library

  1. Start Microsoft Excel. Press ALT+F11 to start the Visual Basic Editor.
  2. On the Insert menu, click Module to insert a blank module.
  3. On the Tools menu, click References. Add a reference to the CryptoClass library and click OK.
  4. Type or paste the following code in the Module1 code window:
    Sub TestCrypto()
      Dim oCrypto As New CryptoClass.Class1
      Dim sCrypt As String
      
      sCrypt = oCrypto.EncryptTripleDES("This is a test")
      MsgBox "Encrypted text = " & sCrypt
      MsgBox "Decrypted text = " & oCrypto.DecryptTripleDES(sCrypt)
    End Sub
    					

Test the Code

  1. On the Tools menu, click Macros. In the list of macros, click TestCrypto, and then click Run.
  2. If a message box appears showing the encrypted string, click OK. A second message box appears showing the decrypted string.
  3. If the class library needs to be registered on another computer running .NET Framework, copy the DLL to the system and run the following command:
    regasm CryptoClass.dll /tlb:CryptoClass.tlb

↑ Back to the top


References

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
301070� HOW TO: Encrypt and Decrypt a File by Using Visual Basic .NET
For more information, see the following Microsoft Developer Network (MSDN) Web site:

↑ Back to the top


Keywords: kbcrypt, kbsecurity, kbhowtomaster, KB317535

↑ Back to the top

Article Info
Article ID : 317535
Revision : 13
Created on : 3/29/2007
Published on : 3/29/2007
Exists online : False
Views : 459