Registering and Trusting an Application-Level Smart Tag Class Library


After you have implemented your Recognizer and Action class completely in your class library project, build the project to create a class library DLL. Then copy the class library DLL that was built to a convenient directory. In this example, we copy it to C:\PartNumberSmartTag\PartNumberSmartTag.dll.

Office can load the Smart Tag DLL we have created directly and without any of the problems associated with managed add-ins described in Chapter 23, "Developing COM Add-Ins for Word and Excel." Office will check the .NET 1.1 security policy to decide whether to trust the DLL. If there is a policy in place to trust the Smart Tag DLL, Office will load the Smart Tag DLL into its own application domain.

We must do three things to get our Smart Tag to work:

  1. We must register the Smart Tag recognizer class in the registry.

  2. We must register the Smart Tag action class in the registry.

  3. We must configure .NET 1.1 policy (not 2.0 policy) to trust the Smart Tag DLL.

The final requirement seems counterintuitive; why would we have to configure .NET 1.1 policy? After all, we built the Smart Tag with Visual Studio 2005 against .NET 2.0. The reason is that trust decisions for managed Smart Tags loaded by Office 2003 are made based on .NET 1.1 policy even when Office is running a newer version of .NET.

Registering the Smart Tag Recognizer Class in the Registry

To register the Smart Tag class library in the registry, we must add a registry entry for the Recognizer class and a registry entry for the Smart Tag Recognizer class. Recognizer classes are registered under this path in the registry:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Recognizers 


Under this path, we must create a new key that has as its name the full name of the managed Recognizer class. In our case, we created a class called Recognizer in the namespace PartNumberSmartTag. Therefore, the full name of the managed Recognizer class is PartNumberSmartTag.Recognizer. We will create a new registry key named as follows:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\  Recognizers\PartNumberSmartTag.Recognizer 


Under the new PartNumberSmartTag.Recognizer registry key, we will create a string value called Filename that is set to the full filename of the Smart Tag DLL (in our example, C:\PartNumberSmartTag\PartNumberSmartTag.dll).

We will also create under the new PartNumberSmartTag.Recognizer registry key a DWORD value called Managed that we will set to 1.

Listing 16.12 shows the final registry settings for registering the Recognizer class when exported to a .reg file.

Listing 16.12. Registry Entries to Register the Recognizer Class

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\ Recognizers\PartNumberSmartTag.Recognizer] "Filename"="c:\\PartNumberSmartTag\\PartNumberSmartTag.dll" "Managed"=dword:00000001 


Registering the Smart Tag Action Class in the Registry

With the Recognizer class registered, the next step is to register the Smart Tag Action class in the registry. Action classes are registered under this path in the registry:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Actions 


Under this path, we must create a new key that has as its name the full name of the managed Action class. In our case, we created a class called Action in the namespace PartNumberSmartTag. Therefore, the full name of the managed Action class is PartNumberSmartTag.Action. We will create a new registry key named as follows:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\  Actions\PartNumberSmartTag.Action 


Under the new PartNumberSmartTag.Action registry key, we will create a string value called "Filename" that is set to the full filename of the Smart Tag DLL (in our example, C:\PartNumberSmartTag\PartNumberSmartTag.dll).

We will also create under the new PartNumberSmartTag.Action registry key a DWORD value called Managed that we will set to 1.

Listing 16.13 shows the final registry settings for registering the Action class when exported to a .reg file.

Listing 16.13. Registry Entries to Register the Action Class

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\ Actions\PartNumberSmartTag.Action] "Filename"="c:\\PartNumberSmartTag\\PartNumberSmartTag.dll" "Managed"=dword:00000001 


Setting Up .NET 1.1 Security Policy to Trust the Smart Tag Class Library

The final step is to set up .NET 1.1 security policy to trust the Smart Tag class library. We will consider how to configure .NET security policy in more detail in Chapter 19, ".NET Code Security." For now, we will use a command-line tool called caspol.exe that configures .NET security policy. From the command line, navigate to the version of caspol.exe that will be at a path such as C:\Windows\Microsoft.NET\Framework\v1.1.4322. In this directory, run the following command:

caspol -user -addgroup "All_Code" url c:\PartNumberSmartTag\PartNumberSmartTag.dll FullTrust -name "PartNumberSmartTag" 


This command adds user-level security policy under the existing code group called All_Code, a new code group called PartNumberSmartTag that grants full trust to our DLL C:\PartNumberSmartTag\PartNumberSmartTag.dll.




Visual Studio Tools for Office(c) Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
ISBN: 0321411757
EAN: 2147483647
Year: N/A
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net