深圳全飞鸿
标题:
C#编写COM组件,验证有效
[打印本页]
作者:
admin
时间:
2019-6-24 23:01
标题:
C#编写COM组件,验证有效
1、新建一个类库项目
2. 项目属性->应用程序->程序集信息->使程序集COM可见
3. 项目属性->生成->为COM互操作注册
4. 切换到“签名”选项卡
勾选“为程序集签名”
在下面的下拉框里面选择“<新建...>”
在弹出的对话框里面,输入MyKey。。或者随便取个名字
去掉“使用密码保护文件(P)”的选项
5.、开始编码,任何一个公开的类,必须有一个 I开通的接口定义
参考:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace nagomes.tools.zxing
{
[ComVisible(true)]
[Guid("7C49E406-C4A7-4279-909E-83427FB4C8E3")]
public interface ITrsn_print
{
void Initialize();
void Dispose();
int Test(int x, int y);
}
[ComVisible(true)]
[Guid("105063F8-6B56-4806-B674-1EFD730F01C0")]
[ProgId("nagomes_tools_zxing.Trsn_print")]
public class Trsn_print : ITrsn_print
{
public void Initialize()
{
// nothing to do
}
public void Dispose()
{
// nothing to do
}
public int Test(int x, int y)
{
return x + y;
}
}
}
复制代码
作者:
admin
时间:
2019-6-24 23:02
delphi 引用:
zhuce:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm nagomes.tools.zxing.dll
uses ComObj;
var
a: olevariant;
i:integer;
begin
a := CreateOleObject('nagomes_tools_zxing.Trsn_print');
i:=a.Test(3,4);
showmessage(inttostr(i));
end;
复制代码
作者:
zhgc
时间:
2019-6-26 00:20
delphi调用没有问题,但是在用VB6调用时会报错
Private Sub Command1_Click()
Dim xlApp As Object
Set xlApp = CreateObject("nagomes_tools_zxing.Trsn_print")
MsgBox xlApp.Test(3, 4)
End Sub
复制代码
改为带/codebase参数的注册方式
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /u nagomes.tools.zxing.dll
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase nagomes.tools.zxing.dll
pause
复制代码
但是已明确指出/codebase不能注册没签名的程序 ,不知道后面会不会有问题。暂时xp和win10上可以用!
作者:
zhgc
时间:
2019-6-26 00:28
php测试
<?php
ini_set("com.allow_dcom","true");
try {
$word = new COM("nagomes_tools_zxing.Trsn_print");
print "$word->Add('22','33')";
}catch (Exception $e){
print_r($e->getMessage());
}
?>
作者:
admin
时间:
2019-6-26 00:50
这个注册过程应该好一点:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /u nagomes.tools.zxing.dll
gacutil /i nagomes.tools.zxing.dll
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm nagomes.tools.zxing.dll
pause
复制代码
注意gacutil有版本差异,一定要用对应framework4.0的
gacutil下载
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2