Wednesday, July 04, 2007

Creating, exposing and signing ActiveX control built with C#

Well, this is really old stuff, but since Silverlight got more and more attention, there are a lot of questions about how to make your ActiveX to be exposed by JavaScript and how to deploy such stuff. You ask why? Well, I can not explain you... yet...

Let's start. Create regular dll in VS2005 and create UserControl there. Then expose the interface of the control by creating regular properties, you want to access from outside. (note: I do not like to do it directly updates something, due to security reasons, but for this example it will fine). So you have a public property, but in order to make it visible to COM world, you should create an interface and make your control to inherit from it. This interface will tell COM/COM+ that there are public properties in this AX.

Now we'll have our AX ready and, actually, you can use it already by settings classid attribute of Object in HTML page to something like YouAssemblyName.dll#YourNamespaceInClass.YouControl. You can also generate GUID for it to assure future compatibility. You can set variables and invoke methods of your control by calling TheNameOfObjectInHTML.PropertyOrMEthodExposedByTheInterface . If you want to use GUID as AX identification, just add [Guid("YOUR-GUID-HERE")] attribute above the class definition in code. Well. run it - see nothing? Of cause. You should give your ActiveX Full Trust permissions.

How to do it? Use CasPol.exe utility from SDK. Run CasPol -m -ag 1.2 -hash SHA1 -file MyFile.exe FullTrust. What, actually we have here?

  • -m is modifying machine level policy (this set in some cases to user level)
  • -ag 1.2 is adding a code group under LocalInternet (in default machine policy)
  • -file / -url is membership conditions for new group
  • FullTrust is FullTrust :)

Now you can copy (in some cases merge) your config into web or machine application config. If something goes wrong use CasPol -all -reset to restore all defaults.

Still problems? Sign your assembly. use "signtool signwizard" from VS command prompt to invoke certificate sign dialog. If you have code certificate you paid for to one of well known authority, you'll be able to install your AX with yellow strip only in client's machine from internet zone, if your certificate is personal or not trusted in some cases you'll get red bar, in order cases the installation will be terminated.

If you have more then one assembly, you want to deploy, use CAB tools from VS (Setup and Deployment) and don't forget to create INF file for your cab project. Then in HTML Object use codebase attribute to point to your cab. You can also point it to use specific version by using YourAXCAB.cab#Version=a,b,c,d instead of just cab. It's recommended to delay sign your assemblies to be enable easy updates in the future.

That's all, we done. Have a nice day :)

2 comments:

C# developer said...

i wrote this in my .dll

