一切福田,不離方寸,從心而覓,感無不通。

C# 开发Activex

由于要在网页上显示Tree的结构,开始使用JQuery的Tree,但是当资料量很大时会导致第一次加载很慢。(特别慢,资料量太大了网页一下还反映不过来),于是改用JQuery的FileTree的插件,每层通过ajax的方式到后台获取资料,结果还好,不过当资料量大的时候,频繁的点击页面,反映也会很慢,而且要求Client的配置好些,而这边的site上帝配置都比较低,有的机器会导致无反映的情况。无奈改用Activex的方式吧。对vc不是特别的熟悉,于是选择了C#,因为site上的每个Client都会有.net Framework。OK..开始改写。

主要参考:http://www.codeproject.com/KB/cs/CreateActiveXDotNet.aspx

1.创建类库工程

2.添加一个UserControl,AxTree

3.定义AxTree Class的属性

[ProgId("MyTreeActiveX.AxTree")]
[Guid("A5C532BA-45B2-44c7-ACAE-D526EF9D47B0")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComSourceInterfaces(typeof(ControlEvents))]

public partial class AxTree : UserControl, IObjectSafety

说明:

ComVisible:是否可以见

ComSourceInterfaces:该Activex实现的事件的类

IObjectSafety:继承与该接口,标记安全,否则在生产Activex后,和网页交互还需要对ie进行设定。

[
Serializable,
ComVisible(true)
]
public enum ObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
INTERFACE_USES_DISPEX = 0x00000004,
INTERFACE_USES_SECURITY_MANAGER = 0x00000008
}

[
ComImport(),
Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface IObjectSafety
{
[PreserveSig]
long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions);

[PreserveSig]
long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions);
};

4.添加方法,属性

[ComVisible(true)]
public string Leve1ColorRGB{get..set…}

方法和属性添加 [ComVisible(true)] 就好

5.添加Event

写一个Event的接口,ControlEvent

[ComVisible(true)]
[Guid("5F6E5E2D-8C6D-4524-AD44-E8B169D90371")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ControlEvents
{

[DispId(0x60020001)]
void OnDial(string callno, string updeppkey);

}

在AxTree中定义对应的Event:

public event ControlEventHandler OnDial;

6.实现IObjectSafety接口

#region [IObjectSafety implementation]
public long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return 0;
}

public long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions)
{
return 0;
}
#endregion

 

7.注册和反注册 Atviex

[ComRegisterFunction()]
public static void RegisterClass(string key)
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don’t need it
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\", "");

// Open the CLSID\{guid} key for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

// And create the 'Control' key – this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey("Control");
ctrl.Close();

// Next create the CodeBase entry – needed if not string named and GACced.
RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();

// Finally close the main key
k.Close();

}

[ComUnregisterFunction()]
public static void UnregisterClass(string key)
{
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\", "");

// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

// Delete the 'Control' key, but don’t throw an exception if it does not exist
k.DeleteSubKey("Control", false);

// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey("CodeBase", false);

// Finally close the main key
k.Close();
}

 

8.生成强命名文件。xxx.snk.编译生成…ActvieX的dll写好了。

 

安装:

在该解决方案中添加一个安装项目,选择项目输出->主输出,确认ActiveX的dll文件的属性Register:vsdrpCOM,在调整下其他的属性(文件名..)编译就好。

cab的安装制作:

下载cabsdk的工具,微软网站上有的下。

下载后执行如下:

C:\cabsdk\BIN\MyTreeActiveX>cabarc.exe n TreeActiveX.cab TreeActiveXSetup.msi My
TreeActiveX.inf

Microsoft (R) Cabinet Tool – Version 1.00.0601 (03/18/97)
Copyright (c) Microsoft Corp 1996-1997. All rights reserved.

Creating new cabinet 'TreeActiveX.cab' with compression 'MSZIP':
— adding TreeActiveXSetup.msi
— adding MyTreeActiveX.inf

Completed successfully

会生成一个TreeActiveX.cab的文件,网页上就可以使用了。

对了。inf的定义:

[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1

[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\TreeActiveXSetup.msi" /qn

 

网页的写法:

<object id="ActivexObject" name="ActivexObject" width="100%" height="100%" classid="clsid:A5C532BA-45B2-44c7-ACAE-D526EF9D47B0"></object>

<script language=’javascript'   for=’ActivexObject'   event=’OnDial(tel,uppkey)'>

dosame…

</script>

 

通过以上步骤完成ActiveX的编写,cab的制作和网页的部分。部署好后,会正常使用。

不过该ActiveX没有签名,所有需要设定一下ie,允许下载为签名的activex才可以正常下载和使用。

如何对ActiveX进行签名我还不知道,等有时间使用Google大法好好再查查看。

from:http://www.cnblogs.com/zhucl1006/archive/2009/08/22/1551779.html