Register Custom URL Protocol handler



In the following example we're going to create a simple url protocol (like mailto/http etc) handler, e.g.
 
<a href="test:123">Some Text</a>
 



First of all we need to create some registry entries in order to register our custom protocol, we can do this programatically like this:
 
RegistryKey Key = Registry.ClassesRoot.CreateSubKey("test");
Key.CreateSubKey("DefaultIcon").SetValue("", "test.exe,1");
Key.SetValue("", "test:Protocol");
Key.SetValue("URL Protocol", "");
Key.CreateSubKey(@"shell\open\command").SetValue("", "test.exe %1");
 

Next we're going to create a very simple console application (test.exe) handling our request, we pass values to the handler via command line - in this case the hyperlink passing the command line.
 
using System;
using System.Collections.Generic;
using System.Text;
 
class Program
{
    static void Main(string[] args)
    {
        foreach (string s in args)
        {
            Console.WriteLine(s);
        }
        Console.ReadKey();
    }
}
 

Once we click on the test:123 hyperlink in the first snippet, you will notice a console application popping up - displaying the values passed to the protocol.

Note that browsers encode passed values differently, so make sure that you've tested your handler in multiple browsers.




Post/View comments
 

C#: Expose internal members to outside assemblies



Normally when members (properties/methods/delegates/indexers etc) are defined as internal, they're only available within the assembly they reside in.
 
internal class b
{
    public void c()
    {
        Console.WriteLine("Internal");
    }
}
 
Note: The default modifier for a top-level class is internal e.g. omitting the internal (in the preceding snippet) means the class will be internal in anycase.

But since .net 2.0 its possible to expose internal members to assemblies outside the residing assembly using the InternalsVisibleTo attribute.

Within the assembly that we wish to expose internal members, you will need to register the names of the assembly(ies) that require access to the internals like this:
 
[assembly: InternalsVisibleTo("someassembly")]
[assembly: InternalsVisibleTo("someotherassembly")]
 
The preceding snippet can be placed within your AssemblyInfo.cs file, or within other source files.

It is also possible (and advisable) to sign the assemblies with a strong key to make it difficult for a malicious user to spoof (replace/inject) code into your assembly.

To create/assign a strong key within visual studio:
  • right click on your project
  • select properties
  • click on the signing tab
  • tick sign the assembly
  • select new(or browse for an exiting key)
In order to extract a public key, go to your visual studio command prompt, browse/point to your strong keys with the following commands:

rem extract public key
sn -p keyPair.snk publicKey.snk
rem display public key
sn -tp publicKey.snk
 

Next add the public key to the assembly exposing internal members:

 
[assembly: InternalsVisibleTo("somesignedassembly, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b12297f91269c8957718971524fc11c1eb8d62bba5b04b82149ca45f4567a6f6f12a76db30fd3a63f89066137b331e48b8e8ff9753720ca1acf4e0910edd606654c8db9b2d5cd5ca08fc4fe7c2fe3d8bcc9debdb292cd0dedfe0737170a539065df87b77a52376b2588e8a1d12a91650aaf0db0204e875b4f40821c714b8e3a6")]
 

Note: In order to use a signed assembly, both the target and source assemblies need to be signed, in order to use an unsigned assembly, both target and source assemblies need to be unsigned.

Some sources:

Strong Name Tool (Sn.exe)
InternalsVisibleToAttribute Class




Post/View comments
 
First 21 22 23 24 25 26 27 28 29 30 Last / 62 Pages (124 Entries)

Latest Posts

Be the best stalker you can be


2011-12-13 22:33:54

Syntactic sugar (C#): Enum


2011-08-04 16:50:18

Top 5 posts

Simple WYSIWYG Editor


Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

Moving items between listboxes in ASP.net/PHP example


Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
2008-06-12 17:07:43

Cross Browser Issues: Firefox Word Wrapping


Firefox word wrapping issues
2008-06-09 09:51:21

Populate a TreeView Control C#


Populate a TreeView control in a windows application.
2009-08-27 16:01:03

C# YouTube : Google API


Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51