9

I found this article on CodeProject: http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

and thought it would be nice to give it a try, but in F#. So I came up with the following code:

open System
open System.IO
open System.Text
open System.Runtime.InteropServices
open System.Windows.Forms
open SharpShell
open SharpShell.Attributes
open SharpShell.SharpContextMenu

[<ComVisible(true)>]
[<COMServerAssociation(AssociationType.ClassOfExtension, ".txt")>]
type CountLinesExtension() =
    inherit SharpContextMenu.SharpContextMenu()

    let countLines =
        let builder = new StringBuilder()
        do 
            base.SelectedItemPaths |> Seq.iter (fun x -> builder.AppendLine(sprintf "%s - %d Lines" (Path.GetFileName(x)) (File.ReadAllLines(x).Length)) |> ignore  )
            MessageBox.Show(builder.ToString()) |> ignore

    let createMenu =
        let menu = new ContextMenuStrip()
        let itemCountLines = new ToolStripMenuItem(Text = "Count Lines")
        do
            itemCountLines.Click.Add (fun _ -> countLines)
            menu.Items.Add(itemCountLines) |> ignore
        menu

    override this.CanShowMenu() = true
    override this.CreateMenu() = createMenu

However, I noticed that there is no support for signing an F# assembly in VS2012 (step 4. in the article). I learnt that if I want to do so, I need to create a key manually (typing "sn -k keyName.snk" into the command prompt) and then add a flag in "Project Properties -> Build -> Other Flags" (--keyfile:keyName.snk).

I still didn't manage to successfully run this. Moreover, using the author's application (in "Debugging the Shell Extension" section) I get an error that my assembly doesn't contain a COM server.

I believe I'm doing something wrong with the signing the component. Could you help me in running this ?

4
  • I believe your issue with the COM server error has to do with your key, not with the F#. Make sure the cert you're using is registered with your local machine.
    – N_A
    Jul 22, 2013 at 14:31
  • You can also try something like this: books.google.com/…
    – N_A
    Jul 22, 2013 at 14:40
  • "Doesnt contain a COM server" suggests a problem with the visibility/registration of your COM component. You can use sn -v (or is it -V ?) to verify your signing but bottom line is there will be an error somewhere saying authenticode signature error if that's the problem. And get ProcMon and DbgView on the case. If you get any more detail re exact error messages etc., no harm to add that detail here Jul 22, 2013 at 23:19
  • I ran "sn -v <myAssemblyName.dll>" and it resulted in Assembly 'ShellExtentions.dll' is valid To be more precise, when I tried to load this assembly in Server Manager (from the article), I got an error "<myAssebly> is not a SharpShell extention". I also tried registering the assembly in GAC, but didn't work neither.
    – LA.27
    Jul 23, 2013 at 21:12

2 Answers 2

15

One way to sign an F# assembly is via the AssemblyFileKeyAttribute attribute.

Create a new module and in it put:

module AssemblyProperties

open System
open System.Reflection;
open System.Runtime.InteropServices;

[<assembly:AssemblyKeyFileAttribute("MyKey.snk")>]

do()

Where "MyKey.snk" is the path to your key relative to the project directory.

Another way, as found in this bug report on Microsoft Connect, is to add --keyfile:MyKey.snk to the Other Flags field in the Properties --> Build tab.

Using either approach; running sn -v myfsharpassembly.dll will assert that the assembly is valid after compilation.

1

Maybe the problem is that the assembly is not "ComVisible"?

I'm using this library in F# like this:

[<assembly:ComVisible(true)>]
[<assembly:AssemblyKeyFile("Key.snk")>]
do
   ()

That is, I specify the assembly-level attributes at a top-level do-block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.