环境:VS2010...WIN7系统 设置工程属性:“生成”-> “为COM Interop注册”。 当然也可以为每个接口设置COM可见性,ComVisibleAttribute类提供了这样的控制。 更改AssemblyInfo.cs……设置COM可见
// 将? ComVisible 设Θ?置?为a false 使?此?程ì序ò集ˉ中D的?类え?型í // 对? COM 组哩?件t不?可é见?。£如?果?需è要癮从洙?COM 访?问ê此?程ì序ò集ˉ中D的?类え?型í,? // 则ò将?该?类え?型í上?的? ComVisible 特?性?设Θ?置?为a true。£ [assembly: ComVisible(true)]
建立源码
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
namespace MyLib { [ComVisible(true)] [Guid("2CBD3D76-35F1-4f9d-9C1B-9DBFEE412F77")] public interface IMyClass { void Initialize(); void Dispose(); String VC(String path); }
[ComVisible(true)] [Guid("EA2F140A-108F-47ae-BBD5-83EEE646CC0e")] [ProgId("MyLib.MyClassa")] public class MyClass : IMyClass { public void Initialize() { //nothing todo } public void Dispose() { //nothing todo } public String VC(String path) { // x + y; if (path.Trim().Equals("")) {
return "no pathx"; }
fastvcAtiLib.fastvcyjmC2k c = new fastvcAtiLib.fastvcyjmC2k(); // c.novc = true; //4 test .only show pic .not show vercode text // c.oriImg = true; //img not process and get vercode... cant echo show img pic .. c.vg = 1; c.sid = "c2n"; string VerifyCode; try { VerifyCode = c.RecByte(path); } catch (Exception ex) { VerifyCode = ex.Message; } return VerifyCode; } } } 注册COM组件DLL regasm ClassLibrary1.dll /codebase
如果你的DLL不 是强签名的,则必须要使用/codebase参数
反注册使用 regasm /u ClassLibrary1.dll
使用HTML中VBS脚本测试
<script language="VBScript"> Dim o : Set o=CreateObject("MyLib.MyClass") o.Initialize MsgBox "1 + 2 = " & o.vc("c:\dx.jpg") o.Dispose Set o=Nothing </script> 提示不能创建 ACTIVEX 控件……
原因:是因为注册没有注册上……
提示找不到路径
说明已经注册上了。可能DLL不是强签名的,而又没有指定/CODEBASE属性,导致注册里没有这个键值。而查找不到。