1

I am creating an ActiveX DLL assembly. I have followed this article. As the article has mentioned, I have created a .cs file (using notepad) and manually compiled it by pasting the file under \WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx and running csc /t:library AClass.cs.

At the next step, when I try to register this assembly, using regasm AClass.dll /tlb /codebase, it gives me a message saying this assembly needs to be signed. It does not throw any error. But, it forces me to enable 'Initialize unsigned ActiveX scripts' under IE settings for it to work.

Can you guide me on how to sign such an assembly... So that enabling the 'Initialize unsigned ActiveX scripts' is not necessary for all clients.

I am using Windows Server 2012 machine with VS2012..

variable
  • 8,262
  • 9
  • 95
  • 215
  • 2
    Why not just use Visual Studio Express? It's free. – John Saunders Jan 14 '14 at 12:07
  • Hi John, I am new to this, so I have followed the steps mentioned in the article. As you say, I have tried using Visual Studio, after compiling I moved the dll file from bin/debug folder into the '\WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx' folder and tried running the regasm command which gave error as INVALID .NET ASSEMBLY FILE.... – variable Jan 14 '14 at 12:10
  • I would suggest you to read [Digital Signing for ActiveX Components](http://msdn.microsoft.com/en-us/library/aa231196(v=vs.60).aspx) and/or [How to sign an ActiveX DLL with a Certificate](http://stackoverflow.com/questions/4549465/how-to-sign-an-activex-dll-with-a-certificate) –  Jan 14 '14 at 12:12

3 Answers3

3

I don't think you need to sign it manually. It just requires strong name, because you use /codebase.

Usually you would create a strong name with an attribute in your code so that it is processed by CSC.

using System;
using System.Reflection;

[assembly:AssemblyKeyFileAttribute("MyPublicKey.snk")]
[assembly:AssemblyDelaySignAttribute(true)]

namespace MyNamespace
{
    public class MyClass { }
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Ok, I will try this, can you tell me how to create the .snk file? – variable Jan 14 '14 at 12:15
  • Yes, this is probably the best way. Note that it IS being signed - it just means that you don't need to explicitly sign it if you do it like this. – Matthew Watson Jan 14 '14 at 12:15
  • @NachiketKamat See the links I posted in my answer; they tell you how to create an SNK file - especially see the second link. – Matthew Watson Jan 14 '14 at 12:16
  • @MatthewWatson I have used the sn -k myfile.snk command and have got the snk file. I am now going to use the code as mentioned by Thomas W and paste it above the namespace tag of the cs file. Then I will place both files (cs and snk) into the '\WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx' folder and run the csc and regasm command, it that OK? – variable Jan 14 '14 at 12:24
  • `Regasm /codebase` will create a Registry entry with the full path of your DLL. If you want to put it into the GAC, use `gacutil` instead http://msdn.microsoft.com/de-de/library/ex0ss12c%28v=vs.110%29.aspx You never put source code anywhere. – Thomas Weller Jan 14 '14 at 12:27
  • Hi Thomas, actually I am following this link http://dotnetslackers.com/articles/csharp/writinganactivexcontrolincsharp.aspx and it has been mentioned to use regasm.. and it works all good. Only signing is the issue... – variable Jan 14 '14 at 12:32
  • If you follow this tutorial, why do you want to copy it into `WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx`? That's not mentioned. – Thomas Weller Jan 14 '14 at 12:34
  • It is mentioned that :- Place your AClass.cs file in the folder where the csc.exe exists (WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx??? isnt it? Am I wrong here?). Then by command (DOS) interface go to that particular folder and execute the following command:csc /t:library AClass.cs then regasm command – variable Jan 14 '14 at 12:37
  • OMG. Don't do that. It will compile your DLL directly into the GAC folder and you mess it up with our source files etc. – Thomas Weller Jan 14 '14 at 12:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45196/discussion-between-thomas-w-and-nachiket-kamat) – Thomas Weller Jan 14 '14 at 13:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45292/discussion-between-thomas-w-and-nachiket-kamat) – Thomas Weller Jan 15 '14 at 15:16
1

You can use SIGNTOOL to do this. It's part of the Windows SDK. Note that you don't actually sign a ".cs" file (which is a source code file) - you sign an Assembly (i.e. a DLL or EXE file).

You will need to have a signing key with which to sign it, which you can create using the Strong Name Tool (sn.exe).

For further details see this introduction to Code Signing.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • thanks Matt, I have created the snk file.. not yet able to attach it to the cs though.. – variable Jan 14 '14 at 12:39
  • Hi although I am able to now sign the dll with a snk file, I still have to enable the Initialize activex script not marked as safe via IE options for this dll to work... Is there nay way to not force user to enable this? – variable Jan 15 '14 at 08:34
-1

OK, here is what I did to sign the assembly:- (thanks to both authors above who have answered the question, this is a summary of the method I have followed:)

1) create cs file using notepad: make sure you add [assembly:AssemblyKeyFileAttribute("mynewkey.snk")] [assembly:AssemblyDelaySignAttribute(true)] above the namespace tag and add using System.Reflection

2) go to c:\program files x86\micorsoft SDKs\windows\v8.0\bin\NETFX 4.0 Tools\sn -k mynewkey.snk

3) go to C:\mysource> C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /t:library myclass.cs

4) c:\program files x86\micorsoft SDKs\windows\v8.0\bin\NETFX 4.0 Tools\sn -R AClass.dll myneykey.snk

5) regasm AClass.dll/tlb /codebase ----> (run this from 4.0 framework)

DONE

I have followed this msdn link but instead of following the manual for STEP 4 of the link, I have used :- sn -k mynewkey.snk --> this is to create a key file..

Also, you can compile the file in Visual Studio.. So, no need to perform steps 1 to 4 of above because VS allows you to sign as well as compile the file. Step 5 must be followed to register the DLL..

variable
  • 8,262
  • 9
  • 95
  • 215