namespace TIFImageEdit
{
[Guid("B5FAFAEB-F745-4f17-B599-5D7C27D5D6D5")]
public partial class TIFImageEditor : UserControl
{
private Boolean startDrag;
private Int16 rotation;

private int zoomFactor;

private string m_location;
public string ImgLocation
{
get { return m_location; }
set { m_location = value; }
}


private string m_ModifiedImageLocation;

public string ModifiedImageLocation
{
get { return m_ModifiedImageLocation; }
set { m_ModifiedImageLocation = value; }
}
and i did this

hi I developed one class liabrary in C# .net and i am launching that file as activeX control
web page.After i get dll i developed .inf file its like this


[version]
; version signature (same for both NT and Win95) do not remove
signature="$CHICAGO$"
AdvancedINF=2.0

[Add.Code]
TIFActiveX.ocx=TIFActiveX.ocx
TIFImageEditor.dll=TIFImageEditor.dll
sharpPDF.dll=sharpPDF.dll

[sharpPDF.dll]
file-win32-x86=thiscab
FileVersion=1,0,1870,19498
DestDir=11
RegisterServer=yes

[TIFImageEditor.dll]
file-win32-x86=thiscab
clsid={89AFC042-93BC-4e72-AA00-343F6CF782EC}
FileVersion=1,0,0,0
RegisterServer=yes

[TIFActiveX.ocx]
file=thiscab
RegisterServer=yes
FileVersion=1,0,0,0
; end of INF file

this time i wrapped my class liabrary in MFC and after that i get .OCX file and using of .dll , .ocx, .inf
i get .cab file and i sign that .cab file with this command signtoll sign /f .pfx /p .cab .
and i launch that cab file in web page like .aspx page goes this
script LANGUAGE="javascript"
function Scrape()
{
try
{

var objDownload = new ActiveXObject("TIFImageEditor.TIFImageEditor");
alert('trying');
objDownload.TIFImageEditor_Load();
}
catch(exception) {
alert( "Failed" );
}
}
/script

/head
body
form id="form1" runat="server"
div
asp:Literal ID="Literal1" runat="server"/asp:Literal
/div
/form
/body
/html

and .aspx.cs goes this

ing strObject = @"OBJECT id='myControl1' name='myControl1' " +
"codebase = http://192.168.1.1/CABFILE/Tiffing.cab#version=1,0,0,0 " +
"classid='CLSID:89AFC042-93BC-4e72-AA00-343F6CF782EC' width: 100%; height: 753px' " +
" PARAM NAME='ImgLocation' VALUE=http://localhost:3631/tiff2/Download.aspx'" +
" PARAM NAME='CaseID' VALUE='" + caseID + "'" +
" PARAM NAME='FileID' VALUE='" + fileID + "'" +
" PARAM NAME='PageNumber' VALUE='" + pageNumber + "'" +
" PARAM NAME='MaxPage' VALUE='" + maxPage + "'/OBJECT";
this.Literal1.Text = strObject;


when i run this page i get massage for install cab file and when install that its show nothig after that
i also cheak in download file its entry for that cab file but status show unknon i try evrithing but
i get same problem evrytime any body knows about that give responce its realy urgent.


can you tell me what is wrong

C# developer said...

i wrote this in my .dll

namespace TIFImageEdit
{
[Guid("B5FAFAEB-F745-4f17-B599-5D7C27D5D6D5")]
public partial class TIFImageEditor : UserControl
{
private Boolean startDrag;
private Int16 rotation;

private int zoomFactor;

private string m_location;
public string ImgLocation
{
get { return m_location; }
set { m_location = value; }
}


private string m_ModifiedImageLocation;

public string ModifiedImageLocation
{
get { return m_ModifiedImageLocation; }
set { m_ModifiedImageLocation = value; }
}
and i did this

hi I developed one class liabrary in C# .net and i am launching that file as activeX control
web page.After i get dll i developed .inf file its like this


[version]
; version signature (same for both NT and Win95) do not remove
signature="$CHICAGO$"
AdvancedINF=2.0

[Add.Code]
TIFActiveX.ocx=TIFActiveX.ocx
TIFImageEditor.dll=TIFImageEditor.dll
sharpPDF.dll=sharpPDF.dll

[sharpPDF.dll]
file-win32-x86=thiscab
FileVersion=1,0,1870,19498
DestDir=11
RegisterServer=yes

[TIFImageEditor.dll]
file-win32-x86=thiscab
clsid={89AFC042-93BC-4e72-AA00-343F6CF782EC}
FileVersion=1,0,0,0
RegisterServer=yes

[TIFActiveX.ocx]
file=thiscab
RegisterServer=yes
FileVersion=1,0,0,0
; end of INF file

this time i wrapped my class liabrary in MFC and after that i get .OCX file and using of .dll , .ocx, .inf
i get .cab file and i sign that .cab file with this command signtoll sign /f .pfx /p .cab .
and i launch that cab file in web page like .aspx page goes this
script LANGUAGE="javascript"
function Scrape()
{
try
{

var objDownload = new ActiveXObject("TIFImageEditor.TIFImageEditor");
alert('trying');
objDownload.TIFImageEditor_Load();
}
catch(exception) {
alert( "Failed" );
}
}
/script

/head
body
form id="form1" runat="server"
div
asp:Literal ID="Literal1" runat="server"/asp:Literal
/div
/form
/body
/html

and .aspx.cs goes this

ing strObject = @"OBJECT id='myControl1' name='myControl1' " +
"codebase = http://192.168.1.1/CABFILE/Tiffing.cab#version=1,0,0,0 " +
"classid='CLSID:89AFC042-93BC-4e72-AA00-343F6CF782EC' width: 100%; height: 753px' " +
" PARAM NAME='ImgLocation' VALUE=http://localhost:3631/tiff2/Download.aspx'" +
" PARAM NAME='CaseID' VALUE='" + caseID + "'" +
" PARAM NAME='FileID' VALUE='" + fileID + "'" +
" PARAM NAME='PageNumber' VALUE='" + pageNumber + "'" +
" PARAM NAME='MaxPage' VALUE='" + maxPage + "'/OBJECT";
this.Literal1.Text = strObject;


when i run this page i get massage for install cab file and when install that its show nothig after that
i also cheak in download file its entry for that cab file but status show unknon i try evrithing but
i get same problem evrytime any body knows about that give responce its realy urgent.


can you tell me what is wrong