PB9(包括 PB9)以前的版本都不能访问VS.Net创建的基于Net FrameWork下的程序,除非将程序编译成COM,PB才能正常调用。
以下是我总结出的C#将类库编译成COM所需要的步骤:
1.创建一个类库程序;
2.在程序里添加using System.Runtime.InteropServices;
3.创建一个公用的函数,以供PB调用;
4.启动命令窗口;
5.生成snk文件:
sn -k test2.snk
6.将cs文件编译成dll文件:
csc /t:library /keyfile:test2.snk /out:test2.dll test2.cs
如果在程序中添加的引用不属于System下的,例如:Microsoft.VisialBasic.dll,编译的语句改为:
csc /t:library /keyfile:test2.snk /out:test2.dll /r:Microsoft.VisualBasic.dll test2.cs
7.注册COM----------这一步非常重要:
regasm test2.dll /tlb:test2.tlb /codebase
C#例子的全部代码如下:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace test2 { public class test2 { public int ShowMsg(out int a1,out string a2) { a1 = 200; a2 = "message"; return 100; } } } |
PB调用的方法如下:
OLEObject test2 long ll_status integer li_return long ll_a1 string ls_a2 //创建OLEObject对象 test2 = Create OLEObject ll_status= test2.ConnectToNewObject("test2.test2") if ll_status=0 then //调用类内的公共函数 li_return = test2.ShowMsg(ref ll_a1,ref ls_a2) messagebox(string(ll_a1)+" "+ls_a2,string(li_return)) end if //释放资源 test2.DisConnectObject() |