这篇文章主要介绍了c#调用vc写的ocx控件示例,需要的朋友可以参考下
在c#中怎样调用VC写的OCX控件,主要有两个关键环节
两个关键环节:导入ocx控件,创建实例。
1 注册ocx:
- regsvr32.exe alasunsmscon.ocx
复制代码
2 在CMD窗口中执行:
- AxImp.exe alasunsmscon.ocx
复制代码
生成两个文件:alasun.dll,Axalasun.dll
3 在项目中引用Axalasun.dll
4 调用
- Axalasun.Axalasunsms sms = new Axalasun.Axalasunsms();
- sms.CreateControl();
复制代码
就可以使用了
这里还有一个c#控制台模式下调用vc开发的ocx组件的示例
1、注册组件 regsvr32 C:\WINDOWS\system32\HX.ocx
2、用Aximp.exe工具对HX.ocx做转换,对APT.ocx再次封装成两个类库(HX.dll,AxHX.dll)
3、代码
- using System;
- using System.Data;
- using System.IO;
- using System.Messaging;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Web;
- using System.Data.SqlClient;
- using AXHH;//ocx控件
- using HH;//ocx控件
- using System.Windows.Forms;
- namespace Ocx_test
- {
- public class Program
- {
- //设置静态变量
- static AXHH hh;
- [STAThread]
- static void Main(string[] args)
- {
- //初始化ocx控件
- hh = new AXHH();
- //显式调用该实例的CreateControl()方法才能调用其他方法否则在调用时系统将报AxHost.InvalidActiveXStateException异常
- hh.CreateControl();
- //调用控件属性及方法
- hh.ip="127.0.0.1";
- hh.test();
- }
- }
复制代码
4、注意,如果是多线程调用,则在 线程名.Start() 前加上,例如:
- 线程名.SetApartmentState(System.Threading.ApartmentState.STA);//设置模式,必须在启动之前设置。
- // 启动线程将调用 ThreadStart 委托。
- 线程名.Start();
复制代码
|
|